Reading ADC register on ATmega32u4

Hello

I am using an Arduino Micro with a ATmega32u4 chip. As a first step in a bigger experiment that involves fast reading of an analog pin I try to manually read the ADC register. However, this fails so far:

uint16_t adc_read(uint8_t ch)
{
        // select the corresponding channel 0~5
	ch &= 0b00000101; // ensure valid argument
	ADMUX = (ADMUX & 0xF8) | ch; // select channel

	// write '1' to ADSC to start single convertion
	ADCSRA |= _BV(ADSC);

	// wait for conversion to complete
	while (ADCSRA & _BV(ADSC));

	return (ADC);
}

ADC gets initialized inside the setup function with

ADMUX = _BV(REFS0);
ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0);

The values returned by the adc_read function are always above 1000 whereas calling analogRead returns values between 200~300 (noise).

Can someone help me to fix this issue?

Have you carefully studied the ADC section of the data sheet for the 32u4? If so, add appropriate comments to the posted code to explain your choice of register settings.

For example, this line makes it impossible to choose channel 2, 3, 6 or 7:

ch &= 0b00000101; // ensure valid argument

Post all the code, showing how the snippets are used in the main program.

It looks like you've copied the example from here or a similar source, with the exception, as noted above, that you have rather oddly restricted the channels to 0,1,4, and 5.
There is nothing connected to the AREF pin ?

Thank you for yours replays. The code suddenly worked. I don’t know what exactly caused this issue. And yes, my modified bit mask was wrong too.