Hi everyone, in this article we will talk about TIMERS and how can we generate PWM signals on STM32F4 microprocessors. Now lets get started to work

Firstly I want to talk about what are TIMERS and PWM signal.

What is TIMER ?

The timer peripheral is part of the basic set of peripherals that are built into all STM32 microcontrollers. They can be used for any purpose related to the timer and counter and are so called. PWM generation, input capture, time base generation, and output comparison are basic uses of a GP timer. Usually in an STM32 microcontroller you will find more GP timers than other timer classes. Studying GP timers clears the basic concepts.

What is PWM signal ?

Pulse width modulation (PWM) is a modulation process or technique used in most communication algorithms to encode the amplitude of a signal to a pulse width or duration of a signal width, the duration of another signal, usually a carrier signal. Although PWM is also used in communication, its main purpose is to control the power supplied to a variety of electrical devices, particularly idle loads such as AC / DC motors.

Now lets get started to coding.

I will use the TIM4 hardware and PD12, PD13, PD14, PD15 pins on STM32F4-Discovery board.

I will use PWM generation for my drone’s motor controlling. So I will define these pins as engines. Here the my defines :

First we are going to declare the TIMER variables to initiate and configure timers.

And keep values of current engine RPM values:

And then start the timer and GPIO’s periphals.

First I will share the PWM and TIMER configuration code and explain it part by part.

Now we need to configure PWM pins for generating PWM signal. To do this first configure them with GPIO and dedicate them to alternate function. After setting the configuration now it is time to init our GPIO settings. Here is the code block will do this :

After making configurations and inititation of the these pins now we must specify their alternate functions. Here we are going to set alternate function as TIMER. Here is the code block:

We have configured the TIMER and pin function. Now it is time to set our PWM generation settings. To do this we need to decide the values of Prescaler, Period, and counter. Here is the code block to set the PWM generation specifications :

Now we are ready to write codes in main function. First we will call the config_PWM method to set and start PWM and then we will set the PWM values in our while section. So we can read some sensors and set the calculated values from sensores to the engine outputs. Lets make some show now. Here is the our main function and while section :

That is all in this article.

Keep following my articles.

Have a nice coding.

Burak Hamdi TUFAN

LEAVE A REPLY

Please enter your comment!
Please enter your name here