Skip to main content
Explorer II
November 15, 2023
Solved

Delay with timers(Non blocking delay)

  • November 15, 2023
  • 2 replies
  • 4087 views

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

    This topic has been closed for replies.
    Best answer by GwenoleB

    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é

     

    2 replies

    GwenoleBAnswer
    ST Employee
    November 15, 2023

    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é

     

    Explorer II
    November 28, 2023

    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