14.2.3.3.1 Application Timer
Application timers can be used to schedule periodic tasks or execute specific actions after a certain delay. A timer created with HAL API may hit only once or fire repeatedly with a given interval.
To start a timer call the HAL_StartAppTimer(HAL_AppTimer_t* params) function, where params
represents function's parameters. In the parameters, specify the timer interval, timer mode (TIMER_REPEAT_MODE
or TIMER_ONE_SHOT_MODE
), and the callback function, which will be called each time the timer fires.
To stop the timer, call the HAL_StopAppTimer(HAL_AppTimer_t* params) with the pointer to the same instance of the ::HAL_AppTimer_t type that was used to configure the timer. This will stop a periodic timer and prevent execution of a one-shot timer if it still has not fired.
Consider the following example:
//Callback definition void timerFired() { //Do whatever you need } //Define the parameters instance in the outer scope HAL_AppTimer_t timer; ... timer.interval = 5000; //Timer interval in milliseconds timer.mode = TIMER_REPEAT_MODE; timer.callback = timerFired; HAL_StartAppTimer(&timer);
When a timer fires, the MCU passes control to the callback function registered for the timer and then continues execution from the place it stopped when the timer fired.