59,9 → 59,9 |
{ |
if (ppm > 2100) ppm = 2100; |
if (ppm < 900) ppm = 900; |
ppm = (ppm_signal * 7 + ppm) / 8; |
ppm = (ppm_signal * 3 + ppm) / 4; |
ppm_signal = ppm; |
ppm_new = 1; |
if (ppm_new < 50) ppm_new++; |
} |
|
} |
109,6 → 109,7 |
PORTB = PORTB_BAK; |
PORTD = PORTD_BAK; |
|
|
if (ms1++ == 32) |
{ |
ms1=0; |
136,6 → 137,7 |
} |
|
|
|
char CheckDelay(unsigned int t) |
{ |
unsigned char temp_hi; |
181,20 → 183,19 |
|
|
/*##############################################################################*/ |
int GetPPM(void) |
unsigned int GetPPM(void) |
{ |
//this routines seems to be nesseccary, as reading a 16 bit value |
//on a 8 bit machine is not atomic, so if an interrupt apears between reading |
//low and high byte of the 16 bit value a wrong result is possible |
|
unsigned char intmask; |
unsigned int ppm_temp; |
unsigned char temp_hi; |
unsigned char temp_lo; |
|
intmask = TIMSK; //backup interupt enable bits |
TIMSK &= ~(1<<TICIE1); //disable ppm interrupt |
ppm_temp = ppm_signal; |
TIMSK = intmask; //restore interupt enable bits |
return(ppm_temp); //return ppm_signal |
temp_hi = (ppm_signal >> 8); |
temp_lo = (ppm_signal & 0xff); |
if (temp_hi != (ppm_signal >> 8)) temp_hi = (ppm_signal >> 8); |
return( (temp_hi << 8) | temp_lo); |
|
} |
|
204,11 → 205,28 |
/*##############################################################################*/ |
int main (void) |
{ |
unsigned int i; |
|
#define step 128 |
#define mul 2 |
|
#define sigeepadr 0x00 |
#define mineepadr 0x20 |
#define maxeepadr 0x22 |
#define eepsig 0x55aa |
|
|
|
unsigned int ppm; |
unsigned char colorState; |
unsigned char nextcolorState=0; |
unsigned int setupdly; |
unsigned int ppmtodly; |
unsigned int lmax; |
unsigned int lmin; |
unsigned int max; |
unsigned int min; |
unsigned int sig; |
unsigned char setup; |
|
|
DDRB = (CH0_B|CH1_B|CH2_B); |
PORTB = 0x00; |
|
225,7 → 243,10 |
ch4 = 0; |
ch5 = 0; |
|
//StartUART(); |
lmax = 0x0000; |
lmin = 0xffff; |
|
StartUART(); |
StartPPM(); |
//StartI2C(); |
StartPWM(); |
232,80 → 253,128 |
sei(); |
|
|
// Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb > |
min = 1100; |
max = 1900; |
|
if (eeprom_read_word(sigeepadr) != eepsig) |
{ |
eeprom_write_word(mineepadr, min); |
eeprom_write_word(maxeepadr, max); |
eeprom_write_word(sigeepadr, eepsig); |
} |
else |
{ |
min = eeprom_read_word(mineepadr); |
max = eeprom_read_word(maxeepadr); |
} |
|
colorState = 0; |
printf("ppm: %d / min: %d / max: %d\n",ppm,min,max); |
|
|
setup = 0; |
setupdly = SetDelay(3000); |
ppmtodly = SetDelay(5000); |
|
while (1) |
{ |
|
/* |
#define step 128 |
#define mul 2 |
if (ppm_new > 20) |
{ |
PORTC clrbit ledred; |
ppm = GetPPM(); |
ppmtodly = SetDelay(500); |
if (lmax < ppm) lmax=ppm; |
if (lmin > ppm) lmin=ppm; |
} |
else |
{ |
PORTC setbit ledred; |
ppm = min; |
} |
|
if (CheckDelay(ppmtodly)) |
{ |
ppmtodly = SetDelay(5000); |
ppm_new = 0; |
} |
|
if ((ppm >= (step * 0) && (ppm < (step * 1))) ch0 = mul * ((ppm - (step * 0))); |
if ((ppm >= (step * 1)) && (ppm < (step * 2))) ch1 = mul * ((ppm - (step * 1))); |
if ((ppm >= (step * 2)) && (ppm < (step * 3))) ch0 = mul * ((step - 1) - (ppm - (step * 2))); |
if ((ppm >= (step * 3)) && (ppm < (step * 4))) ch2 = mul * ((ppm - (step * 3))); |
if ((ppm >= (step * 4)) && (ppm < (step * 5))) ch1 = mul * ((step - 1) - (ppm - (step * 4))); |
if ((ppm >= (step * 5)) && (ppm < (step * 6))) ch0 = mul * ((ppm - (step * 5))); |
if ((ppm >= (step * 6)) && (ppm < (step * 7))) ch2 = mul * ((step - 1) - (ppm - (step * 6))); |
if ((ppm > 1600) && ((setup&1)==0)) setup++; |
if ((ppm < 1400) && ((setup&1)==1)) setup++; |
if (setup == 6) |
{ |
PORTD setbit ledgreen; |
eeprom_write_word(mineepadr, lmin); |
eeprom_write_word(maxeepadr, lmax); |
min = lmin; |
max = lmax; |
setupdly = SetDelay(2000); |
} |
if (CheckDelay(setupdly)) |
{ |
setup = 0; |
PORTD clrbit ledgreen; |
} |
|
//printf("%d ",ppm); |
*/ |
|
for (i=0; i<=255; i++) |
//printf("ppm: %d / min: %d / max: %d\n",ppm,lmin,lmax); |
|
|
/* |
// Farbablauf: rot > Violett > blau > tuerkis > gruen > gelb > |
if ((ppm >= (step * 0)) && (ppm < (step * 1))) |
{ |
switch(colorState) |
{ |
case 0: |
ch0 = i; //fade in (ch0) red |
nextcolorState = 2; |
break; |
ch0 = mul * ((ppm - (step * 0))); //fade in red > red (red only) |
ch1 = 0; |
ch2 = 0; |
} |
if ((ppm >= (step * 1)) && (ppm < (step * 2))) |
{ |
ch0 = ((step-1) * mul); |
ch2 = mul * ((ppm - (step * 1))); //fade in blue > purple (red + blue) |
ch1 = 0; |
} |
if ((ppm >= (step * 2)) && (ppm < (step * 3))) |
{ |
ch0 = mul * ((step - 1) - (ppm - (step * 2))); //fade out red > blue (blue only) |
ch2 = ((step-1) * mul); |
ch1 = 0; |
} |
if ((ppm >= (step * 3)) && (ppm < (step * 4))) |
{ |
ch0 = 0; |
ch2 = ((step-1) * mul); |
ch1 = mul * ((ppm - (step * 3))); //fade in green > tuerkis (blue + green) |
} |
if ((ppm >= (step * 4)) && (ppm < (step * 5))) |
{ |
ch0 = 0; |
ch2 = mul * ((step - 1) - (ppm - (step * 4))); //fade out blue > green (green only) |
ch1 = ((step-1) * mul); |
} |
if ((ppm >= (step * 5)) && (ppm < (step * 6))) |
{ |
ch0 = mul * ((ppm - (step * 5))); //fade in red > yellow (green + red) |
ch2 = 0; |
ch1 = ((step-1) * mul); |
} |
if ((ppm >= (step * 6)) && (ppm < (step * 7))) |
{ |
ch0 = ((step-1) * mul); |
ch2 = 0; |
ch1 = mul * ((step - 1) - (ppm - (step * 6))); //fade out green > red (red only) |
} |
if ((ppm >= (step * 7)) ) |
{ |
ch0 = ((step-1) * mul); |
ch2 = 0; |
ch1 = 9; |
} |
*/ |
|
case 2: |
ch1 = i; //fade in (ch1) blue to get pure purple (red + blue) |
nextcolorState = 4; |
break; |
|
|
|
case 4: |
ch0 = 255 - i; //fade out (ch0) red to get pure blue |
nextcolorState = 6; |
break; |
|
case 6: |
ch2 = i; //fade in (ch2) green to get pure cyan (blue + green) |
nextcolorState = 8; |
break; |
|
case 8: |
ch1 = 255 - i; //fade out (ch1) blue to get pure green |
nextcolorState = 10; |
break; |
|
case 10: |
ch0 = i; //fade in (ch0) red to get yellow pure (green + red) |
nextcolorState = 12; |
break; |
|
case 12: |
ch2 = 255 - i; //fade out (ch2) green to get pure red |
nextcolorState = 0; |
break; |
} |
|
|
timer = SetDelay(5); //wait 10ms |
while (!CheckDelay(timer)); |
} |
|
colorState = nextcolorState; |
|
timer = SetDelay(3); //hold pure colors for additional 3ms |
while (!CheckDelay(timer)); |
|
|
} |
|
|