This calculator will help you compute the desired PWM frequency. Just put in the clock frequency and the desired PWM frequency and click the “calculate” button. The prescaler value and the ARR (auto-reload register value) will be the results.
What is PWM?
Pulse Width Modulation (PWM) is a technique used to generate analog-like signals from a digital source by varying the duty cycle of a square wave. It is widely used in applications like motor control, LED brightness control, and audio signal generation.
In STM32 microcontrollers, PWM signals are typically generated using timers. The timers divide the clock frequency and configure the output pins to create the desired frequency and duty cycle.
Key Concepts of STM32 PWM
- System Clock Frequency:
This is the main clock frequency driving the microcontroller, which serves as the base clock for the timers. - Prescaler:
A value that divides the timer input clock. For example, if the system clock is 72 MHz and the prescaler is set to 71, the timer clock will be.
- Auto-Reload Register (ARR):
Also known as the timer period, this defines how many timer clock cycles occur before the timer resets and the signal toggles. A higher ARR results in a lower frequency. - Duty Cycle:
This is the percentage of time the signal stays HIGH during one period. The duty cycle is controlled by the Capture/Compare Register (CCR) in STM32.
Formula to Calculate PWM Frequency
The PWM frequency can be calculated using the formula:
Where:
- System Clock Frequency: The frequency of the system clock driving the timer (e.g., 72 MHz for many STM32 devices).
- Prescaler: The value used to divide the clock frequency.
- ARR (Auto-Reload Register): The maximum count value for the timer.
Example Calculation
Scenario:
You have an STM32 microcontroller with the following parameters:
- System Clock Frequency:
- Timer Prescaler:
- Auto-Reload Register (ARR):
Steps:
- Calculate the timer clock:
- Calculate the PWM frequency:
Result: The PWM frequency is 1 kHz.
STM32 Configuration for PWM
To set up PWM in STM32, follow these steps:
- Initialize the Timer:
- Set the prescaler and auto-reload values.
- Configure the timer for PWM mode (e.g., using Timerx_CCMRx registers).
- Set the Duty Cycle:
- Configure the Capture/Compare Register (CCR) to control the duty cycle.
For a 50% duty cycle, set
- Configure the Capture/Compare Register (CCR) to control the duty cycle.
- Start PWM:
- Enable the timer (e.g., using Timerx_CR1 register) and the output pin.
Applications of PWM in STM32
- LED Dimming: Control brightness by adjusting the duty cycle.
- Motor Speed Control: Vary the average voltage applied to the motor.
- Audio Generation: Create audio signals by varying PWM frequencies.
- Servo Control: Use PWM to control the position of servo motors
PWM is a versatile feature of STM32 timers, enabling precise control over frequency and duty cycle. By understanding parameters like the system clock, prescaler, and ARR, you can calculate and configure the desired PWM frequency for your application.