2 #include <avr/interrupt.h>
7 #define BATTERY_ADC (N_PWMLEDS + 0)
8 #define BUTTON_ADC (N_PWMLEDS + 1)
9 #define ZERO_ADC (N_PWMLEDS + 2)
11 //#define NUM_ADCS ZERO_ADC
15 unsigned char read_zero_log : 2;
16 unsigned char read_drop_log : 2;
17 unsigned char read_keep_log : 4;
18 } adc_params[NUM_ADCS] = {
19 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 1
21 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 2
22 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 3
23 { 0, 1, AMBIENT_ADC_SHIFT }, // ambient
24 { 0, 1, 0 }, // battery
25 { 0, 1, 0 }, // gain20
26 { 0, 1, 0 }, // buttons
30 volatile static unsigned char current_adc, current_slow_adc;
31 static uint16_t adc_sum, zero_count, drop_count, read_count, n_reads_log;
34 static void setup_mux(unsigned char n)
36 /* ADC numbering: PWM LEDs first, then others, zero at the end */
38 case 0: // pwmled 1: 1.1V, ADC3 (PB3), single-ended
39 ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
41 case ZERO_ADC: // zero: 1.1V, GND, single-ended
42 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
50 if (current_adc == 0) {
51 if (current_slow_adc > N_PWMLEDS) {
52 // read one of the non-PWMLED ADCs
53 current_adc = --current_slow_adc;
55 // no more non-PWMLEDs to do, start with PWMLEDs
56 current_adc = N_PWMLEDS-1;
58 } else if (current_adc >= N_PWMLEDS) {
59 // one of the non-PWMLED ADCs just finished, skip to PWMLEDs.
60 current_adc = N_PWMLEDS-1;
66 // single ADC for testing only
71 log_byte(0x90 + current_adc); // debug ADC switching
75 // we use the last iteration of zero_count to set up the MUX
76 // to its final destination, hence the "1 +" below:
77 if (adc_params[current_adc].read_zero_log)
78 zero_count = 1 + (1 << (adc_params[current_adc].read_zero_log-1));
82 if (adc_params[current_adc].read_drop_log)
83 drop_count = 1 << (adc_params[current_adc].read_drop_log - 1);
87 read_count = 1 << adc_params[current_adc].read_keep_log;
88 n_reads_log = adc_params[current_adc].read_keep_log;
90 // set up mux, start one-shot conversion
94 setup_mux(current_adc);
99 void timer_start_slow_adcs()
101 if (current_slow_adc > N_PWMLEDS) { // Don't start if in progress
102 log_byte(0x80 + current_slow_adc);
104 current_slow_adc = NUM_ADCS;
105 // TODO: kick the watchdog here
110 * Single synchronous ADC conversion.
111 * Has to be called with IRQs disabled (or with the ADC IRQ disabled).
113 static uint16_t read_adc_sync()
117 ADCSRA |= _BV(ADSC); // start the conversion
119 // wait for the conversion to finish
120 while((ADCSRA & _BV(ADIF)) == 0)
124 ADCSRA |= _BV(ADIF); // clear the IRQ flag
132 current_slow_adc = NUM_ADCS;
135 ADCSRA = _BV(ADEN) // enable
136 | _BV(ADPS1) | _BV(ADPS0) // CLK/8 = 125 kHz
137 // | _BV(ADPS2) // CLK/16 = 62.5 kHz
139 // ADCSRB |= _BV(GSEL); // gain 8 or 32
141 // Disable digital input on all bits used by ADC
142 DIDR0 = _BV(ADC3D) | _BV(ADC2D);
145 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
147 /* Do first conversion and drop the result */
150 ADCSRA |= _BV(ADIE); // enable IRQ
162 static void adc1_gain20_adc(uint16_t adcsum)
165 adc1_gain20_offset += adcsum
166 - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
170 ISR(ADC_vect) { // IRQ handler
171 uint16_t adcval = ADCW;
174 if (zero_count > 1) {
179 setup_mux(current_adc);
186 ADCSRA |= _BV(ADSC); // drop this one, start the next
199 * Now we have performed read_count measurements and have them
202 switch (current_adc) {
204 // pwmled_adc(current_adc, adc_sum);