Rev 53 | Rev 55 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 53 | Rev 54 | ||
---|---|---|---|
Line 57... | Line 57... | ||
57 | ppm = (ICR1 - pos_ICR + (int) TMR1OvF * 65536); |
57 | ppm = (ICR1 - pos_ICR + (int) TMR1OvF * 65536); |
58 | if ((ppm > 600) && (ppm < 2400)) |
58 | if ((ppm > 600) && (ppm < 2400)) |
59 | { |
59 | { |
60 | if (ppm > 2100) ppm = 2100; |
60 | if (ppm > 2100) ppm = 2100; |
61 | if (ppm < 900) ppm = 900; |
61 | if (ppm < 900) ppm = 900; |
62 | ppm = (ppm_signal * 7 + ppm) / 8; |
62 | ppm = (ppm_signal * 3 + ppm) / 4; |
63 | ppm_signal = ppm; |
63 | ppm_signal = ppm; |
64 | ppm_new = 1; |
64 | if (ppm_new < 50) ppm_new++; |
65 | } |
65 | } |
66 | 66 | ||
67 | } |
67 | } |
68 | 68 | ||
69 | } |
69 | } |
Line 107... | Line 107... | ||
107 | if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D; |
107 | if (ch5_tmp == counter) PORTD_BAK LEDOFF CH5_D; |
108 | 108 | ||
109 | PORTB = PORTB_BAK; |
109 | PORTB = PORTB_BAK; |
110 | PORTD = PORTD_BAK; |
110 | PORTD = PORTD_BAK; |
111 | 111 | ||
- | 112 | ||
112 | if (ms1++ == 32) |
113 | if (ms1++ == 32) |
113 | { |
114 | { |
114 | ms1=0; |
115 | ms1=0; |
115 | TMR1MS++; |
116 | TMR1MS++; |
116 | } |
117 | } |
Line 134... | Line 135... | ||
134 | return(((temp_hi << 8) | temp_lo) + t + 1); |
135 | return(((temp_hi << 8) | temp_lo) + t + 1); |
135 | 136 | ||
136 | } |
137 | } |
137 | 138 | ||
138 | 139 | ||
- | 140 | ||
139 | char CheckDelay(unsigned int t) |
141 | char CheckDelay(unsigned int t) |
140 | { |
142 | { |
141 | unsigned char temp_hi; |
143 | unsigned char temp_hi; |
142 | unsigned char temp_lo; |
144 | unsigned char temp_lo; |
143 | 145 | ||
Line 179... | Line 181... | ||
179 | } |
181 | } |
180 | 182 | ||
181 | 183 | ||
182 | 184 | ||
183 | /*##############################################################################*/ |
185 | /*##############################################################################*/ |
184 | int GetPPM(void) |
186 | unsigned int GetPPM(void) |
185 | { |
187 | { |
186 | //this routines seems to be nesseccary, as reading a 16 bit value |
188 | //this routines seems to be nesseccary, as reading a 16 bit value |
187 | //on a 8 bit machine is not atomic, so if an interrupt apears between reading |
189 | //on a 8 bit machine is not atomic, so if an interrupt apears between reading |
188 | //low and high byte of the 16 bit value a wrong result is possible |
190 | //low and high byte of the 16 bit value a wrong result is possible |
189 | 191 | ||
190 | unsigned char intmask; |
192 | unsigned char temp_hi; |
191 | unsigned int ppm_temp; |
193 | unsigned char temp_lo; |
192 | 194 | ||
193 | intmask = TIMSK; //backup interupt enable bits |
- | |
194 | TIMSK &= ~(1<<TICIE1); //disable ppm interrupt |
195 | temp_hi = (ppm_signal >> 8); |
195 | ppm_temp = ppm_signal; |
196 | temp_lo = (ppm_signal & 0xff); |
196 | TIMSK = intmask; //restore interupt enable bits |
197 | if (temp_hi != (ppm_signal >> 8)) temp_hi = (ppm_signal >> 8); |
197 | return(ppm_temp); //return ppm_signal |
198 | return( (temp_hi << 8) | temp_lo); |
198 | 199 | ||
199 | } |
200 | } |
200 | 201 | ||
201 | 202 | ||
202 | /*##############################################################################*/ |
203 | /*##############################################################################*/ |
203 | // MAIN |
204 | // MAIN |
204 | /*##############################################################################*/ |
205 | /*##############################################################################*/ |
205 | int main (void) |
206 | int main (void) |
206 | { |
207 | { |
- | 208 | ||
- | 209 | #define step 128 |
|
207 | unsigned int i; |
210 | #define mul 2 |
- | 211 | ||
- | 212 | #define sigeepadr 0x00 |
|
- | 213 | #define mineepadr 0x20 |
|
- | 214 | #define maxeepadr 0x22 |
|
- | 215 | #define eepsig 0x55aa |
|
- | 216 | ||
- | 217 | ||
- | 218 | ||
208 | unsigned int ppm; |
219 | unsigned int ppm; |
- | 220 | unsigned int setupdly; |
|
209 | unsigned char colorState; |
221 | unsigned int ppmtodly; |
- | 222 | unsigned int lmax; |
|
- | 223 | unsigned int lmin; |
|
- | 224 | unsigned int max; |
|
- | 225 | unsigned int min; |
|
- | 226 | unsigned int sig; |
|
210 | unsigned char nextcolorState=0; |
227 | unsigned char setup; |
- | 228 | ||
211 | 229 | ||
212 | DDRB = (CH0_B|CH1_B|CH2_B); |
230 | DDRB = (CH0_B|CH1_B|CH2_B); |
213 | PORTB = 0x00; |
231 | PORTB = 0x00; |
214 | 232 | ||
215 | DDRC = (ledred); |
233 | DDRC = (ledred); |
Line 223... | Line 241... | ||
223 | ch2 = 0; |
241 | ch2 = 0; |
224 | ch3 = 0; |
242 | ch3 = 0; |
225 | ch4 = 0; |
243 | ch4 = 0; |
226 | ch5 = 0; |
244 | ch5 = 0; |
227 | 245 | ||
- | 246 | lmax = 0x0000; |
|
- | 247 | lmin = 0xffff; |
|
- | 248 | ||
228 | //StartUART(); |
249 | StartUART(); |
229 | StartPPM(); |
250 | StartPPM(); |
230 | //StartI2C(); |
251 | //StartI2C(); |
231 | StartPWM(); |
252 | StartPWM(); |
232 | sei(); |
253 | sei(); |
233 | 254 | ||
234 | 255 | ||
235 | // Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb > |
256 | min = 1100; |
- | 257 | max = 1900; |
|
236 | 258 | ||
- | 259 | if (eeprom_read_word(sigeepadr) != eepsig) |
|
- | 260 | { |
|
- | 261 | eeprom_write_word(mineepadr, min); |
|
- | 262 | eeprom_write_word(maxeepadr, max); |
|
- | 263 | eeprom_write_word(sigeepadr, eepsig); |
|
- | 264 | } |
|
- | 265 | else |
|
- | 266 | { |
|
- | 267 | min = eeprom_read_word(mineepadr); |
|
- | 268 | max = eeprom_read_word(maxeepadr); |
|
- | 269 | } |
|
- | 270 | ||
- | 271 | printf("ppm: %d / min: %d / max: %d\n",ppm,min,max); |
|
237 | 272 | ||
- | 273 | ||
238 | colorState = 0; |
274 | setup = 0; |
- | 275 | setupdly = SetDelay(3000); |
|
- | 276 | ppmtodly = SetDelay(5000); |
|
239 | 277 | ||
240 | while (1) |
278 | while (1) |
241 | { |
279 | { |
242 | 280 | ||
243 | /* |
- | |
244 | #define step 128 |
- | |
245 | #define mul 2 |
- | |
246 | - | ||
247 | if ((ppm >= (step * 0) && (ppm < (step * 1))) ch0 = mul * ((ppm - (step * 0))); |
- | |
248 | if ((ppm >= (step * 1)) && (ppm < (step * 2))) ch1 = mul * ((ppm - (step * 1))); |
- | |
249 | if ((ppm >= (step * 2)) && (ppm < (step * 3))) ch0 = mul * ((step - 1) - (ppm - (step * 2))); |
- | |
250 | if ((ppm >= (step * 3)) && (ppm < (step * 4))) ch2 = mul * ((ppm - (step * 3))); |
- | |
251 | if ((ppm >= (step * 4)) && (ppm < (step * 5))) ch1 = mul * ((step - 1) - (ppm - (step * 4))); |
- | |
252 | if ((ppm >= (step * 5)) && (ppm < (step * 6))) ch0 = mul * ((ppm - (step * 5))); |
- | |
253 | if ((ppm >= (step * 6)) && (ppm < (step * 7))) ch2 = mul * ((step - 1) - (ppm - (step * 6))); |
- | |
254 | - | ||
255 | //printf("%d ",ppm); |
- | |
256 | */ |
- | |
257 | - | ||
258 | for (i=0; i<=255; i++) |
281 | if (ppm_new > 20) |
259 | { |
282 | { |
260 | switch(colorState) |
283 | PORTC clrbit ledred; |
261 | { |
- | |
262 | case 0: |
284 | ppm = GetPPM(); |
263 | ch0 = i; //fade in (ch0) red |
285 | ppmtodly = SetDelay(500); |
264 | nextcolorState = 2; |
286 | if (lmax < ppm) lmax=ppm; |
265 | break; |
- | |
266 | - | ||
267 | case 2: |
- | |
268 | ch1 = i; //fade in (ch1) blue to get pure purple (red + blue) |
- | |
269 | nextcolorState = 4; |
287 | if (lmin > ppm) lmin=ppm; |
270 | break; |
- | |
271 | 288 | } |
|
272 | case 4: |
289 | else |
273 | ch0 = 255 - i; //fade out (ch0) red to get pure blue |
- | |
274 | nextcolorState = 6; |
- | |
275 | break; |
- | |
276 | 290 | { |
|
277 | case 6: |
- | |
278 | ch2 = i; //fade in (ch2) green to get pure cyan (blue + green) |
- | |
279 | nextcolorState = 8; |
291 | PORTC setbit ledred; |
280 | break; |
292 | ppm = min; |
281 | 293 | } |
|
282 | case 8: |
- | |
283 | ch1 = 255 - i; //fade out (ch1) blue to get pure green |
- | |
284 | nextcolorState = 10; |
- | |
285 | break; |
- | |
286 | 294 | ||
287 | case 10: |
- | |
288 | ch0 = i; //fade in (ch0) red to get yellow pure (green + red) |
- | |
289 | nextcolorState = 12; |
295 | if (CheckDelay(ppmtodly)) |
290 | break; |
- | |
291 | 296 | { |
|
292 | case 12: |
- | |
293 | ch2 = 255 - i; //fade out (ch2) green to get pure red |
- | |
294 | nextcolorState = 0; |
297 | ppmtodly = SetDelay(5000); |
295 | break; |
298 | ppm_new = 0; |
296 | } |
299 | } |
297 | 300 | ||
- | 301 | if ((ppm > 1600) && ((setup&1)==0)) setup++; |
|
- | 302 | if ((ppm < 1400) && ((setup&1)==1)) setup++; |
|
- | 303 | if (setup == 6) |
|
- | 304 | { |
|
- | 305 | PORTD setbit ledgreen; |
|
- | 306 | eeprom_write_word(mineepadr, lmin); |
|
- | 307 | eeprom_write_word(maxeepadr, lmax); |
|
- | 308 | min = lmin; |
|
- | 309 | max = lmax; |
|
- | 310 | setupdly = SetDelay(2000); |
|
- | 311 | } |
|
- | 312 | if (CheckDelay(setupdly)) |
|
- | 313 | { |
|
- | 314 | setup = 0; |
|
- | 315 | PORTD clrbit ledgreen; |
|
- | 316 | } |
|
298 | 317 | ||
- | 318 | //printf("ppm: %d / min: %d / max: %d\n",ppm,lmin,lmax); |
|
- | 319 | ||
- | 320 | ||
- | 321 | /* |
|
- | 322 | // Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb > |
|
- | 323 | if ((ppm >= (step * 0)) && (ppm < (step * 1))) |
|
- | 324 | { |
|
- | 325 | ch0 = mul * ((ppm - (step * 0))); //fade in red > red (red only) |
|
- | 326 | ch1 = 0; |
|
- | 327 | ch2 = 0; |
|
- | 328 | } |
|
- | 329 | if ((ppm >= (step * 1)) && (ppm < (step * 2))) |
|
- | 330 | { |
|
299 | timer = SetDelay(5); //wait 10ms |
331 | ch0 = ((step-1) * mul); |
- | 332 | ch2 = mul * ((ppm - (step * 1))); //fade in blue > purple (red + blue) |
|
- | 333 | ch1 = 0; |
|
- | 334 | } |
|
- | 335 | if ((ppm >= (step * 2)) && (ppm < (step * 3))) |
|
- | 336 | { |
|
- | 337 | ch0 = mul * ((step - 1) - (ppm - (step * 2))); //fade out red > blue (blue only) |
|
300 | while (!CheckDelay(timer)); |
338 | ch2 = ((step-1) * mul); |
- | 339 | ch1 = 0; |
|
301 | } |
340 | } |
- | 341 | if ((ppm >= (step * 3)) && (ppm < (step * 4))) |
|
- | 342 | { |
|
- | 343 | ch0 = 0; |
|
- | 344 | ch2 = ((step-1) * mul); |
|
- | 345 | ch1 = mul * ((ppm - (step * 3))); //fade in green > tuerkis (blue + green) |
|
- | 346 | } |
|
- | 347 | if ((ppm >= (step * 4)) && (ppm < (step * 5))) |
|
- | 348 | { |
|
- | 349 | ch0 = 0; |
|
- | 350 | ch2 = mul * ((step - 1) - (ppm - (step * 4))); //fade out blue > green (green only) |
|
- | 351 | ch1 = ((step-1) * mul); |
|
- | 352 | } |
|
- | 353 | if ((ppm >= (step * 5)) && (ppm < (step * 6))) |
|
- | 354 | { |
|
- | 355 | ch0 = mul * ((ppm - (step * 5))); //fade in red > yellow (green + red) |
|
- | 356 | ch2 = 0; |
|
- | 357 | ch1 = ((step-1) * mul); |
|
- | 358 | } |
|
- | 359 | if ((ppm >= (step * 6)) && (ppm < (step * 7))) |
|
- | 360 | { |
|
- | 361 | ch0 = ((step-1) * mul); |
|
- | 362 | ch2 = 0; |
|
- | 363 | ch1 = mul * ((step - 1) - (ppm - (step * 6))); //fade out green > red (red only) |
|
- | 364 | } |
|
- | 365 | if ((ppm >= (step * 7)) ) |
|
- | 366 | { |
|
- | 367 | ch0 = ((step-1) * mul); |
|
- | 368 | ch2 = 0; |
|
- | 369 | ch1 = 9; |
|
- | 370 | } |
|
- | 371 | */ |
|
- | 372 | ||
- | 373 | ||
302 | 374 | ||
303 | colorState = nextcolorState; |
- | |
304 | 375 | ||
305 | timer = SetDelay(3); //hold pure colors for additional 3ms |
- | |
306 | while (!CheckDelay(timer)); |
- | |
307 | 376 | ||
308 | 377 | ||
309 | } |
378 | } |
310 | 379 | ||
311 | 380 |