Skip to content
You Start with one site
Amounts in Start with one site
Microcontrollers · Concept

Timers

One idea Beginner 5 min read
Found this useful?

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.

ClockA regular beat inside the chip.16,000,000 a second
PrescalerPasses on only one beat in 8, 64, 256 or 1,024.Slows the counting
CounterGoes up by one on each beat it receives, then starts again at 0.0 to 255

See it

Predict first

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?

See it: a counter that climbs and starts again

One count lasts
Counts in one cycle
One cycle lasts

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 = prescaler × (top + 1) ÷ FCPU

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

The question

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
  1. Find how long one count lasts

    64 ÷ 16,000,000 s = 0.000004 s = 4 µs.

  2. Find how many counts make 1 ms

    1 ms is 1,000 µs. 1,000 µs ÷ 4 µs = 250 counts.

  3. 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 top value of 250 does not give 250 counts. The counter starts at 0, so it gives 251. In the worked number that makes every cycle 0.4% too long, and a clock built on it loses 5.7 minutes a day. The number of counts is always the top value plus one.

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

Question 1

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?

Question 2

A logger must take a reading exactly once a second from a 16 MHz clock. Which timer can make that interval in one cycle?

Question 3

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.
Key points
  • 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.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.