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