3 * @author Sam Moore - adapted from ATMEL AVR Butterfly ADC example
\r
4 * @purpose Analogue to Digital Conversion
\r
10 #include "LCD_functions.h"
\r
13 /*****************************************************************************
\r
15 * Function name : ADC_read
\r
21 * Purpose : Do a Analog to Digital Conversion
\r
23 *****************************************************************************/
\r
24 float ADC_read(uint8 input)
\r
30 ADMUX |= (1<<REFS0) | (1<<REFS1);
\r
36 ADMUX |= (1<<REFS0);
\r
37 ADMUX &= ~(1<<REFS1);
\r
48 ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);
\r
50 //do a dummy readout first
\r
51 ADCSRA |= (1<<ADSC); // do single conversion
\r
52 while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active
\r
54 for(i=0;i<setup.adc_averages;i++) // do several ADC conversions, for averaging
\r
56 ADCSRA |= (1<<ADSC); // do single conversion
\r
57 while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active
\r
59 ADC_temp = ADCL; // read out ADCL register
\r
60 ADC_temp += (ADCH << 8); // read out ADCH register
\r
62 delta = ADC_temp - mean;
\r
63 mean = mean + delta/(i+1);
\r
64 M2 = M2 + delta*(ADC_temp - mean);
\r
66 // ADCr += ADC_temp; // accumulate result
\r
68 // ADCr = ADCr / setup.adc_averages; // average the samples
\r
72 ADCSRA &= ~(1<<ADEN); // disable the ADC
\r
74 setup.adc_values[input] = mean;
\r
77 setup.adc_variance[input] = M2/setup.adc_averages;
\r
79 return (setup.adc_values[input]);
\r