I'm trying to use the timer registers to do some timing on my Arduino Mega 2560.
The output of the following code confuses me:
void setup ()
{
// Set up serial connection
Serial.begin(9600);
while (!Serial) {}
// Print control registers of timer 1
Serial.println(TCCR1A); // Outputs 1
Serial.println(TCCR1B); // Outputs 3
}
void loop () {}
Why is TCCR1A
equal to 1
and TCCR1B
equal to 3
? The documentation of the chip says the initial values of the registers are zero. I could of course set them to zero, but I want to understand why it works like this. The values are the same for the other 16-bit timers (3, 4 and 5).
1 Answer 1
The Arduino core does things at startup. One of those things is to configure the timers ready for PWM operation.
Here's the relevant bits from init()
in wiring.c
:
// set timer 1 prescale factor to 64
sbi(TCCR1B, CS11);
#if F_CPU >= 8000000L
sbi(TCCR1B, CS10);
#endif
#if defined(TCCR1A) && defined(WGM10)
sbi(TCCR1A, WGM10);
#endif