2 #include <avr/interrupt.h>
8 #define BATTERY_ADC (N_PWMLEDS + 0)
9 #define BUTTON_ADC (N_PWMLEDS + 1)
10 #define ZERO_ADC (N_PWMLEDS + 2)
12 //#define NUM_ADCS ZERO_ADC
16 unsigned char read_zero_log : 2;
17 unsigned char read_drop_log : 2;
18 unsigned char read_keep_log : 4;
19 } adc_params[NUM_ADCS] = {
20 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 1
22 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 2
23 { 0, 1, PWMLED_ADC_SHIFT }, // pwmled 3
24 { 0, 1, AMBIENT_ADC_SHIFT }, // ambient
25 { 0, 1, 0 }, // battery
26 { 0, 1, 0 }, // gain20
27 { 0, 1, 0 }, // buttons
31 volatile static unsigned char current_adc, current_slow_adc;
32 static uint16_t adc_sum, zero_count, drop_count, read_count, n_reads_log;
35 static void setup_mux(unsigned char n)
37 /* ADC numbering: PWM LEDs first, then others, zero at the end */
39 case 0: // pwmled 1: 1.1V, ADC3 (PB3), single-ended
40 ADMUX = _BV(REFS1) | _BV(MUX1) | _BV(MUX0);
42 case ZERO_ADC: // zero: 1.1V, GND, single-ended
43 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
51 if (current_adc == 0) {
52 if (current_slow_adc > N_PWMLEDS) {
53 // read one of the non-PWMLED ADCs
54 current_adc = --current_slow_adc;
56 // no more non-PWMLEDs to do, start with PWMLEDs
57 current_adc = N_PWMLEDS-1;
59 } else if (current_adc >= N_PWMLEDS) {
60 // one of the non-PWMLED ADCs just finished, skip to PWMLEDs.
61 current_adc = N_PWMLEDS-1;
67 // single ADC for testing only
72 log_byte(0x90 + current_adc); // debug ADC switching
76 // we use the last iteration of zero_count to set up the MUX
77 // to its final destination, hence the "1 +" below:
78 if (adc_params[current_adc].read_zero_log)
79 zero_count = 1 + (1 << (adc_params[current_adc].read_zero_log-1));
83 if (adc_params[current_adc].read_drop_log)
84 drop_count = 1 << (adc_params[current_adc].read_drop_log - 1);
88 read_count = 1 << adc_params[current_adc].read_keep_log;
89 n_reads_log = adc_params[current_adc].read_keep_log;
91 // set up mux, start one-shot conversion
95 setup_mux(current_adc);
100 void timer_start_slow_adcs()
102 if (current_slow_adc > N_PWMLEDS) { // Don't start if in progress
103 log_byte(0x80 + current_slow_adc);
105 current_slow_adc = NUM_ADCS;
106 // TODO: kick the watchdog here
111 * Single synchronous ADC conversion.
112 * Has to be called with IRQs disabled (or with the ADC IRQ disabled).
114 static uint16_t read_adc_sync()
118 ADCSRA |= _BV(ADSC); // start the conversion
120 // wait for the conversion to finish
121 while((ADCSRA & _BV(ADIF)) == 0)
125 ADCSRA |= _BV(ADIF); // clear the IRQ flag
133 current_slow_adc = NUM_ADCS;
137 ACSR |= _BV(ACD); // but disable the analog comparator
139 ADCSRA = _BV(ADEN) // enable
140 | _BV(ADPS1) | _BV(ADPS0) // CLK/8 = 125 kHz
141 // | _BV(ADPS2) // CLK/16 = 62.5 kHz
143 // ADCSRB |= _BV(GSEL); // gain 8 or 32
145 // Disable digital input on all bits used by ADC
146 DIDR0 = _BV(ADC3D) | _BV(ADC2D);
149 ADMUX = _BV(REFS1) | _BV(MUX3) | _BV(MUX2) | _BV(MUX0);
151 /* Do first conversion and drop the result */
154 ADCSRA |= _BV(ADIE); // enable IRQ
166 static void adc1_gain20_adc(uint16_t adcsum)
169 adc1_gain20_offset += adcsum
170 - (adc1_gain20_offset >> ADC1_GAIN20_OFFSET_SHIFT);
174 ISR(ADC_vect) { // IRQ handler
175 uint16_t adcval = ADCW;
178 if (zero_count > 1) {
183 setup_mux(current_adc);
190 ADCSRA |= _BV(ADSC); // drop this one, start the next
203 * Now we have performed read_count measurements and have them
206 switch (current_adc) {
208 // pwmled_adc(current_adc, adc_sum);
209 pwmled_adc(1, adc_sum);