با سلام خدمت دوستان من برنامه ی زیر را یکمیش رو نوشتم یکمش رو هم توسعه دادم و این برنامه ای که کدش قرار داده شده تهیه شده و کارکردی خوبی هم داره. کار برنامه تولید یک شکل موج سینوسی هست که قابلیت تنظیم دامنه، فرکانس و فاز رو داره... بگذریم حالا می خوام با پالسی که از خروجی یک مدار اشکار ساز عبور از صفر برق شهر میاد موج سینوسی 50HZخروجی از برنامه رو سنکرون کنم یعنی موقعی که پالس اومد برنامه شروع به کار کنه موج سینوسی رو تولید کنه یعدش دوباره صبر کنه تا پالس بعدی بیاد و دوباره همون موج رو تولید کنه... (اومدن پالس رو هم با اینتراپت 2 تشخیص میدم فرکانس پالس و موج سینوسی برابر هست) البته اگر هر چندتا پالس این کار رو بکنه هم طوری نیست تا بار پردازشی میکرو کمتر بشه... برای این کار باید چکار کنم؟ با نوشتن کد من رو راهنمایی کنید (معمولا راهنمایی بدون کد راهی به جایی نمیبره)
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.8d Professional
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2016/03/09
Author : Amin
Company :
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 16.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <stdlib.h>
#include <delay.h>
#define keypad_DDR DDRA
#define keypad_PORT PORTA
#define keypad_PIN PINA
#define key_ctr 5
#define key_delay 1
flash unsigned char const source[5][256] = {
{
128 , 129 , 129 , 130 , 131 , 131 , 132 , 133 , 133 , 134 , 134 , 135 , 136 , 136 , 137 , 137 ,
138 , 139 , 139 , 140 , 140 , 141 , 141 , 142 , 142 , 143 , 143 , 144 , 144 , 145 , 145 , 146 ,
146 , 146 , 147 , 147 , 148 , 148 , 148 , 149 , 149 , 149 , 150 , 150 , 150 , 151 , 151 , 151 ,
151 , 152 , 152 , 152 , 152 , 152 , 152 , 153 , 153 , 153 , 153 , 153 , 153 , 153 , 153 , 153 ,
153 , 153 , 153 , 153 , 153 , 153 , 153 , 153 , 152 , 152 , 152 , 152 , 152 , 152 , 151 , 151 ,
151 , 151 , 150 , 150 , 150 , 149 , 149 , 149 , 148 , 148 , 148 , 147 , 147 , 146 , 146 , 146 ,
145 , 145 , 144 , 144 , 143 , 143 , 142 , 142 , 141 , 141 , 140 , 140 , 139 , 139 , 138 , 137 ,
137 , 136 , 136 , 135 , 134 , 134 , 133 , 133 , 132 , 131 , 131 , 130 , 129 , 129 , 128 , 128 ,
127 , 126 , 126 , 125 , 124 , 124 , 123 , 123 , 122 , 121 , 121 , 120 , 120 , 119 , 118 , 118 ,
117 , 117 , 116 , 116 , 115 , 114 , 114 , 113 , 113 , 112 , 112 , 111 , 111 , 110 , 110 , 110 ,
109 , 109 , 108 , 108 , 107 , 107 , 107 , 106 , 106 , 106 , 105 , 105 , 105 , 105 , 104 , 104 ,
104 , 104 , 103 , 103 , 103 , 103 , 103 , 103 , 102 , 102 , 102 , 102 , 102 , 102 , 102 , 102 ,
102 , 102 , 102 , 102 , 102 , 102 , 102 , 103 , 103 , 103 , 103 , 103 , 103 , 104 , 104 , 104 ,
104 , 105 , 105 , 105 , 105 , 106 , 106 , 106 , 107 , 107 , 107 , 108 , 108 , 109 , 109 , 110 ,
110 , 110 , 111 , 111 , 112 , 112 , 113 , 113 , 114 , 114 , 115 , 116 , 116 , 117 , 117 , 118 ,
118 , 119 , 120 , 120 , 121 , 121 , 122 , 123 , 123 , 124 , 124 , 125 , 126 , 126 , 127 , 128 }, //-------%20-------
{
129 , 130 , 131 , 132 , 134 , 135 , 136 , 138 , 139 , 140 , 141 , 142 , 144 , 145 , 146 , 147 ,
148 , 150 , 150 , 152 , 153 , 154 , 155 , 156 , 157 , 158 , 159 , 160 , 161 , 162 , 163 , 164 ,
164 , 165 , 166 , 167 , 168 , 168 , 169 , 170 , 171 , 171 , 172 , 172 , 173 , 174 , 174 , 175 ,
175 , 176 , 176 , 176 , 177 , 177 , 177 , 178 , 178 , 178 , 178 , 178 , 178 , 178 , 178 , 178 ,
178 , 178 , 178 , 178 , 178 , 178 , 178 , 178 , 177 , 177 , 177 , 176 , 176 , 176 , 175 , 175 ,
174 , 174 , 173 , 172 , 172 , 171 , 171 , 170 , 169 , 168 , 168 , 167 , 166 , 165 , 164 , 164 ,
163 , 162 , 161 , 160 , 159 , 158 , 157 , 156 , 155 , 154 , 153 , 152 , 150 , 150 , 148 , 147 ,
146 , 145 , 144 , 142 , 141 , 140 , 139 , 138 , 136 , 135 , 134 , 132 , 131 , 130 , 129 , 128 ,
126 , 125 , 124 , 123 , 121 , 120 , 119 , 118 , 116 , 115 , 114 , 113 , 112 , 110 , 109 , 108 ,
107 , 106 , 105 , 104 , 102 , 101 , 100 , 99 , 98 , 97 , 96 , 95 , 94 , 93 , 92 , 92 ,
91 , 90 , 89 , 88 , 87 , 87 , 86 , 85 , 84 , 84 , 83 , 83 , 82 , 82 , 81 , 80 ,
80 , 80 , 79 , 79 , 78 , 78 , 78 , 78 , 77 , 77 , 77 , 77 , 77 , 77 , 77 , 77 ,
77 , 77 , 77 , 77 , 77 , 77 , 77 , 78 , 78 , 78 , 78 , 79 , 79 , 80 , 80 , 80 ,
81 , 82 , 82 , 83 , 83 , 84 , 84 , 85 , 86 , 87 , 87 , 88 , 89 , 90 , 91 , 92 ,
92 , 93 , 94 , 95 , 96 , 97 , 98 , 99 , 100 , 101 , 102 , 104 , 105 , 106 , 107 , 108 ,
109 , 110 , 112 , 113 , 114 , 115 , 116 , 118 , 119 , 120 , 121 , 123 , 124 , 125 , 126 , 128 }, //-------%40-------
{
130 , 131 , 133 , 135 , 137 , 139 , 141 , 143 , 144 , 146 , 148 , 150 , 152 , 154 , 155 , 157 ,
159 , 161 , 162 , 164 , 166 , 167 , 168 , 170 , 172 , 173 , 174 , 176 , 178 , 179 , 180 , 182 ,
183 , 184 , 186 , 187 , 188 , 189 , 190 , 191 , 192 , 193 , 194 , 195 , 196 , 197 , 198 , 198 ,
199 , 200 , 200 , 201 , 202 , 202 , 202 , 203 , 203 , 203 , 204 , 204 , 204 , 204 , 204 , 204 ,
204 , 204 , 204 , 204 , 204 , 203 , 203 , 203 , 202 , 202 , 202 , 201 , 200 , 200 , 199 , 198 ,
198 , 197 , 196 , 195 , 194 , 193 , 192 , 191 , 190 , 189 , 188 , 187 , 186 , 184 , 183 , 182 ,
180 , 179 , 178 , 176 , 174 , 173 , 172 , 170 , 168 , 167 , 166 , 164 , 162 , 161 , 159 , 157 ,
155 , 154 , 152 , 150 , 148 , 146 , 144 , 143 , 141 , 139 , 137 , 135 , 133 , 131 , 130 , 128 ,
126 , 124 , 122 , 121 , 118 , 116 , 114 , 113 , 111 , 109 , 107 , 106 , 104 , 102 , 100 , 98 ,
96 , 95 , 94 , 92 , 90 , 88 , 87 , 85 , 84 , 82 , 81 , 79 , 78 , 76 , 75 , 74 ,
72 , 71 , 70 , 68 , 67 , 66 , 65 , 64 , 63 , 62 , 61 , 60 , 59 , 59 , 58 , 57 ,
56 , 56 , 55 , 54 , 54 , 53 , 53 , 53 , 52 , 52 , 52 , 52 , 52 , 52 , 52 , 52 ,
52 , 52 , 52 , 52 , 52 , 52 , 52 , 53 , 53 , 53 , 54 , 54 , 55 , 56 , 56 , 57 ,
58 , 59 , 59 , 60 , 61 , 62 , 63 , 64 , 65 , 66 , 67 , 68 , 70 , 71 , 72 , 74 ,
75 , 76 , 78 , 79 , 81 , 82 , 84 , 85 , 87 , 88 , 90 , 92 , 94 , 95 , 96 , 98 ,
100 , 102 , 104 , 106 , 107 , 109 , 111 , 113 , 114 , 116 , 118 , 121 , 122 , 124 , 126 , 128 }, //-------%60-------
{
130 , 133 , 135 , 138 , 141 , 143 , 146 , 148 , 150 , 153 , 155 , 158 , 160 , 162 , 165 , 167 ,
170 , 172 , 174 , 176 , 178 , 181 , 182 , 185 , 186 , 189 , 190 , 193 , 194 , 197 , 198 , 200 ,
202 , 203 , 206 , 207 , 209 , 210 , 211 , 213 , 214 , 215 , 217 , 218 , 219 , 220 , 222 , 222 ,
223 , 224 , 225 , 226 , 226 , 227 , 227 , 228 , 229 , 229 , 230 , 230 , 230 , 230 , 230 , 230 ,
230 , 230 , 230 , 230 , 230 , 229 , 229 , 228 , 227 , 227 , 226 , 226 , 225 , 224 , 223 , 222 ,
222 , 220 , 219 , 218 , 217 , 215 , 214 , 213 , 211 , 210 , 209 , 207 , 206 , 203 , 202 , 200 ,
198 , 197 , 194 , 193 , 190 , 189 , 186 , 185 , 182 , 181 , 178 , 176 , 174 , 172 , 170 , 167 ,
165 , 162 , 160 , 158 , 155 , 153 , 150 , 148 , 146 , 143 , 141 , 138 , 135 , 133 , 130 , 128 ,
126 , 123 , 121 , 118 , 115 , 113 , 110 , 108 , 106 , 103 , 101 , 98 , 96 , 94 , 91 , 89 ,
86 , 84 , 82 , 80 , 78 , 75 , 74 , 71 , 70 , 67 , 66 , 63 , 62 , 59 , 58 , 56 ,
54 , 53 , 50 , 49 , 47 , 46 , 45 , 43 , 42 , 41 , 39 , 38 , 37 , 36 , 34 , 34 ,
33 , 32 , 31 , 30 , 30 , 29 , 29 , 28 , 27 , 27 , 26 , 26 , 26 , 26 , 26 , 26 ,
26 , 26 , 26 , 26 , 26 , 27 , 27 , 28 , 29 , 29 , 30 , 30 , 31 , 32 , 33 , 34 ,
34 , 36 , 37 , 38 , 39 , 41 , 42 , 43 , 45 , 46 , 47 , 49 , 50 , 53 , 54 , 56 ,
58 , 59 , 62 , 63 , 66 , 67 , 70 , 71 , 74 , 75 , 78 , 80 , 82 , 84 , 86 , 89 ,
91 , 94 , 96 , 98 , 101 , 103 , 106 , 108 , 110 , 113 , 115 , 118 , 121 , 123 , 126 , 128 }, //-------%80-------
{
131 , 134 , 137 , 140 , 144 , 147 , 150 , 153 , 156 , 159 , 162 , 165 , 168 , 171 , 174 , 177 ,
180 , 183 , 185 , 188 , 191 , 194 , 196 , 199 , 201 , 204 , 206 , 209 , 211 , 214 , 216 , 218 ,
220 , 222 , 225 , 227 , 229 , 230 , 232 , 234 , 236 , 237 , 239 , 240 , 242 , 243 , 245 , 246 ,
247 , 248 , 249 , 250 , 251 , 252 , 252 , 253 , 254 , 254 , 255 , 255 , 255 , 255 , 255 , 255 ,
255 , 255 , 255 , 255 , 255 , 254 , 254 , 253 , 252 , 252 , 251 , 250 , 249 , 248 , 247 , 246 ,
245 , 243 , 242 , 240 , 239 , 237 , 236 , 234 , 232 , 230 , 229 , 227 , 225 , 222 , 220 , 218 ,
216 , 214 , 211 , 209 , 206 , 204 , 201 , 199 , 196 , 194 , 191 , 188 , 185 , 183 , 180 , 177 ,
174 , 171 , 168 , 165 , 162 , 159 , 156 , 153 , 150 , 147 , 144 , 140 , 137 , 134 , 131 , 128 ,
125 , 122 , 119 , 116 , 112 , 109 , 106 , 103 , 100 , 97 , 94 , 91 , 88 , 85 , 82 , 79 ,
76 , 73 , 71 , 68 , 65 , 62 , 60 , 57 , 55 , 52 , 50 , 47 , 45 , 42 , 40 , 38 ,
36 , 34 , 31 , 29 , 27 , 26 , 24 , 22 , 20 , 19 , 17 , 16 , 14 , 13 , 11 , 10 ,
9 , 8 , 7 , 6 , 5 , 4 , 4 , 3 , 2 , 2 , 1 , 1 , 1 , 1 , 1 , 1 ,
1 , 1 , 1 , 1 , 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,
11 , 13 , 14 , 16 , 17 , 19 , 20 , 22 , 24 , 26 , 27 , 29 , 31 , 34 , 36 , 38 ,
40 , 42 , 45 , 47 , 50 , 52 , 55 , 57 , 60 , 62 , 65 , 68 , 71 , 73 , 76 , 79 ,
82 , 85 , 88 , 91 , 94 , 97 , 100 , 103 , 106 , 109 , 112 , 116 , 119 , 122 , 125 , 128 }}; //------%100-------
#define D20 0
#define D40 1
#define D60 2
#define D80 3
#define D100 4
#define F1hz 125000
#define dac1 PORTD
#define F1 14
#define F2 24
#define F3 34
#define _FF_ 0.71112
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
// Declare your global variables here
unsigned char Psec = D100; //perSec Damane Freq
unsigned char ph1 = 0; //zavie phase 1
unsigned char ph2 = 0; //zavie phase 2
unsigned long T1; //Timer Numeric
unsigned int frequnce = 50;
unsigned int radu = 0;
unsigned char scale = 100;
int a=0;
// Timer 1 overflow interrupt service routine
interrupt [EXT_INT2] void pulse (void)
{
a++;
if(a==2)
a=0;
}
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer 1 value
if(a==1)
{
TCNT1=T1;
dac1 = source[Psec][ph2];
ph2+=2;
}
//TCNT1L=0xAA;
// Place your code here
}
// Declare your global variables here
//////////////////////////////////////////////
//////////////////////////////////////////////
unsigned char scan_key(void)
{
unsigned char i=0,j=0,key_ctr_temp=0,key_temp=0,scan_code[4]={0xFE,0xFD,0xFB,0xF7};
for (i=0;i<4;i++)
{
keypad_PORT|=0x0F;
keypad_PORT&=scan_code[i];
delay_ms(5);
//key_temp=keypad_PIN;
if (keypad_PIN<0xF0)
{
key_temp=keypad_PIN;
for(j=0;j<key_ctr;j++)
{
delay_ms(key_delay);
if(keypad_PIN==key_temp)
key_ctr_temp++;
}
// if(key_ctr_temp==key_ctr)
{ key_ctr_temp = keypad_PIN;
switch(key_ctr_temp)
{
case 238:
return 1;
break;
case 237:
return 4;
break;
case 235:
return 7;
break;
case 231:
return 41;
break;
case 222:
return 2;
break;
case 221:
return 5;
break;
case 219:
return 8;
break;
case 215:
return 0;
break;
case 190:
return 3;
break;
case 189:
return 6;
break;
case 187:
return 9;
break;
case 183:
return 43;
break;
case 126:
return 14;
break;
case 125:
return 24;
break;
case 123:
return 34;
break;
case 119:
return 44;
break;
default:
return 255;
break;
}
}
}
}
return 255 ;
}
//////////////////////////////////////////////
//////////////////////////////////////////////
unsigned int get_num(unsigned int min, unsigned int max)
{
unsigned int numb = 0;
unsigned char _num, _x = 0, _key;
_num = 0;
do
{
lcd_gotoxy(_x, 1);
_key = scan_key();
if (_key != 255)
{
delay_ms(5);
_key = scan_key();
if (_key != 255)
{
switch (_key)
{
case 1 : if (_x < 4) {_num = 1; numb = numb *10; numb += _num; lcd_putsf("1"); _x++; delay_ms(150);} break;
case 2 : if (_x < 4) {_num = 2; numb = numb *10; numb += _num; lcd_putsf("2"); _x++; delay_ms(150);} break;
case 3 : if (_x < 4) {_num = 3; numb = numb *10; numb += _num; lcd_putsf("3"); _x++; delay_ms(150);} break;
case 4 : if (_x < 4) {_num = 4; numb = numb *10; numb += _num; lcd_putsf("4"); _x++; delay_ms(150);} break;
case 5 : if (_x < 4) {_num = 5; numb = numb *10; numb += _num; lcd_putsf("5"); _x++; delay_ms(150);} break;
case 6 : if (_x < 4) {_num = 6; numb = numb *10; numb += _num; lcd_putsf("6"); _x++; delay_ms(150);} break;
case 7 : if (_x < 4) {_num = 7; numb = numb *10; numb += _num; lcd_putsf("7"); _x++; delay_ms(150);} break;
case 8 : if (_x < 4) {_num = 8; numb = numb *10; numb += _num; lcd_putsf("8"); _x++; delay_ms(150);} break;
case 9 : if (_x < 4) {_num = 9; numb = numb *10; numb += _num; lcd_putsf("9"); _x++; delay_ms(150);} break;
case 0 : if (_x < 4) {_num = 0; numb = numb *10; numb += _num; lcd_putsf("0"); _x++; delay_ms(150);} break;
case 43 : if (_x > 0) {_x--; lcd_gotoxy(_x, 1); lcd_putsf(" "); numb = numb / 10; delay_ms(150);}; break;
case 44 : if (numb < min) {numb = min; }; if (numb > max) {numb = max; }; break;
}
lcd_gotoxy(_x, 1);
}
}
} while(_key != 44);
//numb = max-min;
return numb;
}
void lcd_panel1(void)
{
char s_s[5];
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Freq : hz. ");
lcd_gotoxy(0, 1);
lcd_putsf("R : . Y-G:% ");
itoa(frequnce, s_s);
lcd_gotoxy(7, 0);
lcd_puts(s_s);
itoa(radu, s_s);
lcd_gotoxy(3, 1);
lcd_puts(s_s);
itoa(scale, s_s);
lcd_gotoxy(13, 1);
lcd_puts(s_s);
.
.
.
}
void panel2(void) // change freq
{
.
.
.
.
}
void panel3(void) // change phase
{
.
.
.
}
void panel4(void) // change scale
{
.
.
.
}
void main(void)
{
unsigned char keybd;
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTA=0xF0;
DDRA=0x0F;
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 16000.000 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x01;
TCNT1H=0xAA;
TCNT1L=0xAA;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x40;
GICR=0x20;
GIFR=0x20;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
while (1)
{
.
.
.
}
}
// }
};
}
دیدگاه