Toolvito

Works on this device

Timer Frequency Calculator

A hardware timer counts ticks of a clock and starts again at a set value. This calculator finds how often it does that, or which prescaler and register value give the frequency or period you want, and how far off the result is.

Enter your values

For a hardware timer of a microcontroller: how often it overflows or matches, from its settings.

Your values

The clock that feeds the timer, for example the CPU or peripheral bus clock. Check the clock tree in your data sheet.

Type 1 for no prescaler.

The value at which the counter turns round: a compare, period or auto-reload register, depending on the maker.

Most timers that restart at a register value count TOP + 1 ticks. Phase-correct and centre-aligned modes count up and down.

Your values are worked out in this browser. They are not sent to a server.

Your result will show here

Fill in the values, then choose Calculate.

How to use this calculator

  1. Choose what you want: the frequency from your settings, the settings for a frequency or period, or the overflow period of a free-running counter.
  2. Enter the clock that feeds the timer, before its prescaler. Check the clock tree in the data sheet; the timer clock is not always the CPU clock.
  3. Choose how the counter counts: up and start again (most timers), or up and down (phase-correct or centre-aligned modes).
  4. To find settings, type the prescalers your timer offers and the width of its counter.
  5. Choose Calculate. The settings search shows every prescaler in a table.

The formulas

  • One tick of the counter = prescaler ÷ clock.
  • Counting up from 0 to TOP and starting again: f = clock ÷ (prescaler × (TOP + 1)).
  • Counting up and down: f = clock ÷ (prescaler × 2 × TOP).
  • Register value for a frequency f: TOP = clock ÷ (prescaler × f) − 1 (or clock ÷ (2 × prescaler × f) up and down), taken to the whole number that comes closest.
  • Error = (frequency you get − frequency you want) ÷ frequency you want, in % and in ppm (parts per million).
  • Overflow period of a free-running n-bit counter = prescaler × 2^n ÷ clock.
  • A pin that toggles at each event makes a square wave of half the event frequency.

Worked examples

A 1 ms tick. Clock 16 MHz, prescaler 64, TOP = 249, counting up: one tick is 64 ÷ 16,000,000 = 4 µs, one period is 250 ticks = 1 ms, so the timer runs at 1,000 Hz.

440 Hz from an 8-bit timer. Clock 16 MHz with prescalers 1, 8, 64, 256 and 1024. Only 256 and 1024 fit in 8 bits. With 256 the exact TOP is 141.05; TOP = 141 gives 16,000,000 ÷ (256 × 142) = 440.14 Hz, an error of +0.032% (320 ppm).

Overflow. A 16-bit counter at 16 MHz with prescaler 64 overflows every 64 × 65,536 ÷ 16,000,000 = 0.262144 s, which is 3.815 times a second.

Limits you should know

  • Register names and counting modes differ between makers. TOP here is the register value at which the counter turns round. The data sheet of your microcontroller decides how many ticks one period has.
  • The error shown comes only from whole register values. The clock source adds its own error: a crystal is often off by tens of ppm, an internal RC oscillator by much more.
  • No prescaler lists are built in. The list in the box is only an example; type the one from your data sheet.
  • For the duty cycle and resolution of a PWM output, use the Pwm Calculator. To divide a clock by a whole number in logic, use the Clock Divider Calculator. For a 555 timer, use the 555 Timer Calculator.

Related tools