retrolefty:
Here is a piece of code that I use to read an A/D port on a M328P chip. I am using the internal 1.1v reference and I wonder if the delay between setting the reference source and doing a conversion is necessary:
I suspect so. Coding Badly and I were working on a sketch way back when that would allow one to read the chips AVcc voltage indirectly by manipulating the MUX register directly and we both found that the first 1 to 3 reading were not 'stabilized', so I think a delay after writing to the mux register before issuing a ADC conversion is worth while. I used the below delay value but the 'wasted' time was not a factor for this particular application so I didn't play around with how short I could make it and still get a stable first ADC conversion value:
delay(50); // Let mux settle a little to get a more stable A/D conversion
// Start a conversion
ADCSRA |= _BV( ADSC );
// Wait for it to complete
while( ( (ADCSRA & (1<<ADSC)) != 0 ) );
// Scale the value
int results = (((InternalReferenceVoltage * 1024L) / ADC) + 5L) / 10L; // calculates for straight line value
return results;
Lefty
Thanks for the input. 50 milliseconds is way too long for my application. I have an LED display routine that runs in an ISR, plus I read the A/D 1000 times and average it to get a clean reading. I also use floating point in the averaging code so that I get the "between" values for the times when the A/D value is sitting on the fence (like blinking between "123" and 124").
The value I am using is as large as I can get away with without crashing the stack due to the code taking longer than the ISR interval.