Skip to main content
Rakesh Menon
Associate
July 2, 2017
Solved

Problem in PWM generation on timer output channel

  • July 2, 2017
  • 4 replies
  • 3249 views
Posted on July 02, 2017 at 05:34

Hello,

I recently migrated to STM32 Platform and I'm trying to learn all the Peripheral features without using Standard Peripheral Libraries. 

Problem Statement: 

I am not able to  get PWM output on  timer output channel 2 ( GPIOA1)  after configuring general purpose timer 2 of STM32F103c8T6 Micro-controller in PWM mode 2.

My Configuration:

 

Timer :              General Purpose timer 2

Timer Clock:     72 MHz

Duty cycle:        50%

Frequency:       50 Hz

MCU crystal :   8 MHz

I configured the GPIOA in Alternate function output Push Pull mode with maximum frequency not exceeding 50 MHz,  I connected an LED bulb to the GPIOA pin 1, but the LED wend on glowing without reduction in the brightness level. The LED was still glowing even after i performed a test with duty cycle set to 0. On the debugger window I could see exactly all the parameters I configured and the Count register was getting incremented. I cannot find information regarding selection of alternate functions for a GPIO Port.

I used  Keil Micro vision 4 IDE for compiling and debugging my source code, 

I read reference manual for register description and used steps from AN4013 Application note to develop source code

Kindly have a view of my source code and please notify me the corrections that need to be done. The target specifications are mentioned in the source code.

Thank you

.

    This topic has been closed for replies.
    Best answer by Mike Burmeister
    Posted on July 03, 2017 at 15:49

    Because you are using PWM2 which means the pulse is negative instead of Positive with PWM1, if you set the counter to zero the pin stays in the high state which will leave the LED turned on.  At 50% which is 32767 you may not see the LED blink as it is too fast.  You may want to try 65500 as a value and see if dims at that level which leaves the pin low.

    Mike

    4 replies

    Amel NASRI
    ST Technical Moderator
    July 3, 2017
    Posted on July 03, 2017 at 13:54

    Hi

    Menon.Rakesh

    ‌,

    I cannot find information regarding selection of alternate functions for a GPIO Port.

    Please refer to GPIO chapter in RM0008 and product datasheet.

    In your code, it seems you are configuring pins 0 and 1 of port A instead of pins 1 and 2 as per your comment in the code (but I don't think this is the problem as PA1 is properly configured).

    Other than AN4013, I recommend you have a look to

    http://www.st.com/content/ccc/resource/technical/document/application_note/group0/91/01/84/3f/7c/67/41/3f/DM00236305/files/DM00236pdf/jcr:content/translations/en.DM00236pdf

    (General-purpose timer cookbook).

    You can also test some ready to use examples based on the Cube package likeSTM32Cube_FW_F1_V1.4.0\Projects\STM3210E_EVAL\Examples\TIM\TIM_PWMOutput.

    -Amel

    To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.
    Rakesh Menon
    Associate
    July 3, 2017
    Posted on July 03, 2017 at 14:20

    Hello Amel,

    I  configured the port pin to be gpio: 'alternate function output push-pull ' after reading RM0008, I doubt whether if there is an option to select which alternate function have to be assigned for specific pin, under gpio section. I read about pin remapping but that's not what I really need.

    Any how I will also test the ready to use examples which you mentioned.

    Regards,

    Rakesh Menon

    Mike Burmeister
    Associate
    July 3, 2017
    Posted on July 03, 2017 at 14:02

    I hate configuring clocks.  It's too cryptic to get it right.  Anyway I used the STM32Cub software to build your example and put a scope on it and it seems to work just fine.

    What I don't see in your code is the configuration of the MCU clock.  Also when you put the pulse to zero the pin goes to a high state and don't know if you have the LED configured active low or high.

    Mike

    Rakesh Menon
    Associate
    July 3, 2017
    Posted on July 03, 2017 at 15:10

    Hello Mike,

    Also when you put the pulse to zero the pin goes to a high state and don't know if you have the LED configured active low or high.

    I configured the LED  in active  high configuration.

    What I don't see in your code is the configuration of the MCU clock. 

    If you are mentioning about system core clock, it has already been set to 72 MHz inside the header file ''stm32f10x.h ''

    The timer clock is also 72 MHz only because the clock branching to timer 2 from APB1 has a multiplier.

    I could see that in the debug Window.

    0690X00000607JjQAI.png

    Also here is the view of the GPIOA peripheral block:

    0690X00000603viQAA.jpg

    I couldn't find any clock setting mistakes.

    I hate configuring clocks.  It's too cryptic to get it right.  Anyway I used the STM32Cub software to build your example and put a scope on it and it seems to work just fine.

     Can you kindly mention about the GPIOA configuration ( selection of particular GPIOA 1 alternate function ).

    Regards,

    Rakesh Menon

    Mike Burmeister
    Mike BurmeisterBest answer
    Associate
    July 3, 2017
    Posted on July 03, 2017 at 15:49

    Because you are using PWM2 which means the pulse is negative instead of Positive with PWM1, if you set the counter to zero the pin stays in the high state which will leave the LED turned on.  At 50% which is 32767 you may not see the LED blink as it is too fast.  You may want to try 65500 as a value and see if dims at that level which leaves the pin low.

    Mike

    Rakesh Menon
    Associate
    July 3, 2017
    Posted on July 03, 2017 at 16:15

    Hello Mike,

    Because you are using PWM2 which means the pulse is negative instead of Positive with PWM1, if you set the counter to zero the pin stays in the high state which will leave the LED turned on. 

    Yeah that's write, I did changed the value to 65000, and I saw the LED getting less brighter, and I did tests with values ranging from 65000 to 65530 in a for loop, and now the change in illumination level is clearly visible. 

    Thanks and regards,

    Rakesh Menon

    MKaba
    Visitor II
    March 24, 2019

    Hi Rakesh,

    You'r output compare outputs disabled. You need to set MOE bit on TIM2_BDTR register and maybe RCC for AFIO is needed.

    Mustafa