SME
Embedded Systems · Problem type
Longest timer period
Use this for “How long can a 16-bit timer at 16 MHz run with prescaler 1024 before it overflows?”
Diagram
The
timer
counts
up
at
fₜ
and
resets
after
it
matches
the
compare
value,
raising
the
interrupt
(IRQ).
fₜ
=
fclk
N
=
16
MHz
1,024
=
15.63
kHz.
65,536 ticks of 64 μs make P = 4.194 s; compare = 65,536 − 1 = 65,535.
It fits a 16-bit timer (below 65,536).
This is an example. Type your own values to start a new problem.
Tₘₐₓ (s)
=
2
×
fclk (MHz)
fₜ
Timer clock
calculated · kHz
ticks
Ticks to overflow
calculated
top
Top count
calculated
Formulas
fₜ
=
1,000
×
fclk
N
15.625
=
1,000
×
16
1,024
ticks
=
2
n
65,536
=
2
16
top = ticks − 1
65,535 = 65,536 − 1
Tₘₐₓ
=
ticks
1,000 × fₜ
4.1943
=
65,536
1,000 × 15.625
Assumptions
•
The timer counts up from 0 through its top count 2ⁿ − 1 and overflows on the next tick.
•
So the longest period is 2ⁿ ticks: Tₘₐₓ = 2ⁿ × N ÷ fclk.
•
A longer period needs a bigger prescaler, a slower clock or a software count of overflows.
Step-by-step
WE KNOW
n = 16, N = 1,024, fclk = 16 MHz
FIND
timer clock (fₜ), ticks to overflow (ticks), top count (top), longest period (Tₘₐₓ)
Step 1 · Find timer clock (fₜ)
Use fₜ = 1,000 × fclk ÷ N
The prescaler divides the clock; 1000 turns MHz into kHz.
fₜ
=
1,000
×
16
1,024
fₜ
=
16,000
1,024
fₜ = 15.625 kHz
Step 2 · Find ticks to overflow (ticks)
Use ticks = 2ⁿ
An
n
-bit
counter
holds
2ⁿ
counts,
0
to
2ⁿ
−
1,
before
it
wraps
to
0.
ticks
=
2
16
ticks = 65,536
Step 3 · Find top count (top)
Use top = ticks − 1
The count starts at 0, so the top count is one less than the ticks.
top = 65,536 − 1
top = 65,535
Step 4 · Find longest period (Tₘₐₓ)
Use Tₘₐₓ = ticks ÷ (1,000 × fₜ)
The ticks over the tick rate; 1000 turns kHz into Hz, so the time is in seconds.
Tₘₐₓ
=
65,536
1,000 × 15.625
Tₘₐₓ
=
65,536
15,625
Tₘₐₓ = 4.1943 s
Check
Put the numbers back in. Both sides should match.
15.625
=
1,000
×
16
1,024
✓
65,536
=
2
16
✓
65,535 = 65,536 − 1 ✓
4.1943
=
65,536
1,000 × 15.625
✓
Topic check
See what you know: 5 questions on this topic
New questions each time. Pass to mark it complete.
Related lessons
Interrupts and timers
Main lesson
PWM frequency and duty cycle
Use this for “A 16 MHz timer with prescaler 8 counts to TOP = 1999 and compares at 500. Find the PWM frequency, duty and average voltage at 5 V.”
CPU time in an interrupt
Use this for “An interrupt fires at 10 kHz and its ISR takes 20 μs. What share of the CPU does it use?”
What happens on an interrupt
Use this for “A timer matches its compare value. Put in order what the CPU does until the main program resumes.”