Missing something with Register Bit Setting Statement

Hi,
This seems to be a very basic question, however I’m clearly missing something.

in the code snippet below the author is trying to set bits WGM12 and CS10 (I think).
However I cannot resolve the compound function bitshift operation.

In the 4th line:

  • if CS10 was 0, after bitshifting 1 to the left by 0 then it will be 1. ANDed to the register would result in the CS10 being set.
  • But if CS10 were 1 then bitshifting 1 to the left by 1 the result would be 10 (dec 2), AND that back into the Register value and you would set the bit to the left of CS10.

This seems to be a common bit of code. Can someone tell me where I’m going astray?

Thanks

JR

OCR1A = 15624;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS10);

WGM12 and CS10 are constants. They're not reads of the bit, they're just constants defined in the core so you don't have to remember which bit in the register is which, you can just remember the names. WGM12 is bit 3 in the TCCR1B register, so WGM12 == 3.

SO you shift a 1 3 places to the left and OR that result with TCCR1B. That sets the WGM12 bit no matter what it was before and doesn't affect any other bits because they're all being ORed with 0.

Thanks much.

Without realizing they were constants, I would have gone ahead and tried to "set" them. You save'd me a lot of hair pulling

JR

By the way, |= means to OR, not to AND.

&= would mean to AND.