Skip to main content
SJose.11
Associate
June 27, 2021
Solved

How to generate PWM using STM32 Nucleo L433RC?

  • June 27, 2021
  • 15 replies
  • 11557 views

I have followed the ST tutorial on how to genterate a 1Hz PWM using timer, but for some reason its not blinking.

I configured:

1)Clock configuration to 8Mhz

2)Set PWM genration channel 3

3)Set prescaler to 127

4)Set ARR to 62499

0693W00000Bc6snQAB.png5)Selected PWM mode 1 and made the pulse value to 31250

6)Finally I set the PINB10 to tim2_ch3 and connected it to an LED in a breadboard

In the main.c I have this HAL function to start the PWM

 HAL_TIM_PWM_Start(&htim2,HAL_TIM_ACTIVE_CHANNEL_3 );

Please help ,I have been with this for some time now. But still I am not getting the output.

This topic has been closed for replies.
Best answer by waclawek.jan

Read out and check/post content of TIM and relevant GPIO registers.

Make sure PB10 is connected to the header where you observe it, that there are no disconnected jumpers or solder bridges, and that there is no onboard resource connected to this pin - check this in the Nucleo's manual.

Try to set PB10 to GPIO and toggle it in a loop, with a delay. Even better, try to toggle it "manually" from debugger.

JW

15 replies

waclawek.jan
waclawek.janBest answer
Super User
June 27, 2021

Read out and check/post content of TIM and relevant GPIO registers.

Make sure PB10 is connected to the header where you observe it, that there are no disconnected jumpers or solder bridges, and that there is no onboard resource connected to this pin - check this in the Nucleo's manual.

Try to set PB10 to GPIO and toggle it in a loop, with a delay. Even better, try to toggle it "manually" from debugger.

JW

SJose.11
SJose.11Author
Associate
June 28, 2021

I checked it with toggling PB10 ,It was working fine. But for some reason PWM blink code is note working. I also tried debugging it and the the PWM timer was kept in if condition to check it with HAL_OK, and it was without any error.

if(HAL_TIM_PWM_Start(&htim2,HAL_TIM_ACTIVE_CHANNEL_3 )!=HAL_OK){

 Error_Handler();

 }

waclawek.jan
Super User
June 28, 2021

Read out and check/post content of TIM and relevant GPIO registers.

JW

SJose.11
SJose.11Author
Associate
June 28, 2021

0693W00000BcC1aQAF.png

Javier1
Principal
June 28, 2021

ST videos sometimes feel more like a powerpoint sales pitch than hands-on engineering learning material.

Try this video https://www.youtube.com/watch?v=rM7QonHkh2w&feature=emb_title

hit me up in https://www.linkedin.com/in/javiermuñoz/
waclawek.jan
Super User
June 28, 2021

TIM2_CR1.CEN=0, i.e. timer is not enabled, it is not running. You should also see TIM2_CNT changing, when you read in the TIM2 values repeatedly.

Also, TIM2_CCER=0, i.e. no output is enabled. But maybe because you stopped execution before calling the Cube/HAL function which would set it.

GPIO_ODR is irrelevant here; you want to have a look at GPIO_MODER and GPIO_AFR.

[EDIT] After reloading the thread I see that you've posted all GPIOB registers - MODER and AFR for PB10 look OK there.

JW

SJose.11
SJose.11Author
Associate
June 29, 2021

0693W00000BcHssQAF.pngI checked it and the timer is enabled and its also counting, but there is some problem with the GPIOB configuration I guess.

waclawek.jan
Super User
June 29, 2021

In CCMR2, you have set CH3 for output compare, but in CCER you have set the enable bit for CH2.

JW

SJose.11
SJose.11Author
Associate
June 29, 2021

Thanks for your help.The issue was a bug in HAL function :

HAL_TIM_PWM_Start(&htim2,HAL_TIM_ACTIVE_CHANNEL_4 )

When I gave HAL_TIM_ACTIVE_CHANNEL_4 its taken as channel 3 and giving the correct output.0693W00000BcLuKQAV.png

waclawek.jan
Super User
June 29, 2021

I don't use Cube/CubeMX, but it appears that you are supposed to use TIM_CHANNEL_3 as a parameter there https://github.com/STMicroelectronics/STM32CubeL4/blob/5e1553e07706491bd11f4edd304e093b6e4b83a4/Drivers/STM32L4xx_HAL_Driver/Src/stm32l4xx_hal_tim.c#L1420

JW

SJose.11
SJose.11Author
Associate
June 29, 2021

I used TIM_CHANNEL_3  , then Channel 2 was enabling. Then I changed to TIM_CHANNEL_4.

waclawek.jan
Super User
June 29, 2021

> I used TIM_CHANNEL_3 , then Channel 2 was enabling. Then I changed to TIM_CHANNEL_4.

No. You were using HAL_TIM_ACTIVE_CHANNEL_3 and then you've change it to HAL_TIM_ACTIVE_CHANNEL_4 . Those are different constants than TIM_CHANNEL_3 and TIM_CHANNEL_4.

JW

SJose.11
SJose.11Author
Associate
June 30, 2021

You are right. When I use TIM_CHANNEL_3 its working.

Thanks for your help.