PWM Output on TIM3 Channel 1 Not Working for 4 kHz Buzzer on STM32L476
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-11-11 4:16 AM - last edited on 2024-11-11 4:18 AM by Andrew Neil
Hello,
I'm writing this post because I'm facing an issue with generating a PWM signal on TIM3 for a buzzer application using the STM32L476RET microcontroller. Based on the buzzer's datasheet, it requires a 4,000 Hz square wave with a 50% duty cycle. I’ve connected the buzzer to the PC6 pin, which should work as TIM3_CH1 when configured to AF2, according to the microcontroller’s documentation.
Configuration Summary
- Microcontroller : STM32L476RET
- Clock Source: Internal clock at 16MHz
- PWM Pin : PC6 (TIM3 Channel 1)
In CubeMX, I configured TIM3 Channel 1 for PWM output with these settings:
- Prescaler: 3
- Auto-Reload Register (ARR): 999
These settings were calculated based on the formula:
This results in a 4,000 Hz PWM frequency, as expected:
I did this based on microcontroller datasheet:
For the 50% duty cycle, I set TIM3->CCR1 = 500 in code.
Code for sound Generation
Here is the code I use to start the PWM signal:
(Note that HAL_TIM_PWM_Start returns HAL_OK)
/**
* @function start_sound_generation
*
* @brief Enables the square wave of PWM signal on TIM3, and channel 1.
*/
void start_sound_generation(void)
{
TIM3->CCR1 = 500;
printf("Starting sound generation...\n");
HAL_StatusTypeDef res = HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
printf("res:%d\n", res);
}
Issue
The buzzer does not generate any sound. When I check the PC6 pin with an oscilloscope, there is no waveform, indicating the PWM output is not active.
Any insights on why the PWM signal might not be generating as expected, or potential troubleshooting steps, would be greatly appreciated!
- Labels:
-
STM32CubeMX
-
STM32L4 Series
-
TIM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
2024-11-11 5:24 AM
Read out and check/post content of TIM and relevant GPIO registers.
JW
