user218649
user218649

Reputation: 73

Arduino Interrupts programming

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);

Upvotes: 1

Views: 1109

Answers (1)

skrrgwasme
skrrgwasme

Reputation: 9633

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:

  1. CS12 is bit #2 of the TCCR1B register, so it has a value of 2.
  2. The integer 1 has a value of 0b00000001
  3. 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.
  4. 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

Upvotes: 3

Related Questions