简体   繁体   中英

avr: atmega328p ADC always reads max value

I've been trying to sample an analog signal with the atmega328p in c. Previously i've got this working with something similar, but somehow this time i can't get it to work. I've tried to make a minimal example that gets me the same result:

#include <avr/io.h>

uint8_t data[2];    

ADMUX = (1 << REFS0);
ADCSRA = (1 << ADEN);

ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
data[1] = ADCL;
data[0] = ADCH;

This code results in the array data = {0b00000011, 0b11111111} , no matter what. As the atmega328p has a 10-bit adc, this is the maximum value, so i'm probably doing something wrong but i just can't spot the mistake.

Thanks in advance for any answers

Regards, Harm

It looks like you're trying to read from ADC0, which is pin PC0. Make sure that you are connecting PC0 to GND or some other known voltage; if it is floating, you can get unpredictable results.

You should also try writing 0x87 to ADMUX to slow down the ADC's clock.

If you need more help, you should post your complete code and and pictures showing how everthing is wired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM