MCU Timer Calculator

Calculate prescaler, reload, and actual timing values with error for STM32 and 8051 timers, generate init code, and compute STM32 PWM frequency, duty cycle, and CCR.

FreeOnline Tool
Loading…

How to Use

The tool has two tabs, Timer and PWM, and recalculates automatically when inputs change.

  1. On the Timer tab, choose the platform: STM32 or 8051.
  2. STM32: pick a timer clock (72/84/168/180 MHz or custom), enter the target period, and choose μs, ms, or s. The tool scans PSC from 0 to 65535 for the 16-bit ARR combination with the smallest error and reports PSC, ARR, actual time, and error.
  3. 8051: pick a crystal (11.0592 MHz, 12 MHz, or custom), enter the target time in ms, and choose Mode 1 (16-bit) or Mode 2 (8-bit auto-reload) to get TH0, TL0, actual time, and error.
  4. Switch to the PWM tab, choose a timer clock, and enter PWM frequency in Hz and duty cycle from 0 to 100 percent to get PSC, ARR, CCR, actual frequency, and error.
  5. “Copy code” copies the generated initialization code. The reference table below lists common periods or PWM frequencies for the current clock and settings.
  6. “Load sample” restores the defaults for the current tab and recalculates.

Features

  • STM32 timer: solves (ARR+1)×(PSC+1)/TIM_CLK by scanning PSC to minimize error, with both PSC and ARR limited to the 16-bit range 0–65535.
  • 8051 timer: machine cycle = 12/crystal frequency. Mode 1 is 16-bit and Mode 2 is 8-bit auto-reload; TH0/TL0 are shown in decimal and hexadecimal.
  • STM32 PWM: finds the PSC/ARR pair closest to TIM_CLK/((PSC+1)×(ARR+1)) and computes CCR = ARR × duty cycle.
  • Code generation: produces StdPeriph-style STM32 timer/PWM initialization or 8051 C code for Mode 1 and Mode 2, ready to copy.
  • Reference tables: STM32 timer periods from 1 to 1000 ms, 8051 times by mode, and PWM frequencies from 100 Hz to 50 kHz, all computed with the current clock.
  • Range errors: if the target period or frequency cannot be generated, the tool reports an error and shows the approximate limit for the current crystal or mode.

Use Cases

STM32 periodic interrupt setup
For a 1 ms or 10 ms tick, get the PSC/ARR pair and StdPeriph initialization code without manual trial and error.
8051 timer initialization
Calculate TH0/TL0 at an 11.0592 MHz crystal for a baud-rate generator or periodic task interrupt.
PWM dimming and motor control
Enter a target PWM frequency and duty cycle to get PSC, ARR, and CCR for LED dimming, servos, or a DC motor driver.
Compare clock options
Switch between 72/84/168/180 MHz or a custom clock and compare actual timing error for the same target to choose a suitable timer clock.

FAQ

What are the valid PSC and ARR ranges for STM32?
Both are 16-bit and range from 0 to 65535. A target that is too short makes the computed ARR negative, and one that is too long exceeds 65535; in either case the tool reports that no valid combination was found, so adjust the target or clock.
What is the difference between 8051 Mode 1 and Mode 2?
Mode 1 is a 16-bit timer with a maximum of about 65536 machine cycles and requires reloading TH0/TL0 in the interrupt. Mode 2 is 8-bit auto-reload with a maximum of 256 machine cycles, suited to short high-frequency interrupts but with a much smaller timing limit.
Why does the actual time differ from the target?
PSC, ARR, TH0, and TL0 must be integers, so prescaling and counting introduce quantization error. The tool reports the error percentage so you can choose a better combination. Crystal tolerance, interrupt latency, and clock accuracy add further error.
Can the generated code be compiled as-is?
It is common StdPeriph-style or 8051 C code that configures only the timer time base or PWM channel. It does not configure GPIO, clock buses, or interrupt priority. Adjust the timer instance, APB bus, and pins for your exact part.
How is the PWM duty cycle converted to CCR?
The tool uses CCR = round(ARR × duty / 100), with duty from 0 to 100 percent. The actual frequency is TIM_CLK/((PSC+1)×(ARR+1)), and both the actual frequency and relative error are shown.
Can I enter a custom clock or crystal?
Yes. Both the STM32 clock and 8051 crystal selectors include a Custom option, which reveals an input field in Hz or MHz respectively. The value must be a valid positive number.