/****************************************************************************************** * * Function name : ADC_conversion * * Returns : unsigned int adc result, 8 x averaged * * Parameters : None * * Purpose : Run the ADC conversion on ATMEGA16 * ******************************************************************************************/ unsigned int ADC_conversion(unsigned char source) // needs approx 9*250µs = 2.25ms { unsigned char i; unsigned int ADC_temp = 0; unsigned int adcresult = 0; ADMUX = source; // select ax, ay or bat_mon, each single ended delay(66); // some delay for channel selection to settle! // ADC needs 13 cyles/conversion = 13*16,1µs = 209µs ADCSRA |= (1< batmon() conflict solution while(!(ADCSRA & 0x10)); // wait for conversion done, ADIF flag active for(i=0;i<8;i++) // do the ADC conversion 8 times for better accuracy { ADCSRA |= (1<> 3; // average the 8 samples return adcresult; }