+
+ adc_sum = 0;
+ // we use the last iteration of zero_count to set up the MUX
+ // to its final destination, hence the "1 +" below:
+ if (adc_params[current_adc].read_zero_log)
+ zero_count = 1 + (1 << (adc_params[current_adc].read_zero_log-1));
+ else
+ zero_count = 1;
+
+ if (adc_params[current_adc].read_drop_log)
+ drop_count = 1 << (adc_params[current_adc].read_drop_log - 1);
+ else
+ drop_count = 0;
+
+ read_count = 1 << adc_params[current_adc].read_keep_log;
+ n_reads_log = adc_params[current_adc].read_keep_log;
+
+ // set up mux, start one-shot conversion
+ if (zero_count > 1)
+ setup_mux(ZERO_ADC);
+ else
+ setup_mux(current_adc);
+
+ ADCSRA |= _BV(ADSC);
+}
+
+void timer_start_adcs()
+{
+ if (current_adc == NUM_ADCS) // Don't start if in progress
+ start_next_adc();
+ else
+ log_byte(0x99); // overrun
+}
+
+/*
+ * Single synchronous ADC conversion.
+ * Has to be called with IRQs disabled (or with the ADC IRQ disabled).
+ */
+static uint16_t read_adc_sync()
+{
+ uint16_t rv;
+
+ ADCSRA |= _BV(ADSC); // start the conversion
+
+ // wait for the conversion to finish
+ while((ADCSRA & _BV(ADIF)) == 0)
+ ;
+
+ rv = ADCW;
+ ADCSRA |= _BV(ADIF); // clear the IRQ flag
+
+ return rv;