2024-11-11 04:16 AM - last edited on 2024-11-11 04: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
In CubeMX, I configured TIM3 Channel 1 for PWM output with these settings:
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!
2024-11-11 05:24 AM
Read out and check/post content of TIM and relevant GPIO registers.
JW