read ADC in one step and adjust calculation (most probably it doesn't

make much of a difference)
This commit is contained in:
Nicu Hodos 2018-12-31 18:07:20 +01:00
parent 3d7e0e3ec6
commit bd36cfed2d

View File

@ -35,15 +35,11 @@ protected:
delay(2); // Wait for Vref to settle delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA, ADSC)) while (bit_is_set(ADCSRA, ADSC)); // measuring
; // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH uint16_t result = ADC; // read ADC in one step
uint8_t high = ADCH; // unlocks both
long result = (high << 8) | low; result = 1126400L / result; // Calculate Vcc (in mV); 1126400 = 1.1*1024*1000
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts return result; // Vcc in millivolts
} }