Timer1 not resetting - counting down again?

I’m playing around with the timers on the Arduino Micro board and have come across something I don’t understand. I thought that Timer1 was a 16 bit timer and that when it reached 2^16 - it would reset to zero.

I’m finding that upon filling up 8 bits it appears to count back down to 0 and repeat the cycle?

Whats going on here? I assume its incorrect configuration of TCCR1A/B?

Is TCNT1 was accessing all 16bits of the timer register?

void setup() {  
    TCCR1B |= (1 << CS12); // Start timer1 with prescaler 256
}

void loop() {  
    Serial.println(TCNT1); // coount up 8 bits then back down to zero
}

That’s strange. I would expect it to be in 8-bit phase correct PWM mode, since init() sets that up (see wiring.c), hence I agree that it should be counting up and back down.

I’d read out the values of the TCCR1x registers and sanity check them against the datasheet. I think TCNT1 should get you the whole value.

Apologies, I had expected it NOT to be counting up and down and to to wrap at 2^16-1.

I think I need to set TCCR1A = 0x00; to get the behaviour I want.

TCCR1A = 0x01 by default which is indeed PWM, Phase Correct, 8-bit mode. I had expected “normal mode” to be default by virtue of its naming!

For future readers, this appears to give the desired behaviour.

// Configure the timer
TCCR1A = 0x00;          // normal mode
TCCR1B &= 0xF8;         // clear prescaler bits
TCCR1B |= (1 << CS12);  // set presecaler to 256

The default IS normal mode as far as the chip is concerned.

But in order to have PWM on the timer1 pins, it needs to be initialized to a PWM mode. It's getting switched to PWM mode (it's in wiring.c in the blob one of the blobs of #ifdef hell) before setup() is called. init() is what the function that eventually does this is called - it does things like turn on the ADC and set the timers spinning so PWM works and millis() counts up.

Thanks for clearing that up. I was able to find the definitions in the wiring code.