16 MHz÷640/64TCNT00255001TOV0CPU · SRAMms0fract0011ISR(TIMER0_OVF_vect) {2 ms += 1; fract += 3;3 if (fract >= 125) {4 fract -= 125; ms += 1;5 }6}1now = millis();2if (now - last >= 500) {3 last += 500;4 led = !led;5 digitalWrite(13, led);6}1+1+3+15001500
Back: the timer path on an Uno (clock, prescaler, counter, overflow flag, CPU with SRAM, LED)Front: the core's overflow ISR and your loop()Tokens: clock ticks, the overflow signal, additions to ms and fract, millis() valuesAn Arduino Uno's Timer0: the 16 MHz clock feeds a prescaler (÷64), which steps the 8-bit counter TCNT0 once every 4 µs, in hardware, while the CPU runs your code. Every 1,024 µs the counter rolls from 255 to 0 and sets the flag TOV0; the overflow interrupt adds to ms and fract, and fract carries the extra 24 µs into whole milliseconds. millis() returns ms, so loop() can blink the LED every 500 ms without delay(); ms wraps after about 49.7 days, and now - last stays correct. The motion is slowed down a great deal.

The timer hardware

  1. The chip's clock ticks 16 million times a second. An 8-bit counter fed straight from it would fill up every 16 µs. That is too fast to be useful for counting milliseconds, so we slow the ticks down first.

The millis() interrupt

Using millis()

What happens

;; step 1/9 · clock
clock     16,000,000 ticks per second (16 MHz)
one tick  1 / 16 MHz = 0.0625 µs
8-bit     counts 0 .. 255, so 256 ticks fill it
256 x 0.0625 µs = 16 µs: far too short

The cards rename the core's timer0_millis and timer0_fract to ms and fract, and fold its MILLIS_INC (1), FRACT_INC (3) and FRACT_MAX (125) into plain numbers. The real ISR also counts overflows for micros(). Timer0 belongs to millis(), so for your own timing use Timer1, a 16-bit timer. In CTC mode it clears when it matches OCR1A: with prescaler 1024 it ticks 15,625 times a second, so OCR1A = 15624 gives exactly one interrupt per second.