In Arduino\hardware\arduino\avr\cores\arduino\wiring.c
Line "clockCyclesToMicroseconds(64 * 256)" should be 250 not 256:
#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256)) //Should be 250
clockCyclesToMicroseconds is described simply as the CPU speed divided by 1 million. So a 16mhz cpu returns 16:
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
So at 256 the overflow happens at 16384 / 16 = 1024. However MILLIS_INC (the basis for millis() and other timing code) is set to divide by 1000 not 1024. So 24 microseconds are lost at every millisecond(?).
#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
There are 2 other locations where 255 is used in wiring.c. Should be 250?
#ifdef TIFR0
if ((TIFR0 & _BV(TOV0)) && (t < 255))
m++;
#else
if ((TIFR & _BV(TOV0)) && (t < 255))
m++;
#endif
I found changing all three values to 250 makes RPM reading code much more stable as it reads the millis() inbetween each tacho pulse, then determines the pulses/per minute. Can anyone else confirm a more stable millis() by changing this?