X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=bike-lights.git;a=blobdiff_plain;f=firmware%2Fadc.c;h=6db036af530b124e8aec9db50e668b6802d669a9;hp=9155f325ccfd2c857a93c503b2c2f6337a78ad04;hb=1f28156058ed3af9e87afdc60d631ca5a47eb2cb;hpb=cb10e19fb7311df4c3c14b64064c3d9a44fdacd1 diff --git a/firmware/adc.c b/firmware/adc.c index 9155f32..6db036a 100644 --- a/firmware/adc.c +++ b/firmware/adc.c @@ -1,21 +1,20 @@ #include #include -#include #include "lights.h" #define AMBIENT_ADC N_PWMLEDS #define BATTERY_ADC (N_PWMLEDS + 1) #define ADC1_GAIN20 (N_PWMLEDS + 2) +#define BUTTON_ADC (N_PWMLEDS + 2) -#define NUM_ADCS 6 +#define NUM_ADCS 7 volatile static unsigned char current_adc; static uint16_t adc_sum; static unsigned char sum_shift; static unsigned char adc_vals; #define ADC1_GAIN20_OFFSET_SHIFT 6 static uint16_t adc1_gain20_offset; -static unsigned char handler_running; static void inline setup_mux(unsigned char n) { @@ -45,6 +44,11 @@ static void inline setup_mux(unsigned char n) ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0); sum_shift = 0; // 1 measurement break; + case 6: // buttons: 1.1V, ADC3, single-ended + PORTA |= _BV(PA3); // +5V to the voltage splitter + ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0); + sum_shift = 0; + break; } adc_sum = 0; @@ -88,7 +92,6 @@ void init_adc() { unsigned char i; current_adc = NUM_ADCS; - handler_running = 0; ADCSRA = _BV(ADEN) // enable | _BV(ADPS1) | _BV(ADPS0) // CLK/8 = 125 kHz @@ -97,7 +100,7 @@ void init_adc() // ADCSRB |= _BV(GSEL); // gain 8 or 32 // Disable digital input on all bits used by ADC - DIDR0 = _BV(ADC0D) | _BV(ADC1D) | _BV(ADC2D) + DIDR0 = _BV(ADC0D) | _BV(ADC1D) | _BV(ADC2D) | _BV(ADC3D) | _BV(ADC4D) | _BV(ADC5D) | _BV(ADC6D); // 1.1V, ADC1,1, gain 20 @@ -157,28 +160,15 @@ ISR(ADC_vect) { // IRQ handler adc_sum = 0; } - if (handler_running & (1 << current_adc)) { - log_byte(0xB0 + current_adc); - - // drop the result, what else to do? - - start_next_adc(); - } else { - unsigned char current_adc_copy = current_adc; - uint16_t adc_sum_copy = adc_sum; - - start_next_adc(); - - handler_running |= (1 << current_adc_copy); - NONATOMIC_BLOCK(NONATOMIC_FORCEOFF) { - if (current_adc_copy < N_PWMLEDS) - pwmled_adc(current_adc_copy, adc_sum_copy); - if (current_adc_copy == AMBIENT_ADC) - ambient_adc(adc_sum_copy); - if (current_adc_copy == BATTERY_ADC) - battery_adc(adc_sum_copy); - } - handler_running &= ~(1 << current_adc_copy); - } + if (current_adc < N_PWMLEDS) + pwmled_adc(current_adc, adc_sum); + if (current_adc == AMBIENT_ADC) + ambient_adc(adc_sum); + if (current_adc == BATTERY_ADC) + battery_adc(adcval); + if (current_adc == BUTTON_ADC) + button_adc(adcval); + + start_next_adc(); }