X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=heater.git;a=blobdiff_plain;f=firmware%2Fmain.c;fp=firmware%2Fmain.c;h=ec582327a3d450d78aa6ae54058354a8862c3e91;hp=4ac8171d16ac03f482409081a76847b7c5659753;hb=164f72d29f27faca0a4b2683f73e96a53edeedee;hpb=444a06ece7f2f468445e188b5f9801850564a19d diff --git a/firmware/main.c b/firmware/main.c index 4ac8171..ec58232 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -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);