I get empty results in the ADCL and ADCH registers after using the ADC on an ATmega328. I've tried my best following the datasheet. The reason for doing this manually (not using AnalogRead) is in case we need to port this code to another MCU in the future. Any help is greatly appreciated. The following is the code:
ADMUX = 0x00;
/* Start conversion */
PRR = 0x00;
ADCSRA = B1100111;
/* Wait until conversion is done */
bool conv_running = true;
while (true) {
conv_running = ADCSRA & (1 << 6);
if (!conv_running) break;
}
/* Results of conversion */
uint8_t low = ADCL;
uint8_t high = ADCH;
volatile int res = (high << 8) | low;
Serial.println(String(res));
I was wondering if there was something I had configured wrong in any register that could be seen in my code. Since the ADC register was 0000000000000000 after the conversion (the problem). So, everything about the code (after I did the fixes you gave me) seems to be okay?
Here is what I use, sample rate around 38 kHz, if I recall correctly.
// setup
ADCSRA = 0xe5; // set the adc to free running mode
ADMUX = 0x40; // use adc0
DIDR0 = 0x01; // turn off the digital input for adc0
// code
while(!(ADCSRA & 0x10)); // wait for adc to be ready
ADCSRA = 0xf5; // restart adc
byte m = ADCL; // fetch adc data
byte j = ADCH;
int result=(j<<8)|m;