diff options
author | Martino Facchin <m.facchin@arduino.cc> | 2020-11-02 11:44:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-02 11:44:10 +0100 |
commit | 87236bccc731f8e81c3f1d0aa4526bb09c381f10 (patch) | |
tree | 538a4fada3e9bc79dab1e9827a95a160695d8614 | |
parent | 6ec80154cd2ca52bce443afbbed8a1ad44d049cb (diff) | |
parent | 4fd3801e32fee9e25f04ae68a4cbfc95533d56df (diff) |
Merge pull request #345 from Vitve4/pr_344
Improve reading ADC result
-rw-r--r-- | cores/arduino/wiring_analog.c | 18 |
1 files changed, 5 insertions, 13 deletions
diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c index e237d6d..0de64f7 100644 --- a/cores/arduino/wiring_analog.c +++ b/cores/arduino/wiring_analog.c @@ -37,7 +37,6 @@ void analogReference(uint8_t mode) int analogRead(uint8_t pin) { - uint8_t low, high; #if defined(analogPinToChannel) #if defined(__AVR_ATmega32U4__) @@ -74,27 +73,20 @@ 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 sbi(ADCSRA, ADSC); // ADSC is cleared when the conversion finishes while (bit_is_set(ADCSRA, 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; } // Right now, PWM output only works on the pins with |