From: Jan "Yenya" Kasprzak Date: Fri, 8 Jun 2012 22:45:42 +0000 (+0200) Subject: pattern flashing X-Git-Tag: gedasymbols-20120913~63 X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=44655bfd75c9213673c827661a6471b3fb38032f;p=bike-lights.git pattern flashing --- diff --git a/lights.c b/lights.c index aa018d9..6d78e23 100644 --- a/lights.c +++ b/lights.c @@ -9,9 +9,9 @@ volatile uint16_t adcval; volatile struct { - uint8_t tmr_int: 1; + uint8_t pwm_int: 1; uint8_t adc_int: 1; - uint8_t rx_int: 1; + uint8_t tmr_int: 1; } intflags; @@ -28,30 +28,65 @@ ISR(ADC_vect) ISR(TIMER1_OVF_vect) { TIMSK &= ~_BV(TOIE1); - intflags.tmr_int = 1; + intflags.pwm_int = 1; } -int main(void) +ISR(TIMER0_COMPA_vect) { - char seen = 0; + intflags.tmr_int = 1; +} +struct { + unsigned char intensity :2; + unsigned char length :6; +} pattern[] = { {1, 2}, {0, 4}, { 1, 2 }, {0, 15}, {0, 0} }; + +static void inline init_pwm() +{ TCCR1C = _BV(COM1D0) | _BV(COM1D1) | _BV(PWM1D); TCCR1A = _BV(COM1A0) | _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B); - TCCR1B = 0x80| _BV(CS13) | _BV(CS10); + TCCR1B = 0x80| _BV(CS13) | _BV(CS11); TC1H = 0x03; OCR1C = 0xFF; OCR1D = OCR1B = OCR1A = 0x40; TCNT1 = 0; DDRB |= _BV( PB5 ) | _BV( PB1 ) | _BV( PB3 ); PORTB &= ~(_BV( PB5 ) | _BV( PB1 ) | _BV( PB3 )); - TIMSK = _BV(TOIE1); + TIMSK |= _BV(TOIE1); +} +static void inline init_adc() +{ ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADPS1) | _BV(ADPS0); ADMUX = _BV(REFS1) | _BV(MUX0); // ADCSRB = _BV(REFS2); DIDR0 = _BV(ADC1D) | _BV(AREFD); +} + +static void inline init_tmr() +{ + TCCR0A = _BV(WGM00); + TCCR0B = _BV(CS02); // | _BV(CS00); + OCR0A = 0x80; + TIMSK |= _BV(OCIE0A); + DDRA |= _BV( PA0 ); +} + +int main(void) +{ + char seen = 0; + char pcount, ppos; + + init_pwm(); + init_adc(); + init_tmr(); + + ppos = 0; + pcount = pattern[ppos].length; + if (pattern[ppos].intensity) { + PORTA |= _BV( PA0 ); + } - sei(); while (1) { unsigned char pwmhi, pwmlo; @@ -77,12 +112,31 @@ int main(void) TIMSK |= _BV(TOIE1); } + if (intflags.pwm_int) { + intflags.pwm_int = 0; + ADCSRA |= _BV(ADIE) | _BV(ADSC); + } + if (intflags.tmr_int) { intflags.tmr_int = 0; - ADCSRA |= _BV(ADIE) | _BV(ADSC); + if (--pcount == 0) { + ppos++; + pcount = pattern[ppos].length; + if (!pcount) { + ppos = 0; + pcount = pattern[ppos].length; + } + if (pattern[ppos].intensity) { + PORTA |= _BV(PA0); + } else { + PORTA &= ~_BV(PA0); + } + } } + sei(); sleep_mode(); + cli(); #if 0 if (!seen) {