The problem is that I am unable to read from multiple ADC channels (atmega8-au). The code works fine if only one channel is used eg. ADC0, however, if the second channel ADC1 is used then the value of readADC1 is equal to readADC0.

What should I do this to fix the problem? Might it be related to resetting the ADMUX bits?

The related code:

#include <avr/io.h>

unsigned int threshold = 900;
volatile unsigned int sensor_left = 0;
volatile unsigned int sensor_center = 0;
volatile unsigned int sensor_right = 0;

void init()
{
DDRD |= 1 << PORTD0;
DDRD |= 1 << PORTD1;

}

{
ADMUX |= (0 << REFS1) | (1 << REFS0);
ADMUX |= (0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (0 << MUX0);
}

{
ADMUX |= (0 << REFS1) | (1 << REFS0);
ADMUX |= (0 << MUX3) | (0 << MUX2) | (0 << MUX1) | (1 << MUX0);
}

int main(int argc, char* argv)
{
init();

while (1) {

if (sensor_left < threshold) {
PORTD |= 1 << PORTD0;
} else {
PORTD &= ~(1 << PORTD0);
}

if (sensor_center < threshold) {
PORTD |= 1 << PORTD1;
} else {
PORTD &= ~(1 << PORTD1);
}
}
}

• And we are just supposed to guess what hardware is involved? – PlasmaHH Apr 21 '17 at 9:48
• Updated, it's atmega8-au @PlasmaHH – sitilge Apr 21 '17 at 9:50