To go off on a tangent a little bit, I wondered when I saw the source code that functions like millis() did not disable interrupts while accessing timer0_overflow_count:
unsigned long millis()
{
// (comments omitted)
return timer0_overflow_count * 64UL * 2UL / (F_CPU / 128000UL);
}
Because it requires multiple atmega8 instructions to fetch the value of a 4-byte long integer like timer0_overflow_count, isn't it possible for a timer interrupt to occur in the middle of those instructions and mess up the result? I wonder if the following might make the code safer:
unsigned long millis()
{
// (comments omitted)
unsigned long safe_copy;
cli();
safe_copy = timer0_overflow_count;
sei();
return safe_copy * 64UL * 2UL / (F_CPU / 128000UL);
}
Likewise, to reset the timer to zero, it might be better to do:
cli();
timer0_overflow_count = 0;
sei();