Skip to content

Improve reading ADC result #345

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions cores/arduino/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand Down