cancel
Showing results for 
Search instead for 
Did you mean: 

Function : __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3,(duty_c)*400); is disturbing my 16x2 LCD routine which demands a delay to receive and display data on the screen.

FAleg.1
Associate II

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.

11 REPLIES 11
TDK
Guru

What is your call stack when this occurs? Sounds like it is stuck in an interrupt handler that isn't be pre-empted by the SysTick interrupt.

If you feel a post has answered your question, please click "Accept as Solution".

Be careful not to use blocking functions in interrupt handlers and callback routines, and code they call.

Make sure that SysTick has highest priority in the system and can preempt other interrupts, otherwise the HAL code is likely to function in undesirable ways, unless you're very attentive to detail and potential interactions.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I thought of increasing SysTick priority but I realized that all functions in the interrupt table are assigned 0 priority (highest), so should I decrease all the others to 1 and leave SysTick as 0?

Additionally, I am not calling any function inside the interrupt other than the _HAL_TIM_SET_COMPARE, they LCD ones are all inside my main.c. Also, when I try to set the value straight to the register (htim2.Instance->CCR= xx;) the same bug takes place.

TDK
Guru

> so should I decrease all the others to 1 and leave SysTick as 0?

That's one way to solve it.

> Additionally, I am not calling any function inside the interrupt, they are all inside my main.c

Then it shouldn't be blocking. Did you disable interrupts? What is your call stack when it occurs?

If you feel a post has answered your question, please click "Accept as Solution".

I am sorry, I made a mistake stating I had no function in my interrupt handler, Actually I do which is the _HAL_TIM_SET_COMPARE. Even though when I try to change it writing directly to the register (htim2.Instance->CCR=xx ; ) the same bug happens. I did not disable interrupts, they are pre-set when I generated the code from CubeMX. I am trying to look the call stack but it's only written main, its location/value and type, I can't see anything else.

0693W000003PvZeQAK.jpg

It looks like you're inside an interrupt to me and you're calling HAL_Delay(1) which is blocking.
If you feel a post has answered your question, please click "Accept as Solution".

I have this 50us interrupt which comes from the interrupt in order to change the pwm duty cycle using HAL_TIM_SET_COMPARE, and inside my main I have this HAL_Delay(1) which I use to control the LCD. It works fine when I do not insert that HAL_TIM_SET_COMPARE function, but as soon as I add it, the code gets stuck exactly inside that HAL_Delay(1).

50us interrupt? You simply exhaust the processor.

JW