0
votes

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

1

1 Answers

0
votes

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.