So I have to do a little task on interrups and I need to know what do these lines of code mean:
TCCR1A = 0;
TCCR1B = 0;
TCCR1B |= (1 << CS12);
user218649user218649
asked Mar 13, 2015 at 6:58
1 Answer 1
TCCR1A and TCCR1B are macros for addressing Timer #1 in the Arduino. This is what these lines are doing:
TCCR1A = 0; // Clear all bits of TCCR1A register
TCCR1B = 0; // Clear all bits of TCCR1B register
TCCR1B |= (1 << CS12); // ORs the current value of TCCR1B with 0b00000100,
// and stores the result back in TCCR1B
This is how the last line does its magic:
- CS12 is bit #2 of the TCCR1B register, so it has a value of 2.
- The integer 1 has a value of 0b00000001
- The
<<
operator is a logical shift left operation that takes the left operand, and shifts its bits to the left by the number of places indicated by the right operand. In this case, this means that 0b00000001 becomes 0b000000100. - The
|=
operator takes the value of the left operand, ORs it with the value of the right operand, and assigns the result back to the left operand location.
The result of this code is that TCCR1A ends up with all bits clear, and TCCR1B will have all bits clear execpt for bit 2.
Here is some more information on the TCCRx registers that explain what these assignments actually accomplish:
http://letsmakerobots.com/content/arduino-101-timers-and-interrupts
answered Mar 13, 2015 at 8:39
Sign up to request clarification or add additional context in comments.
2 Comments
user218649
what about this line? TIMSK1 = (1 << TOIE1);
Igor Stoppa
it enables the interrupt upon counter overflow: Timer Overflow Interrupt Enable -> TOIE