2024-04-22 09:04 PM
I am developing a product using the STM32F412REFxLQFP64 chip, and I want to implement multi-PWM control.
For TIM2 buzzer control: TIM_HandleTypeDef TIM2 PB10 ------> TIM2_CH3
TIM3 is used to generate a 1kHz waveform: TIM_HandleTypeDef TIM3 Channel 4 PB1 ------> TIM3_CH4
void buzzerInit(void)
{
Buzzer_InitTypeDef buzzerConfig;
buzzerConfig.channel = TIM_CHANNEL_3;
buzzerConfig.timer = &htim2;
buzzerConfig.timerClockFreqHz = HAL_RCC_GetPCLK2Freq(); // NOTE: this should be freq of timer, not frequency of peripheral clock
hbuzzer.Init = buzzerConfig;
}
void buzzer(BUZZER_StateTypeDef mode){
buzzerStart(&hbuzzer);
size_t songSize;
switch(mode){
case BUZZER_REGISTER:{
songSize = sizeof(buzzer_register_theme) / sizeof(buzzer_register_theme[0]);
for (size_t i = 0; i < songSize; i++) {
buzzerNote(&hbuzzer, buzzer_register_theme[i].pitch);
HAL_Delay(buzzer_register_theme[i].duration * 15);
}
break;
}
case BUZZER_START:{
songSize = sizeof(buzzer_start_theme) / sizeof(buzzer_start_theme[0]);
for (size_t i = 0; i < songSize; i++) {
buzzerNote(&hbuzzer, buzzer_start_theme[i].pitch);
HAL_Delay(buzzer_start_theme[i].duration * 15);
}
break;
}
case BUZZER_END:{
songSize = sizeof(buzzer_end_theme) / sizeof(buzzer_end_theme[0]);
for (size_t i = 0; i < songSize; i++) {
buzzerNote(&hbuzzer, buzzer_end_theme[i].pitch);
HAL_Delay(buzzer_end_theme[i].duration * 15);
}
break;
}
default :{
break;
}
}
buzzerStop(&hbuzzer);
return;
}
void ctrl_pwm_1hz(char _flg)
{
if(pwm_status == _flg)
return;
pwm_status = _flg;
if(_flg == 0)
{
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_4);
}
else
{
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
}
}
When I control the buzzer and the 1kHz signal individually,
there are no issues. However, when I try to control them simultaneously,
the buzzer does not sound.
How can I solve this issue?
Thank you.
Solved! Go to Solution.
2024-05-15 09:36 PM
Thank you
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
I changed this source for activation and it works normally.