As a part of my venture to learn in depth about microcontroller, I used the analogRead() and then viewed the contents of the register using the following code
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.println(analogRead(1));
Serial.println(ADMUX,BIN);
Serial.println(ADCSRA,BIN);
Serial.println(ADCL,BIN);
Serial.println(ADCH,BIN);
delay(1000);
}
The above code worked perfectly and got me random values, as I left the analog pin floating.
What stymied me is that when i modded the code a bit as below:
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.println(analogRead(1));
Serial.println(ADMUX,BIN);
Serial.println(ADCSRA,BIN);
Serial.println(ADCH,BIN);
Serial.println(ADCL,BIN);
delay(1000);
}
The analogRead() function started giving me the same value over and over again. I connected 5V to the pin and it was giving the same value as before which was around 300. Then I noticed page 265 of the Atmgea328P's datasheet which says:
When ADCL is read, the ADC Data Register is not updated until ADCH is read. Consequently, if
the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read
ADCH. Otherwise, ADCL must be read first, then ADCH.
I think it holds the answer. But can someone tell me the reason why the order of reading of the ADCL and ADCH registers affect the update of the register in the next use of analogRead().
Thanks.