Fusion of Engineering, Control, Coding, Machine Learning, and Science

Generate PWM signals in STM32 microcontrollers

Figure shown below illustrates a Pulse Width Modulation (PWM) signal.

Figure 1: Illustration of the PWM signal.

The PWM signal is defined by the following variables

The frequency of the PWM signal is

(1)  

The duty cycle is defined by

(2)  

In the STM32 development environment, we select the frequency of the PWM signal by using the three parameters , , and , and the following equation

(3)  

where

The frequency the time clock of the clock is measured in MHz. Usually, every STM32 microcontroller has a maximal value. The prescaler value is used to downscale the frequency of the time clock. The period value is the final parameter that determines the frequency of the PWM signal.

In the STM32CubeIDE development environment:

In the STM32CubeIDE environment, the duty cycle is determined by

(4)  

where is the Capture/Compare Register (CCR). We select the value of in the interval .

Let us illustrate this with an example. Let us suppose that we want to generate a PWM signal with the frequency of . Let us suppose that the time clock frequency is . Then, by selecting the prescaler value of and the period of , we obtain the desired frequency

(5)  

Exit mobile version