Lite / Building Machines That Respond / Week 25
Timers: hardware that counts for you
Your loop is busy reading sensors. Who is keeping track of the time?
Why should I care?
Timers are the peripheral behind every blink, every PWM signal, every timeout and every 'millis()' clock. Knowing how they count lets you predict timing exactly instead of hoping your loop runs at the right speed.
- Explain what a hardware timer and its prescaler do.
- Calculate tick rates, ticks in a time window and the time until a counter overflows.
- Choose a prescaler for a required timing resolution and range.
Predict
A 16 MHz clock feeds a timer through a prescaler of 64. How many times a second does the timer count?
- 16,000,000
- 250,000
- 64
Guess first, then reveal
The prescaler divides the clock: 16,000,000 ÷ 64 = **250,000** ticks a second, one every 4 µs. In 1 ms the timer counts 250.
Discover
Ignore the tasks for now and use the timer readout. Scrub to 1 ms, 100 ms and 1,000 ms with a prescaler of 64, then 1,024. When does the 16-bit counter overflow for each prescaler?
What the interactive shows (text version)
The timing scrubber includes a timer readout: with a 16 MHz clock and the chosen prescaler (1, 8, 64, 256 or 1,024), it shows the tick rate (16,000,000 ÷ prescaler), the ticks counted by the scrubbed time, and how many times a 16-bit counter (65,536 values) has overflowed.
Understand
A counter in hardware
A timer is a peripheral that adds 1 to a counter on every tick, whatever the CPU is doing. Firmware reads the counter to know how much time has passed, or asks the timer to raise a flag (or an interrupt) when it reaches a value. Because it is hardware, its timing does not wobble when the loop gets busy.
The prescaler
Counting at the full 16 MHz would fill a 16-bit counter in about 4 ms. A prescaler divides the clock first: ÷8, ÷64, ÷256, ÷1,024 are common. Dividing by 64 gives 4 µs ticks and about 0.26 s before overflow; dividing by 1,024 gives 64 µs ticks and about 4.2 s.
Overflow and wrap-around
A 16-bit counter can only hold 65,536 values (0–65,535). One tick after 65,535 it wraps back to 0: an overflow. Firmware either counts overflows to extend the range, or measures intervals with subtraction that copes with wrap-around (week 25's non-blocking pattern does this with a 32-bit millisecond clock).
Explain it another way
Intuitive
A timer is a turnstile clicker at a stadium gate: it counts by itself while the manager does other jobs. The prescaler is only clicking for every 64th person, so the clicker fills up more slowly.
Technical
Timers can count up to a compare value and reset (clear-on-compare), toggle a pin, capture the count when an input changes, or generate PWM. Arduino-style millis() uses a timer overflow interrupt to keep a 32-bit millisecond count.
Mathematical
ticks = f_clock·t/prescaler; t_overflow = 2^bits·prescaler/f_clock; resolution = prescaler/f_clock.
Applied
A washing machine's controller uses one timer for its motor PWM, another for its 1 ms system clock, and counts minutes for each wash stage from that clock.
Derivation: Ticks, prescalers and overflow
Tick rate: f_tick = f_clock ÷ prescaler. 16 MHz ÷ 64 = 250,000 ticks per second.
Ticks in a time t: ticks = f_tick × t. In 20 ms: 250,000 × 0.020 = 5,000 ticks.
Resolution (time per tick) = 1 ÷ f_tick = 4 µs.
A 16-bit counter holds 0 to 65,535 and then wraps to 0: it overflows every 65,536 ÷ f_tick seconds = 65,536 ÷ 250,000 ≈ 0.262 s.
Bigger prescaler: coarser ticks but a longer time before overflow. Choose the smallest prescaler whose range covers the longest interval you need.
Worked example 1
You need to time intervals up to 2 s with a 16-bit timer on a 16 MHz clock. Which prescaler from 1, 8, 64, 256, 1,024 should you choose?
Overflow time = 65,536 × prescaler ÷ 16,000,000. ÷256: 1.05 s (too short). ÷1,024: 4.19 s (enough), with 64 µs resolution. Choose 1,024.
Common mix-ups
The CPU has to count time itself in a loop.
✓ Timers are hardware: they count on their own and the CPU just reads them.Overflow means the timer broke.
✓ Overflow is normal wrap-around. Good firmware expects it and handles it.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Plan the timers for a greenhouse controller: a 1 ms system tick, a 20 kHz fan PWM and a 10-minute logging interval. For each, choose how you would make it (prescaler, counts, or counting ticks in firmware) and show the arithmetic.
Reflect
When would a 4 µs timing resolution matter, and when would 64 µs be plenty?
Write your answer on paper or in your own notes. Lite never stores what you write.