Arduino ADC "ADMUX" configuration

Hi all!

This is just a quick question to anyone here that has set up their ADC with their arduino (Mega 2560 or UNO).

There's a line of code that's commonly used .... this one here...

ADMUX =   bit (REFS0) | (adcPin & 0x07);  // AVcc

where, 'adcPin' is often the A0 analog input pin on the arduino, generally defined by:

const byte adcPin = 0;  // A0

Just going to ask somebody here .... is the "& 0x07" part of the code necessary?

Would the following line of code be ok to use too?

ADMUX =   bit (REFS0) | (adcPin);  // AVcc

I'm thinking that the 0x07 part could be to just ensure that bit positions b3, b4, b5, b6, b7 remain all '0' regardless of value is assigned to adcPin, right? Thanks for your help in advance!

I'm thinking that the 0x07 part could be to just ensure that bit positions b3, b4, b5, b6, b7 remain all '0' regardless of value is assigned to adcPin, right? Thanks for your help in advance!

Yes, that's correct. But you should nevertheless not remove that part as it makes your code more robust and in the case you really use a constant value for adcPin the and operation will be removed by the compiler. Just in case you define the wrong value for adcPin the & 0x07 ensures that only the input selection bits are changed.

1. To select Ch-0, we put 0000 into MUX3 - MUX0 bits of ADMUX Register.
2. To select VREF at 5V (AVcc, we put 1 into REFS0 bits of ADMUX Regsiter.
3. Let us leave the ADLAR bit at its default value of 0.
4. Let us make the undefined bit (bit-4) to 0.

The above objectives could be achieved by executing the following codes:

const byte adcPin = 0;  // A0

void setup()
{
  Serial.begin(9600);  // put your setup code here, to run once:
  ADMUX =   adcPin;

  //bit(REFS0); puts 0s in all bit excepet REFS0 bit of ADMUX register
  ADMUX = bit(REFS0) | adcPin;  //01000000 | 00000000 = 01000000 = 0x40
  Serial.println(ADMUX, HEX); //40 = 0100 0000
}

void loop()
{

}

Yes, that’s correct. But you should nevertheless not remove that part as it makes your code more robust and in the case you really use a constant value for adcPin the and operation will be removed by the compiler. Just in case you define the wrong value for adcPin the & 0x07 ensures that only the input selection bits are changed.

It is not really clear how the codes become robust due to this operation – adcPin & 0x07.

GolamMostafa: It is not really clear how the codes become robust due to this operation -- adcPin & 0x07.

It is not clear? ? ?

adcPin & 0x07 makes absolutely certain that only a value between 0 and 7 are used for bits 3 - 0.

That value is then OR'd with REF0.

.

GolamMostafa: It is not really clear how the codes become robust due to this operation -- adcPin & 0x07.

In theory land where you operate it does nothing. But in the real world you can almost count on some user passing a number too big to that and the bitmask makes sure that when they do they don't stomp on the rest of the register.

Thanks very much pylon, golam, ieee and deltaG. All these points you mentioned make good sense. Thanks for helping me with understanding what that '& 7' is for, and the benefits of it. Much appreciated.