From: Jan "Yenya" Kasprzak Date: Fri, 8 Jun 2012 18:36:20 +0000 (+0200) Subject: Drive all three PWMs X-Git-Tag: gedasymbols-20120913~65 X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=186eb129213ef9cb6d8ef1cea70d9bc7559d380d;p=bike-lights.git Drive all three PWMs --- diff --git a/lights.c b/lights.c index 653e8d4..aa018d9 100644 --- a/lights.c +++ b/lights.c @@ -5,51 +5,93 @@ #include uint16_t pwmee EEMEM; +volatile uint16_t adcval; + +volatile struct +{ + uint8_t tmr_int: 1; + uint8_t adc_int: 1; + uint8_t rx_int: 1; +} +intflags; + + +unsigned char debug EEMEM = 1; + +ISR(ADC_vect) +{ + adcval = ADCW; + ADCSRA &= ~_BV(ADIE); /* disable ADC interrupt */ + intflags.adc_int = 1; +} + +ISR(TIMER1_OVF_vect) +{ + TIMSK &= ~_BV(TOIE1); + intflags.tmr_int = 1; +} int main(void) { char seen = 0; TCCR1C = _BV(COM1D0) | _BV(COM1D1) | _BV(PWM1D); - TCCR1A = _BV(PWM1A) | _BV(PWM1B); - TCCR1B = 0x80| _BV(CS13) | _BV(CS11); + TCCR1A = _BV(COM1A0) | _BV(COM1A1) | _BV(COM1B0) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B); + TCCR1B = 0x80| _BV(CS13) | _BV(CS10); TC1H = 0x03; OCR1C = 0xFF; - OCR1D = 0x20; + OCR1D = OCR1B = OCR1A = 0x40; TCNT1 = 0; - DDRB |= _BV( PB5 ); - PORTB &= ~_BV( PB5 ); + DDRB |= _BV( PB5 ) | _BV( PB1 ) | _BV( PB3 ); + PORTB &= ~(_BV( PB5 ) | _BV( PB1 ) | _BV( PB3 )); + TIMSK = _BV(TOIE1); ADCSRA = _BV(ADEN) | _BV(ADATE) | _BV(ADPS1) | _BV(ADPS0); ADMUX = _BV(REFS1) | _BV(MUX0); // ADCSRB = _BV(REFS2); DIDR0 = _BV(ADC1D) | _BV(AREFD); + sei(); while (1) { - uint16_t pwm; unsigned char pwmhi, pwmlo; - ADCSRA |= _BV(ADSC); + if (intflags.adc_int) { + intflags.adc_int = 0; + + if (adcval > 0x3C0) + adcval = 0x3C0; + if (adcval < 1) + adcval = 1; + pwmhi = adcval >> 8; + pwmlo = adcval & 0xFF; + + TC1H = pwmhi; + OCR1D = pwmlo; + + TC1H = pwmhi; + OCR1B = pwmlo; + + TC1H = pwmhi; + OCR1A = pwmlo; + + TIMSK |= _BV(TOIE1); + } + + if (intflags.tmr_int) { + intflags.tmr_int = 0; + ADCSRA |= _BV(ADIE) | _BV(ADSC); + } - _delay_ms(100); + sleep_mode(); - pwm = ADCW; +#if 0 if (!seen) { seen = 1; - eeprom_write_word(&pwmee, pwm); + eeprom_write_byte(&debug, 2); } - if (pwm > 0x3C0) - pwm = 0x3C0; - if (pwm < 1) - pwm = 1; - pwmhi = pwm >> 8; - pwmlo = pwm & 0xFF; - - TC1H = pwmhi; - OCR1D = pwmlo; +#endif } - sleep_mode(); DDRA |= _BV( PA0 ); while( 1 ) { PORTA |= _BV( PA0 );