cancel
Showing results for 
Search instead for 
Did you mean: 

PWM No Output

cage
Associate II

Hello,

I am using STM32L052K6T6 with a passive buzzer is connected to PA6. Idea was to use TIM22_CH1 as PWM output to feed the buzzer. However when I get to configure PWM on this port, only relevant looking option is "PWM Generation No Output" and indeed the pin does not seem to be outputting any PWM waveform.

I might be misunderstanding something, what does PWM Generation No Output means ? I checked the Datasheet several times, and there is no difference on specs between this channel and others. What does it mean a pin being assigned to PWM but not outputting ?

Is there a way for me to get PWM output from this pin (aside from bouncing the pin manually on every timer interrupt) ?

Many thanks,

Ali

 

1 ACCEPTED SOLUTION

Accepted Solutions

> What does it mean a pin being assigned to PWM but not outputting ?

That's just some CubeMX nonsense. Maybe if you are using a preset board such as Nucleo, that pin is used up for some other purpose.

Timers are simple enough so that you don't need to depend on CubeMX to set them up:

  • set given pin to AF in GPIOx_MODER
  • set the proper AF (see Alternate function table in datasheet - here, TIM22_CH1 on PA6 is AF5) into GPIOx_AFR[]
  • set GPIOx_OSPEEDR and GPIOx_OTYPER as needed (or just let them at default for now)
  • in TIM22, set TIMx_ARR to the required period
  • set TIMx_CCMRx.OCxM to PWM1 or PWM2 as required
  • set required compare point into TIMx_CCRx
  • enable given channel in TIMx_CCER.CCxE
  • and finally enable timer's counter by setting TIMx_CR1.CEN.

You can get more fancy than that by using preloads, using prescaler, etc.; but for first experiment this is enough. Later you can simply copypaste this piece of code whenever you need PWM (that's how you build your own "library" of code).

JW

View solution in original post

10 REPLIES 10
KSOdin2
Associate III

I will reply to this post as if you are using STM32CubeIDE:

To make a pin a PWM signal, you must enable the Timer, select a channel to be PWM Generation and set the counter period in parameter settings. See an example below of the settings (using the STM32H723)

KSOdin2_0-1690375426818.png

Next, the code has 2 parts. First, you must start the timer. For Timer 2, channel 1, I use the following code (I've added the HAL_OK check, but you can remove that):

 

 

if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}

 

Then you must set the Duty cycle. Otherwise, there will be no signal on the pin and just a flat line. This can be done will the following code:

 

TIM2->CCR1 = 50;

 

TIM2 is the timer, CCR1 is the channel (channel 1), and 50 is the duty cycle %.

I hope this helps.

> What does it mean a pin being assigned to PWM but not outputting ?

That's just some CubeMX nonsense. Maybe if you are using a preset board such as Nucleo, that pin is used up for some other purpose.

Timers are simple enough so that you don't need to depend on CubeMX to set them up:

  • set given pin to AF in GPIOx_MODER
  • set the proper AF (see Alternate function table in datasheet - here, TIM22_CH1 on PA6 is AF5) into GPIOx_AFR[]
  • set GPIOx_OSPEEDR and GPIOx_OTYPER as needed (or just let them at default for now)
  • in TIM22, set TIMx_ARR to the required period
  • set TIMx_CCMRx.OCxM to PWM1 or PWM2 as required
  • set required compare point into TIMx_CCRx
  • enable given channel in TIMx_CCER.CCxE
  • and finally enable timer's counter by setting TIMx_CR1.CEN.

You can get more fancy than that by using preloads, using prescaler, etc.; but for first experiment this is enough. Later you can simply copypaste this piece of code whenever you need PWM (that's how you build your own "library" of code).

JW

cage
Associate II

Hello, @KSOdin2  thanks  for the detailed post. I did all of this. Only difference we are having is on Timer setup this is what I see:

cage_0-1690376813094.png

For Channel 1 yours reads "PWM Generation CH1", mine reads "PWM Generation No Output". Question is what is the difference and why.

Thanks @waclawek.jan . I hope it is actually a UI issue as you described, I will retest. FYI I am just I didn't choose a board by mistake but picked up the correct MPU only. 

KnarfB
Principal III

It works for me. Using the latest STM32CubeMX 6.9.0 and STM32L052K6T6 MCU. If you get conflicts (in red), hover the mouse over the conflicting setting and get some explanation.
If your error persists, show screen shots etc..

hth

KnarfB

And how do you define the pin on PA6? If it is a GPIO, it can no longer be used for PWM output. Must be configured as a TIM.........

cage
Associate II

Ok sorted, this was a UI problem as @waclawek.jan suggested and adding 

HAL_TIM_PWM_Start

as @KSOdin2 made it work. Thanks for everyones help. 

Glad you got it sorted :) 

And what does the fact, that you didn't write your code, have to do with UI?