Working with STM32F7 and Timers: PWM mode

In the previous guide (here), we discussed how to use the timer to generate accurate delay. In this guide, we shall look at another application for timer which is to generate PWM signal on two channels and fading two leds.

In this guide, we will cover the following:

  • What is PWM
  • Configure the timer and GPIO to generate PWM signal
  • Connection 
  • Code
  • Demo

1. What is PWM:

PWM is a technique used to emulate “analog signal” by rapidly turning on-off the pin. This allow the mcu to vary the power delivered to the load such as motor (will be covered later). The PWM has three main characteristics;

  • Frequency:

which describe the duration time of the entire signal

  • Duty cycle

The term duty cycle describes the proportion of ‘on’ time to the regular interval or ‘period’ of time; a low duty cycle corresponds to low power, because the power is off for most of the time. Duty cycle is expressed in percent, 100% being fully on.

  • Amplitude 

Which is the voltage level of the PWM (3.3v for STM32F7).

For more details, please check this wikipedia article (here

2. Configure the timer and GPIO to generate PWM Signal:

First we need to locate which pins connected to TIMER2_CH1 and TIMER2_CH2.

From the datasheet, we can find the related timer channels

From the datasheet, we can see that TIM2_CH1 is connected to PA0 and CH2 is connected to PA1.

Hence, we can start configuring the timer and GPIO

We start off by setting GPIO

First thing first, we need to enable clock access to GPIOA as following: (for enabling clock access, check this topic)

C

Then setting the pins in alternate mode as following:

C

Then select which alternate function, in our case it is AF1

C

Now we can configure the timer to generate PWM signal

We start off by enabling clock access to TIMER2 as following:

C

Set the prescaller to 0 (no divider)

C

Set the maximum value to 255 ( this will give frequency of 62.5KHz)

C

Reset the current counter

C

Now, we configure the mode to be PWM mode as following:

C

And enable the channel as following:

C

Finally, we enable the timer as following:

C

hence, the initializing function shall be this

C

3. Connection:

4. Code:

C

5. Demo:

Happy coding 😀

6 Comments

  • MehmetAli AYDIN Posted April 21, 2024 2:28 pm

    TIM2->CCMR1=TIM_CCMR1_OC1M_1|TIM_CCMR1_OC1M_2|TIM_CCMR1_OC2M_1|TIM_CCMR1_OC2M_2; //configure the pins as PWM
    ——————————————————————————————————————————————–

    TIM2->CCMR1 | =
    Why is there no or operator?

    • Husamuldeen Posted April 22, 2024 5:10 am

      You can use either | or =.
      The | will modify only the required bits from 0 to 1 while = will set the entire register to the desired value.

      • MehmetAli AYDIN Posted April 23, 2024 5:46 pm

        Mr Husamuldeen, for your answer Thank you

        I live in Turkey, Whenever I have time, I learn from the articles you write on your website.I am interested in programming as a hobby.I do other jobs for a living.age 51
        see you goodbye

        • Husamuldeen Posted June 3, 2024 1:58 pm

          Nothing I am aware of.
          It should just be the SPI function and the delay function that needed to be changed.

  • MehmetAli AYDIN Posted April 23, 2024 6:01 pm

    TIM2->CCMR1=
    TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2|
    TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2;
    I couldn’t see any value assigned to the first and second bits of OC1M.

    • Husamuldeen Posted April 28, 2024 1:02 pm

      Hi,
      Is the PWM working properly ?

Add Comment

Leave a Reply to Husamuldeen Cancel reply

Your email address will not be published. Required fields are marked *