Simple question really, I just want to be sure that I understand it correctly. I understand this isn't necessarily Arduino specific.

With regards to the ATmega2560 I'm doing 3 back-to-back A/D conversions - one immediately after the previous finishes. I'm accomplishing this by calling a Single Conversion each time.

I stumbled across this useful code to wait for the conversion to finish:

while(ADCSRA & (1<<ADSC))

I understand that it is waiting for the conversion to finish and that when this is done the ADSC will be set to 0. (Conversely, I could wait for ADIF to be set to 1.) I'm not as clear on how exactly this line is interpreted.

Normally when I see something along the lines of (1<<ADSC) it means write a 1 to the ADSC bit.

But, in this instance, that isn't the case.

Why not just write:


You misunderstand the meaning of:

1 << ADSC

That actually means Bit-shift 1 left by the number of bits specified by the value ADSC.

ADSC in this case is 6:

#define ADSC    6

So if you did:


you would actually be doing:


Which of course will never end, since 6 is not 0, so is true.

Instead you shift 1 left 6 times, which in binary would be:

    << 6

That, of course, is 64, or 0x40 in hexadecimal. That is then ANDed with the value of the ADCSRA register to return the value of one single bit within that register. It's then the result of that AND that you control the while with. So it ends up as:

while(ADCSRA & 0x40);

Which will either equate to:


if the 7th bit is set, or if it's not set:


And since 0x00 is 0 it will equate to false when the bit isn't set and the while will finish.

  • I should've specified because I think it will make a difference, my apologies, ADSC is a flag in the ADCSRA register.
    – Ramrod
    Mar 15 '16 at 21:49
  • 1
    It is also a #define for a number. In this instance it is the number of the ADSC bit in the register. You use it to manipulate the register - you can't (easily) manipulate the bit by itself.
    – Majenko
    Mar 15 '16 at 21:50
  • There is great support for this in the AVR library. Please see nongnu.org/avr-libc/user-manual/group__avr__sfr.html. This also helps explain the AVR MCU registers and bit field symbols and usage. loop_until_bit_clear(ADCSRA, ADSC); Mar 15 '16 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.