2020-08-20 04:27 PM
Hi,
I have a code in which I am controlling a PWM output in Center Aligned Mode using Timer 2 Channels 2 (inverted polarity) and 3, both having their duty cycles changed by the functions:
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,(duty_c-0.05)*400);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3,(duty_c)*400);
When I do not use these functions, all my HAL_Delay functions in main.c work properly and so does the LCD, with the 50us interrupt working properly as well.
As soon as I add those two lines using __HAL_TIM_SET_COMPARE to change the duty cycle(CCR registers), the code gets stuck in a HAL_Delay loop, and I don't get why that happens.
The code is stuck in the infinite here:
while ((HAL_GetTick() - tickstart) < wait)
__weak uint32_t HAL_GetTick(void)
{
return uwTick;
}
Why does it happen only when this line is added?
I would like to understand how to solve such issue, it's botching me for a long time so far.
2020-08-21 09:41 AM
Why would it be exhausted? That's a 20kHz control application in which I change the duty cycle every 50us in order to adjust the system control base on adc values, isn't it quite normal in many power electronics and motor control applications?
2020-08-22 05:46 AM
You can look at the code for __HAL_TIM_SET_COMPARE and verify it's not going to block execution. The problem is somewhere else.
#define __HAL_TIM_SET_COMPARE(__HANDLE__, __CHANNEL__, __COMPARE__) \
(((__CHANNEL__) == TIM_CHANNEL_1) ? ((__HANDLE__)->Instance->CCR1 = (__COMPARE__)) :\
((__CHANNEL__) == TIM_CHANNEL_2) ? ((__HANDLE__)->Instance->CCR2 = (__COMPARE__)) :\
((__CHANNEL__) == TIM_CHANNEL_3) ? ((__HANDLE__)->Instance->CCR3 = (__COMPARE__)) :\
((__HANDLE__)->Instance->CCR4 = (__COMPARE__)))
Agreed that a 20kHz interrupt is fast, but doable, for typical clock speeds.