]> www.fi.muni.cz Git - heater.git/commitdiff
firmware: faster T/C1 clock
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 3 Feb 2014 17:26:15 +0000 (18:26 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Mon, 3 Feb 2014 17:26:15 +0000 (18:26 +0100)
We should have T/C1 as fast as possible (but slow enough to allow for
ADC measurements both in on and off states). Faster T/C1 allows lower
voltage ripple of Vcc, and less LED flicker.

firmware/main.c

index 4ac8171d16ac03f482409081a76847b7c5659753..ec582327a3d450d78aa6ae54058354a8862c3e91 100644 (file)
@@ -84,6 +84,7 @@ static unsigned char batt_levels[] = {
 /* output power and PWM calculation */
 #define PWM_TOP        255
 #define PWM_MAX        (PWM_TOP - 8)   // to allow for ADC "batt_off" measurements
+#define PWM_MIN        8               // to allow for ADC "batt_on" measurements
 
 /*
  * The values in power_levels[] array are voltages at which the load
@@ -182,7 +183,15 @@ static void pwm_init()
        PORTB &= ~_BV(PB4);
 
        // TCCR1 = _BV(CS10); // clk/1 = 1 MHz
-       TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
+       // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
+       /*
+        * clk/64 = 16 kHz. We use PWM_MIN and PWM_MAX, so we have at least
+        * 8 full T/C1 cycles to do two ADC measurements. The ADC with 125 kHz
+        * clock can do about 7000-9000 measurement per second, so we should
+        * be safe both on low and high OCR1B values with this clock
+        */
+       TCCR1 = _BV(CS12) | _BV(CS11) | _BV(CS10);
+
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
        OCR1C = PWM_TOP;
        // OCR1B = steps[0];
@@ -200,14 +209,14 @@ static void pwm_susp()
 
 ISR(TIM1_OVF_vect)
 {
-       adc_drop = 2;
+       adc_drop = 1;
        adc_type = 1;
        adc_start_measurement();
 }
 
 ISR(TIM1_COMPB_vect)
 {
-       adc_drop = 2;
+       adc_drop = 1;
        adc_type = 0;
        adc_start_measurement();
 }
@@ -495,6 +504,9 @@ static void calculate_power_level()
        if (pwm > PWM_MAX)
                pwm = PWM_MAX;
 
+       if (pwm < PWM_MIN)
+               pwm = PWM_MIN;
+
        log_byte(0x10 + power_level);
        log_byte(batt_on8);
        log_byte(pwm & 0xFF);