I was chasing a weird timer related bug in my code and finally discovered it was because I was assuming the system programmed timer0 to run at 4 us per step in normal mode. Instead it is programmed for fast PWM (TCCR0A = 0x03, TCCR0B = 0x03).
Why is it set this way?
And where is the code in the system that sets it (maybe it's commented)?
I reprogrammed it to normal mode (TCCR0A = 0x00) and millis() and delay() were seemingly unaffected. But maybe something else is depending on the fast PWM programming? I can't imagine what it is.
void init()
{
// this needs to be called before setup() or some functions won't
// work there
sei();
// on the ATmega168, timer 0 is also used for fast hardware pwm
// (using phase-correct PWM would mean that timer 0 overflowed half as often
// resulting in different millis() behavior on the ATmega8 and ATmega168)
#if defined(TCCR0A) && defined(WGM01)
sbi(TCCR0A, WGM01);
sbi(TCCR0A, WGM00);
#endif
Thanks for that. I can never seem to find where something is defined.
It looks like the system is also configuring the other timers, anticipating how they might be used. I think this means that if I change timer0 to normal mode it won't harm anything.