Embedded calculator, part of Premium

MCU timer & PWM

MCU timer & PWM: the circuit drawn with the example's values16 MHz1 kHzprescalercounter
÷ 1, reload 15999 for 1 kHz on 16 MHz (0 % off)

A timer counts clock ticks: the prescaler slows them, the reload says how many.

Opens in the app on 1 December, with Circuit Notebook 1.0 See it worked out

Part of Premium. It runs in your browser: nothing to install.

The example, worked out

Given

Timer clock
16 MHz
Counter width
16 bits
Prescaler
Pick for me (finest)
I want
A frequency
Frequency
1 kHz

Working

  1. ticks per period
  2. the counter goes from 0 to ARR, that is N ticks
  3. what you really get

Results

Prescaler÷ 1
Reload (top) value15,999
Frequency you get1 kHz
Period you get1 ms
Error+0 %
PWM resolution here14 bits
Longest period, this prescaler4.1 ms

The formulas

  1. N = fclk / (P × f)ticks per period
  2. ARR = round(N) − 1the counter runs from 0 to ARR
  3. PWM bits = log2(ARR + 1)the duty resolution at that frequency

The usual traps

  • Writing the prescaler without the minus one.
  • A small count weighs one tick heavily: pick the smallest prescaler that fits.

Questions

How do I set the prescaler and reload of a microcontroller timer?

N = fclk / (P × f): ticks per period. ARR = round(N) − 1: the counter runs from 0 to ARR. PWM bits = log2(ARR + 1): the duty resolution at that frequency.

What does it give for typical values?

÷ 1, reload 15999 for 1 kHz on 16 MHz (0 % off). Prescaler: ÷ 1; Reload (top) value: 15,999.

What are the usual mistakes?

Writing the prescaler without the minus one. A small count weighs one tick heavily: pick the smallest prescaler that fits.

In the app

Your circuit, drawn live

Change a value: the schematic redraws with your numbers on the parts.

The part you can buy

Results rounded to standard values (E12, E24, E96) and power ratings.

The working, shown

Every step written out. Try it first by hand, then check.

Into your notebook

One click puts the circuit on a sheet of your project.

Opens in the app on 1 December, with Circuit Notebook 1.0

Also in Embedded