]> www.fi.muni.cz Git - bike-lights.git/commitdiff
Drive all three PWMs
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 8 Jun 2012 18:36:20 +0000 (20:36 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Fri, 8 Jun 2012 18:36:20 +0000 (20:36 +0200)
lights.c

index 653e8d4f7e7b3dcd1e550120f637652b7d82f2e4..aa018d977d61d8156b43e3f64e29a837d388b8b7 100644 (file)
--- a/lights.c
+++ b/lights.c
@@ -5,51 +5,93 @@
 #include <avr/interrupt.h>
 
 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 );