From: Jan "Yenya" Kasprzak Date: Thu, 30 Jan 2014 23:25:09 +0000 (+0100) Subject: battery measurements (off, on) X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=4c4fe8554a4c770d68dba4b45df716c4d0308a2f;p=heater.git battery measurements (off, on) --- diff --git a/firmware/main.c b/firmware/main.c index afaa557..fcb7426 100644 --- a/firmware/main.c +++ b/firmware/main.c @@ -17,11 +17,28 @@ static void timer_init() DDRB |= _BV(PB4); - TCCR1 = _BV(CS10); // clk/1 = 1 MHz - // TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz + // TCCR1 = _BV(CS10); // clk/1 = 1 MHz + TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz GTCCR = _BV(COM1B1) | _BV(PWM1B); OCR1C = 255; - OCR1B = 0; + OCR1B = steps[0]; + TIMSK = _BV(OCIE1B) | _BV(TOIE1); +} + +volatile unsigned char adc_type, adc_drop; + +ISR(TIM1_OVF_vect) +{ + adc_drop = 2; + adc_type = 1; + ADCSRA |= _BV(ADSC); +} + +ISR(TIM1_COMPB_vect) +{ + adc_drop = 2; + adc_type = 0; + ADCSRA |= _BV(ADSC); } static void set_pwm(unsigned char pwm) @@ -33,11 +50,38 @@ static void adc_init() { power_adc_enable(); - ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0); // clk/8 = 125 kHz + ADCSRA = _BV(ADEN) | _BV(ADPS1) | _BV(ADPS0) | _BV(ADIE); // clk/8 = 125 kHz ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); // 1.1V ref., PB3 single-ended DIDR0 = _BV(ADC3D); } +volatile uint16_t batt_on, batt_off; + +ISR(ADC_vect) +{ + uint16_t adcw = ADCW; + + if (adc_drop) { + adc_drop--; + ADCSRA |= _BV(ADSC); + return; + } + + if (adc_type == 0) { + if (batt_off) { + batt_off += adcw - (batt_off >> 5); + } else { + batt_off = adcw << 5; + } + } else { + if (batt_on) { + batt_on += adcw - (batt_on >> 5); + } else { + batt_on = adcw << 5; + } + } +} + static void status_led_init() { DDRB |= _BV(PB2); @@ -236,6 +280,11 @@ static void timer_blink() blink_on_time = 0; blink_off_time = 2; blink_counter = 10; +#if 0 + log_byte(0xbb); + log_byte(batt_on >> 7); + log_byte(batt_off >> 7); +#endif } } @@ -245,17 +294,6 @@ int main() power_down(); -#if 0 - ADCSRA |= _BV(ADSC); - while (!(ADCSRA & _BV(ADIF))) - ; - log_word(ADCW); - ADCSRA |= _BV(ADSC); - while (!(ADCSRA & _BV(ADIF))) - ; - log_word(ADCW); - log_flush(); -#endif sei(); // we try to be completely IRQ-driven, so just wait for IRQs here @@ -272,6 +310,7 @@ int main() wdt_timer_fired = 0; timer_check_buttons(); timer_blink(); + log_flush(); } } }