2023-07-26 05:30 AM
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
Solved! Go to Solution.
2023-07-26 06:05 AM - edited 2023-07-26 06:10 AM
> 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:
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
2023-07-26 05:49 AM
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)
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.
2023-07-26 06:05 AM - edited 2023-07-26 06:10 AM
> 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:
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
2023-07-26 06:08 AM
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:
For Channel 1 yours reads "PWM Generation CH1", mine reads "PWM Generation No Output". Question is what is the difference and why.
2023-07-26 06:12 AM
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.
2023-07-26 06:13 AM
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
2023-07-26 06:23 AM
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.........
2023-07-26 07:05 AM
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.
2023-07-26 08:17 AM
Glad you got it sorted :)
2023-07-26 04:04 PM
And what does the fact, that you didn't write your code, have to do with UI?