cancel
Showing results for 
Search instead for 
Did you mean: 

Delay with timers(Non blocking delay)

sireevenkat1
Senior

Hi,

I need  microsec/millisec delay  in my project but I don't want to effect the process flow.I am using STM32G4.

I am using timer for microseconds delay below is the function.

void delay_us (uint16_t us)

{

__HAL_TIM_SET_COUNTER(&htim1,0); // set the counter value a 0

while (__HAL_TIM_GET_COUNTER(&htim1) <= us); // wait for the counter to reach the us input in the parameter

}
Above function is working fine but the problem is with while loop in the delay function

it is getting struck there for given usec time it is blocking the

remaining process for that time to execute.
How to develop non blocking delay function with timers without while loop.

I have to provide multiple delays in my project.

Can I use same timer with different channels for different delay requirement
Please suggest/share the resources if available.
Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
GwenoleB
ST Employee

Hello @sireevenkat1,

I recommend you to use Interrupt instead of polling mode.
By configuring correctly the ARR register of the TIMER and enable UDPATE_EVENT interrupt, the timer will raise a interrupt each time COUNTER register reaches ARR.
It allows your program to run while Timer is managing delay.

Please refer to AN4776 for more details on Timer time-base working. You can also have a look on example available on STM32_CubeG4.
Best Regards,

Gwénolé

 

View solution in original post

2 REPLIES 2
GwenoleB
ST Employee

Hello @sireevenkat1,

I recommend you to use Interrupt instead of polling mode.
By configuring correctly the ARR register of the TIMER and enable UDPATE_EVENT interrupt, the timer will raise a interrupt each time COUNTER register reaches ARR.
It allows your program to run while Timer is managing delay.

Please refer to AN4776 for more details on Timer time-base working. You can also have a look on example available on STM32_CubeG4.
Best Regards,

Gwénolé

 

sireevenkat1
Senior

Hi @GwenoleB ,

Can I use one timer for different delay's. In my project I need many number of delays like 50usec,750 usec,3 sec,2sec,6sec and 100usec etc in different places. I want to use single hardware timer with interrupt for these delays. Is it possible. If possible how can I start the timer at different delay places in the project. Can you please share any resource for this or some example. It will be helpful .

Thanks