2024-12-16 07:20 AM - last edited on 2024-12-16 07:40 AM by Sarra.S
Hello everyone,
I'm working on a motor control project using an STM32 microcontroller (TIM1 and TIM8 timers), and I need to implement a soft Break to safely stop the PWM signals with proper transitions (respecting Dead Time and setting the outputs to 0V in low-impedance mode). However, the methods I've tried so far don't seem to work as expected. Here's what I want to achieve:
Initialization Code (PWM Start):
HAL_StatusTypeDef motor_pwm_init(uint8_t motor_i) {
HAL_StatusTypeDef status = HAL_OK;
TIM1->CCR2 = VALUE_MAX_TIMER_PWM;
TIM1->CCR1 = VALUE_MAX_TIMER_PWM;
TIM8->CCR2 = VALUE_MAX_TIMER_PWM;
TIM8->CCR1 = VALUE_MAX_TIMER_PWM;
if (motor_i == MOTOR_1) {
status |= HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
status |= HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
status |= HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
status |= HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
} else if (motor_i == MOTOR_2) {
status |= HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_1);
status |= HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_1);
status |= HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_2);
status |= HAL_TIMEx_PWMN_Start(&htim8, TIM_CHANNEL_2);
}
return status;
}
HAL_StatusTypeDef motor_pwm_stop(uint8_t motor_i) {
HAL_StatusTypeDef status = HAL_OK;
if (motor_i == MOTOR_1) {
status |= HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
status |= HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
status |= HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_2);
status |= HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
} else if (motor_i == MOTOR_2) {
status |= HAL_TIM_PWM_Stop(&htim8, TIM_CHANNEL_1);
status |= HAL_TIMEx_PWMN_Stop(&htim8, TIM_CHANNEL_1);
status |= HAL_TIM_PWM_Stop(&htim8, TIM_CHANNEL_2);
status |= HAL_TIMEx_PWMN_Stop(&htim8, TIM_CHANNEL_2);
}
return status;
}
htim1.Instance->BDTR |= TIM_BDTR_OSSI; // Set off-state in Idle
htim1.Instance->BDTR |= TIM_BDTR_OSSR; // Set off-state after Break
htim1.Instance->BDTR |= TIM_BDTR_BKE; // Trigger Break
How can I configure TIM1 to respect the Dead Time when stopping the PWM signals? Is triggering a soft Break (TIM_BDTR_BKE) supposed vto work without an external signal? If yes, what additional configurations are required?
Any help or guidance would be greatly appreciated! Thank you in advance.
Solved! Go to Solution.
2024-12-17 07:04 AM
Hello @simon_EQUIUM,
Thank you for your feedback.
Refer to Advanced-control timers chapter on Reference manual documentation.
As mentioned in "Using the break function" chapter, "Break events can also be generated by software using BG and B2G bits in the TIMx_EGR register."
2024-12-17 06:42 AM
Ok, I have found the solution.
To stop the motor by software you have to use the lines :
htim1.Instance->EGR |= TIM_EGR_BG;
And to restart the motor :
htim1.Instance->BDTR &= ~TIM_BDTR_MOE;
status |= HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
status |= HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
osDelay(1);
status |= HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
status |= HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
htim1.Instance->BDTR |= TIM_BDTR_MOE;
I don't know why, but I have to place a osDelay before PWMN because I want to active in a first time the low side of my channel. Without this delay, the high side start before the low side and the bootstrap capacitor can't be charged.
2024-12-17 07:04 AM
Hello @simon_EQUIUM,
Thank you for your feedback.
Refer to Advanced-control timers chapter on Reference manual documentation.
As mentioned in "Using the break function" chapter, "Break events can also be generated by software using BG and B2G bits in the TIMx_EGR register."