Rev 28 | Rev 52 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
28 | walter | 1 | //############################################################################ |
51 | walter | 2 | // - PWM CTRL |
3 | // - Main |
||
28 | walter | 4 | // - ATMEGA8 mit 8MHz |
5 | // - Nur für den privaten Gebrauch |
||
6 | // - Keine Garantie auf Fehlerfreiheit |
||
7 | // - Kommerzielle Nutzung nur mit meiner Zustimmung |
||
8 | // - walter Meyer @ www.freakware.de |
||
9 | // - 11.12.2007 |
||
10 | // - Make sure Fuses are programmed for internal 8 MHz RC Oscilator |
||
11 | //############################################################################*/ |
||
12 | |||
13 | #include "main.h" |
||
14 | #include "uart.h" |
||
15 | #include "twislave.h" |
||
16 | |||
17 | volatile unsigned int ppm_signal = 1500; |
||
18 | volatile unsigned char ppm_new = 0; |
||
19 | volatile unsigned char TMR1OvF = 0; |
||
51 | walter | 20 | volatile unsigned int TMR1MS; |
28 | walter | 21 | volatile unsigned char ch0; |
22 | volatile unsigned char ch1; |
||
23 | volatile unsigned char ch2; |
||
24 | volatile unsigned char ch3; |
||
25 | volatile unsigned char ch4; |
||
26 | volatile unsigned char ch5; |
||
51 | walter | 27 | unsigned int timer; |
28 | walter | 28 | |
29 | SIGNAL(SIG_OVERFLOW1) |
||
30 | { |
||
31 | TMR1OvF++; |
||
32 | } |
||
33 | |||
34 | |||
35 | SIGNAL(SIG_INPUT_CAPTURE1) |
||
36 | { |
||
37 | static unsigned int pos_ICR; |
||
38 | static unsigned int ppm; |
||
39 | |||
40 | if ((TCCR1B & (1<<ICES1)) != 0) //rising edge |
||
41 | { |
||
42 | TCCR1B &= ~(1<<ICES1); //set falling egde |
||
43 | TMR1OvF = 0; |
||
44 | pos_ICR = ICR1; |
||
45 | } |
||
46 | else //falling edge |
||
47 | { |
||
48 | TCCR1B |= (1<<ICES1); //set rising egde |
||
49 | ppm = (ICR1 - pos_ICR + (int) TMR1OvF * 65536); |
||
50 | if ((ppm > 600) && (ppm < 2400)) |
||
51 | { |
||
52 | if (ppm > 2100) ppm = 2100; |
||
53 | if (ppm < 900) ppm = 900; |
||
54 | ppm = (ppm_signal * 7 + ppm) / 8; |
||
55 | ppm_signal = ppm; |
||
56 | ppm_new = 1; |
||
57 | } |
||
58 | |||
59 | } |
||
60 | |||
61 | } |
||
62 | |||
63 | |||
64 | |||
65 | |||
66 | |||
67 | SIGNAL(SIG_OVERFLOW0) |
||
68 | { |
||
69 | static unsigned char counter; |
||
51 | walter | 70 | static unsigned char ms1; |
28 | walter | 71 | unsigned char PORTB_BAK; |
72 | unsigned char PORTD_BAK; |
||
73 | |||
74 | PORTB_BAK = PORTB; |
||
75 | PORTD_BAK = PORTD; |
||
76 | |||
51 | walter | 77 | if (counter++ == 254) |
28 | walter | 78 | { |
51 | walter | 79 | PORTB_BAK LEDON (CH0_B | CH1_B | CH2_B); |
80 | PORTD_BAK LEDON (CH3_D | CH4_D | CH5_D); |
||
81 | counter = 0; |
||
28 | walter | 82 | } |
83 | |||
51 | walter | 84 | if (ch0 == counter) PORTB_BAK LEDOFF CH0_B; |
85 | if (ch1 == counter) PORTB_BAK LEDOFF CH1_B; |
||
86 | if (ch2 == counter) PORTB_BAK LEDOFF CH2_B; |
||
87 | if (ch3 == counter) PORTD_BAK LEDOFF CH3_D; |
||
88 | if (ch4 == counter) PORTD_BAK LEDOFF CH4_D; |
||
89 | if (ch5 == counter) PORTD_BAK LEDOFF CH5_D; |
||
90 | |||
91 | PORTB = PORTB_BAK; |
||
92 | PORTD = PORTD_BAK; |
||
28 | walter | 93 | |
51 | walter | 94 | if (ms1++ == 32) |
28 | walter | 95 | { |
51 | walter | 96 | ms1=0; |
97 | TMR1MS++; |
||
28 | walter | 98 | } |
51 | walter | 99 | |
28 | walter | 100 | |
101 | |||
102 | |||
51 | walter | 103 | } |
28 | walter | 104 | |
105 | |||
106 | |||
51 | walter | 107 | unsigned int SetDelay (unsigned int t) |
108 | { |
||
109 | unsigned char temp_hi; |
||
110 | unsigned char temp_lo; |
||
28 | walter | 111 | |
51 | walter | 112 | temp_hi = (TMR1MS >> 8); |
113 | temp_lo = (TMR1MS & 0xff); |
||
114 | if (temp_hi != (TMR1MS >> 8)) temp_hi = (TMR1MS >> 8); |
||
28 | walter | 115 | |
51 | walter | 116 | return(((temp_hi << 8) | temp_lo) + t + 1); |
28 | walter | 117 | |
51 | walter | 118 | } |
28 | walter | 119 | |
120 | |||
51 | walter | 121 | char CheckDelay(unsigned int t) |
122 | { |
||
123 | unsigned char temp_hi; |
||
124 | unsigned char temp_lo; |
||
125 | |||
126 | temp_hi = (TMR1MS >> 8); |
||
127 | temp_lo = (TMR1MS & 0xff); |
||
128 | if (temp_hi != (TMR1MS >> 8)) temp_hi = (TMR1MS >> 8); |
||
129 | |||
130 | return(((t - ((temp_hi << 8) | temp_lo)) & 0x8000) >> 9); |
||
131 | |||
28 | walter | 132 | } |
133 | |||
134 | |||
135 | |||
136 | |||
137 | /*##############################################################################*/ |
||
51 | walter | 138 | void StartPWM(void) |
28 | walter | 139 | { |
140 | //Timer 0 Config for getting right timing for IR Pattern |
||
141 | TCCR0 = (0<<CS02)|(0<<CS01)|(1<<CS00); // (@8MHz) = 1/8us Clk = 256/8 = 32us overflow |
||
142 | TIMSK setbit (1<<TOIE0); // enable Int |
||
143 | |||
144 | } |
||
145 | |||
146 | |||
147 | |||
148 | |||
149 | /*##############################################################################*/ |
||
150 | void StartPPM(void) |
||
151 | { |
||
152 | |||
153 | //global timer1 Config |
||
154 | TCCR1A = (0<<COM1A1)|(0<<COM1A0)|(0<<COM1B1)|(0<<COM1B0)| |
||
155 | (0<<FOC1A) |(0<<FOC1B) |(0<<WGM10) |(0<<WGM11); |
||
156 | TCCR1B = (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)| |
||
157 | (0<<WGM12)|(0<<CS12)|(1<<CS11)|(0<<CS10); //ICP_POS_FLANKE |
||
158 | |||
159 | // interrupts |
||
160 | TIMSK |= (1<<TICIE1)|(1<<TOIE1); //ICP_INT_ENABLE and TIMER1_INT_ENABLE |
||
161 | |||
162 | } |
||
163 | |||
164 | |||
165 | |||
166 | int GetPPM(void) |
||
167 | { |
||
168 | //this routines seems to be nesseccary, as reading a 16 bit value |
||
169 | //on a 8 bit machine is not atomic, so if an interrupt apears between reading |
||
170 | //low and high byte of the 16 bit value a wrong result is possible |
||
171 | |||
172 | unsigned char intmask; |
||
173 | unsigned int ppm_temp; |
||
174 | |||
175 | intmask = TIMSK; //backup interupt enable bits |
||
176 | TIMSK &= ~(1<<TICIE1); //disable ppm interrupt |
||
177 | ppm_temp = ppm_signal; |
||
178 | TIMSK = intmask; //restore interupt enable bits |
||
179 | return(ppm_temp); //return ppm_signal |
||
180 | |||
181 | } |
||
182 | |||
183 | |||
184 | /*##############################################################################*/ |
||
185 | // MAIN |
||
186 | /*##############################################################################*/ |
||
187 | int main (void) |
||
188 | { |
||
51 | walter | 189 | unsigned int i; |
190 | unsigned char colorState; |
||
191 | unsigned char nextcolorState; |
||
28 | walter | 192 | |
51 | walter | 193 | DDRB = (CH0_B|CH1_B|CH2_B); |
194 | PORTB = 0x00; |
||
195 | |||
196 | DDRC = (ledred); |
||
28 | walter | 197 | PORTC = 0x00; |
51 | walter | 198 | |
199 | DDRD = (ledgreen|CH3_D|CH4_D|CH5_D); |
||
28 | walter | 200 | PORTD = 0x00; |
201 | |||
202 | ch0 = 0; |
||
203 | ch1 = 0; |
||
204 | ch2 = 0; |
||
205 | ch3 = 0; |
||
206 | ch4 = 0; |
||
207 | ch5 = 0; |
||
208 | |||
209 | //StartUART(); |
||
210 | StartPPM(); |
||
51 | walter | 211 | //StartI2C(); |
28 | walter | 212 | StartPWM(); |
213 | sei(); |
||
214 | |||
51 | walter | 215 | |
216 | //rot > Violett > Blau > Türkis > Grün > Gelb > |
||
217 | |||
218 | |||
219 | colorState = 0; |
||
28 | walter | 220 | |
221 | while (1) |
||
222 | { |
||
51 | walter | 223 | |
28 | walter | 224 | //printf("%d ",ppm_signal); |
225 | |||
51 | walter | 226 | for (i=0; i<=255; i++) |
227 | { |
||
228 | switch(colorState) |
||
229 | { |
||
230 | case 0: |
||
231 | ch0 = i; //fade in (ch0) red |
||
232 | nextcolorState = 2; |
||
233 | break; |
||
28 | walter | 234 | |
51 | walter | 235 | case 2: |
236 | ch1 = i; //fade in (ch1) blue to get purple (red + blue) |
||
237 | nextcolorState = 4; |
||
238 | break; |
||
239 | |||
240 | case 4: |
||
241 | ch0 = 255 - i; //fade out (ch0) red to get pure blue |
||
242 | nextcolorState = 6; |
||
243 | break; |
||
244 | |||
245 | case 6: |
||
246 | ch2 = i; //fade in (ch2) green to get cyan (blue + green) |
||
247 | nextcolorState = 8; |
||
248 | break; |
||
249 | |||
250 | case 8: |
||
251 | ch1 = 255 - i; //fade out (ch1) blue to get pure green |
||
252 | nextcolorState = 10; |
||
253 | break; |
||
254 | |||
255 | case 10: |
||
256 | ch0 = i; //fade in (ch0) red to get yellow (green + red) |
||
257 | nextcolorState = 12; |
||
258 | break; |
||
259 | |||
260 | case 12: |
||
261 | ch2 = 255 - i; //fade out (ch2) green to get pure red |
||
262 | nextcolorState = 0; |
||
263 | break; |
||
264 | } |
||
265 | |||
266 | |||
267 | timer = SetDelay(1); |
||
268 | while (!CheckDelay(timer)); |
||
269 | } |
||
270 | |||
271 | colorState = nextcolorState; |
||
272 | timer = SetDelay(3); |
||
273 | while (!CheckDelay(timer)); |
||
274 | |||
275 | |||
28 | walter | 276 | } |
277 | |||
278 | |||
279 | } |