X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=tinyboard.git;a=blobdiff_plain;f=projects%2Fstep-up%2Fadc.c;h=55cd8610ab441cacc7663ddc8e605dde49869d7c;hp=326588a1253d5dc8cbba468d918cf87432852e08;hb=HEAD;hpb=3e9d94b2511bc76aab92b2af38aacadf2dbe4dab diff --git a/projects/step-up/adc.c b/projects/step-up/adc.c index 326588a..55cd861 100644 --- a/projects/step-up/adc.c +++ b/projects/step-up/adc.c @@ -10,8 +10,9 @@ //#define NUM_ADCS ZERO_ADC #define NUM_ADCS 2 -volatile static unsigned char current_adc;; -static unsigned char need_battery_adc; +volatile static unsigned char current_adc; +volatile unsigned char adc_enabled; +volatile unsigned char need_battery_adc, need_pwmled_adc; static uint16_t adc_sum, read_zero, drop_count, read_count, n_reads_log; volatile uint16_t jiffies; @@ -40,13 +41,23 @@ void start_next_adc() drop_count = 1; read_count = 1; n_reads_log = 0; - } else { + } else if (need_pwmled_adc) { current_adc = 0; read_zero = 0; drop_count = 1; read_count = 1 << PWMLED_ADC_SHIFT; n_reads_log = PWMLED_ADC_SHIFT; + } else { + ADCSRA &= ~_BV(ADEN); + power_adc_disable(); + adc_enabled = 0; + return; + } + if (!adc_enabled) { + power_adc_enable(); + ADCSRA |= _BV(ADEN); + adc_enabled = 1; } adc_sum = 0; @@ -95,7 +106,9 @@ static uint16_t read_adc_sync() void init_adc() { need_battery_adc = 0; + need_pwmled_adc = 0; current_adc = 0; + adc_enabled = 1; power_adc_enable(); ACSR |= _BV(ACD); // but disable the analog comparator @@ -124,32 +137,13 @@ void susp_adc() { ADCSRA = 0; DIDR0 = 0; -} - -static void inline adc_based_timer() -{ - static unsigned char count; - - if (++count < 40) // about 100 Hz jiffies - return; - - count = 0; - ++jiffies; - - if ((jiffies & 0x007F) == 1) { // about every 1s - need_battery_adc = 1; - } - if ((jiffies & 0x0007) == 0) { - patterns_next_tick(); - } - timer_check_buttons(); + power_adc_disable(); + adc_enabled = 0; } ISR(ADC_vect) { // IRQ handler uint16_t adcval = ADCW; - adc_based_timer(); - if (read_zero) { setup_mux(current_adc); read_zero = 0;