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=194b00e0b0de070d3ab0369b287560df8f93f37a;hb=HEAD;hpb=8b54d4bdf1305a636d6c1d03e6a725061f47c612 diff --git a/projects/step-up/adc.c b/projects/step-up/adc.c index 194b00e..55cd861 100644 --- a/projects/step-up/adc.c +++ b/projects/step-up/adc.c @@ -5,12 +5,14 @@ #include "lights.h" -#define ZERO_ADC 1 +#define ZERO_ADC 2 //#define NUM_ADCS ZERO_ADC -#define NUM_ADCS 1 +#define NUM_ADCS 2 -volatile static unsigned char current_adc, current_slow_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; @@ -21,6 +23,9 @@ static void setup_mux(unsigned char n) case 0: // pwmled 1: 1.1V, ADC3 (PB3), single-ended ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); break; + case 1: // battery voltage: 1.1V, ADC1 (PB2), single-ended + ADMUX = _BV(REFS1) | _BV(MUX0); + break; case ZERO_ADC: // zero: 1.1V, GND, single-ended ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0); break; @@ -29,37 +34,33 @@ static void setup_mux(unsigned char n) void start_next_adc() { -#if 0 - if (current_adc == 0) { - if (current_slow_adc > N_PWMLEDS) { - // read one of the non-PWMLED ADCs - current_adc = --current_slow_adc; - } else { - // no more non-PWMLEDs to do, start with PWMLEDs - current_adc = N_PWMLEDS-1; - } - } else if (current_adc >= N_PWMLEDS) { - // one of the non-PWMLED ADCs just finished, skip to PWMLEDs. - current_adc = N_PWMLEDS-1; + if (need_battery_adc) { + need_battery_adc = 0; + current_adc = 1; + read_zero = 1; + drop_count = 1; + read_count = 1; + n_reads_log = 0; + } 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 { - // next PWMLED - current_adc--; + ADCSRA &= ~_BV(ADEN); + power_adc_disable(); + adc_enabled = 0; + return; } -#else - // single ADC for testing only - current_adc = 0; -#endif -#if 0 - log_byte(0x90 + current_adc); // debug ADC switching -#endif + if (!adc_enabled) { + power_adc_enable(); + ADCSRA |= _BV(ADEN); + adc_enabled = 1; + } adc_sum = 0; - read_zero = 0; - drop_count = 1; - - read_count = 1 << PWMLED_ADC_SHIFT; - n_reads_log = PWMLED_ADC_SHIFT; // set up mux, start one-shot conversion if (read_zero) @@ -104,8 +105,10 @@ static uint16_t read_adc_sync() void init_adc() { - current_slow_adc = NUM_ADCS; + 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 @@ -130,42 +133,17 @@ void init_adc() start_next_adc(); } -#if 0 void susp_adc() { ADCSRA = 0; DIDR0 = 0; -} - -static void adc1_gain20_adc(uint16_t adcsum) -{ - // running average - adc1_gain20_offset += adcsum - - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT); -} -#endif - -static void inline adc_based_timer() -{ - static unsigned char count; - - if (++count < 40) // about 100 Hz jiffies - return; - - count = 0; - ++jiffies; - - 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; @@ -195,6 +173,9 @@ ISR(ADC_vect) { // IRQ handler // pwmled_adc(current_adc, adc_sum); pwmled_adc(adc_sum); break; + case 1: + battery_adc(adc_sum); + break; } start_next_adc();