I've never quite internalized setting bits, so after some time away..... I see an example and don't have the confidence in my understanding to just say its wrong so:
I don't recall where I copied this snippet from but looking a it, it seems wrong.
would this not set ADEN and ADPS2 to 1 and all others to 0?
ADCSRA =
(1 << ADEN) | // Enable ADC
(1 << ADPS2) | // set prescaler to 16, bit 2
(0 << ADPS1) | // set prescaler to 16, bit 1
(0 << ADPS0); // set prescaler to 16, bit 0
making
(0 << ADPS1) | // set prescaler to 16, bit 1
(0 << ADPS0); // set prescaler to 16, bit 0
meaningless.
I believe the correct way is ....
To set ADEN & ADPS2 to 1, ADPS1 & ASPS0 to 0 one should code:
ADCSRA = ASCSRA |
(1 << ADEN) | // Enable ADC
(1 << ADPS2) | // set prescaler to 16, bit 2
(0 << ADPS1) | // set prescaler to 16, bit 1
(0 << ADPS0); // set prescaler to 16, bit 0
ADCSRA = 0b00001111
Will set the leftmost 4 bits to zero
ADCSRA = ADCSRA | 0b00001111
Will leave the leftmost 4 bits at their current value.
I've seen a number of examples where a register is set by the first example above. And is successful because many / most / all? registers default to all zero's on reboot. But still wrong IMHO
finds nothing like that, so is that a typo? Or is my google broken?
Part of the |= syntax and its friends
ADCSRA |= 0b00001111;
is that it is one less place to wonder if terms in an expression are the same, or should be, or if they are indeed different. And one less place to make a typo, I suppose.