From 64786bc166546858ff3fccc845be32ef9d5af3d0 Mon Sep 17 00:00:00 2001 From: MCUdude Date: Thu, 19 Jan 2023 22:12:48 +0100 Subject: [PATCH] Use ADC instead of combining ADCL and ADCH --- wiring_analog.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/wiring_analog.c b/wiring_analog.c index 6401003..a7a843d 100755 --- a/wiring_analog.c +++ b/wiring_analog.c @@ -72,27 +72,19 @@ int analogRead(uint8_t pin) // without a delay, we seem to read from the wrong channel //delay(1); -#if defined(ADCSRA) && defined(ADCL) +#if defined(ADCSRA) && defined(ADC) // start the conversion ADCSRA |= _BV(ADSC); // ADSC is cleared when the conversion finishes while (ADCSRA & _BV(ADSC)); - // we have to read ADCL first; doing so locks both ADCL - // and ADCH until ADCH is read. reading ADCL second would - // cause the results of each conversion to be discarded, - // as ADCL and ADCH would be locked when it completed. - low = ADCL; - high = ADCH; + // ADC macro takes care of reading ADC register. + // avr-gcc implements the proper reading order: ADCL is read first. + return ADC; #else - // we dont have an ADC, return 0 - low = 0; - high = 0; + return 0; #endif - - // combine the two bytes - return (high << 8) | low; }