PWM No Output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 5: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.
- Labels:
-
STM32L0 Series
-
TIM
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 6:05 AM - edited ‎2023-07-26 6: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:
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 5: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 6:05 AM - edited ‎2023-07-26 6: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:
- 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 6: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 6: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 6: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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 6: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.........
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 7: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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 8:17 AM
Glad you got it sorted :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-07-26 4:04 PM
And what does the fact, that you didn't write your code, have to do with UI?
