Timers
On this page
A wall clock knows the time because it counts the swings of something regular. A microcontroller does the same. Its timer is a counter that goes up by one on each beat of the clock of the chip, so every count is a known length of time.
See it
A counter runs from a 16 MHz clock through a prescaler of 64. It counts from 0 to 255 and starts again, which takes 1.024 ms. You change the prescaler to 256. How long does one cycle take now?
Each tooth is one cycle: the counter climbs from 0 to the top value and starts again. A larger prescaler makes every count last longer, so the teeth grow wider. A lower top value ends each cycle sooner. The clock is 16 MHz and the drawing shows 20 ms.
The idea
A chip has a clock: a signal that beats at a fixed frequency, here 16 million times a second (16 MHz). A timer is a counter built into the chip that counts those beats while the program does other work.
Two settings decide how long one cycle takes. The prescaler divides the clock, so that the counter steps only on every 8th, 64th, 256th or 1,024th beat. The top value is the count at which the counter starts again from 0. An 8-bit counter cannot go above 255. (On an STM32 the prescaler can be any whole number from 1 to 65,536 and the counters have 16 bits, so the top value can reach 65,535; the formula is the same.) Passing the largest value and starting again is called overflow.
t is the time for one cycle in seconds, FCPU is the clock frequency in hertz, and the + 1 is there because 0 is also a count
Each time the counter starts again, the chip can change a pin or run a short piece of code, called an interrupt. The time is only as exact as the clock: the clock inside a chip can be wrong by a few percent, and a crystal is far better.
One worked number
A program needs a signal every 1 ms. The clock is 16 MHz and the prescaler is 64. What top value do you set?
Show the worked solution, 3 steps
- Find how long one count lasts
64 ÷ 16,000,000 s = 0.000004 s = 4 µs.
- Find how many counts make 1 ms
1 ms is 1,000 µs. 1,000 µs ÷ 4 µs = 250 counts.
- Take one away for the count at 0
Counting 0, 1, 2 up to 249 is 250 counts, so the top value is 249. Check it in the figure: keep the prescaler at 64 and set the top value to 249. One cycle lasts 1.000 ms.
The mistake to avoid
A second trap: a large prescaler gives long times but coarse steps. With a prescaler of 1,024 the time can only change in steps of 64 µs. Choose the smallest prescaler that still reaches the time you need.
Check yourself
An 8-bit counter (0 to 255) runs from 16 MHz with a prescaler of 1,024. How long is it from one overflow to the next?
A logger must take a reading exactly once a second from a 16 MHz clock. Which timer can make that interval in one cycle?
You want a counter to make exactly 100 counts in each cycle. What top value do you set?
Where you meet it
- Data loggersA reading taken every second or every minute is started by a timer, not by a delay in the program.
- Speed and flowA timer measures the time between pulses from a shaft sensor or a flow meter, which gives revolutions or litres per minute.
- Motor drives and dimmersThe PWM signal that sets the speed of a fan is made by a timer: the top value sets the frequency.
- A timer is a counter driven by the clock of the chip. A count is a known length of time.
- The prescaler sets how long one count lasts. The top value sets how many counts make a cycle.
- One cycle = prescaler × (top + 1) ÷ clock frequency.
- The count starts at 0, so the number of counts is the top value plus one.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.