Empty result after using ADC

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));

PelleS:
I get empty results in the ADCL and ADCH registers after using the ADC on an ATmega328.

“Empty” is a concept not supported by any AVR register so you cannot possibly be getting “empty results in the ADCL and ADCH registers”.

uint8_t low = ADCL;
uint8_t high = ADCH;
volatile int res = (high << 8) | low;

Don’t bother reading individual registers. Using the 16 bit ADC “register” is a far better choice.

(A volatile local is an oxymoron.)

Serial.println(String(res));

Casting to String before printing an int is a pointless waste. Don’t do that.

Thank you for your reply. "Empty" was a figure of speech. Doing these changes did unfortunately not fix the problem.

It appears my primary message has not been received. Maybe this will work...

PelleS: ...the problem.

  • ? -

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?

Ah, there we go. You are expecting a non-zero value.

This is when you post a schematic.

And complete up-to-date code.

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;

Thank you so much, jremington! You saved my day! :) After sitting with this for many, many hours, I was about to give up.