Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

µC/OS-III provides a number of services to manage timers as summarized in the table below.

Anchor
Table - Timer API summary
Table - Timer API summary

Panel
borderWidth0
titleTable - Timer API summary


Function NameOperation
OSTmrCreate()Create and specify the operating mode of the timer.
OSTmrDel()Delete a timer.
OSTmrRemainGet()Obtain the remaining time left before the timer expires.
OSTmrStart()Start (or restart) a timer.
OSTmrStateGet()Obtain the current state of a timer.
OSTmrStop()Stop the countdown process of a timer.



A timer needs to be created before it can be used. You create a timer by calling OSTmrCreate() and  specify a number of arguments to this function based on how the timer is to operate. Once the timer operation is specified, its operating mode cannot be changed unless the timer is deleted and recreated. The function prototype for OSTmrCreate() is shown below as a quick reference:


Code Block
languagecpp
void  OSTmrCreate (OS_TMR              *p_tmr,            /* Pointer to timer     */
                   CPU_CHAR            *p_name,           /* Name of timer, ASCII */
                   OS_TICK              dly,              /* Initial delay        */
                   OS_TICK              period,           /* Repeat period        */
                   OS_OPT               opt,              /* Options              */
                   OS_TMR_CALLBACK_PTR  p_callback,       /* Fnct to call at 0    */
                   void                *p_callback_arg,   /* Arg. to callback     */
                   OS_ERR              *p_err)


Once created, a timer can be started (or restarted) and stopped as often as is necessary. Timers can be created to operate in one of three modes: One-shot, Periodic (no initial delay), and Periodic (with initial delay).

...

You terminate the countdown process of a timer (before it reaches zero) by calling OSTmrStop(). In this case, you can specify that the callback function be called or not.

Anchor
Figure - One Shot Timers (dly > 0, period == 0)
Figure - One Shot Timers (dly > 0, period == 0)

Panel
borderWidth0
titleFigure - One Shot Timers (dly > 0, period == 0)

Image Added


As shown in the figure below, a one-shot timer can be retriggered by calling OSTmrStart() before the timer reaches zero. This feature can be used to implement watchdogs and similar safeguards.

Anchor
Figure - Retriggering a One Shot Timer
Figure - Retriggering a One Shot Timer

Panel
borderWidth0
titleFigure - Retriggering a One Shot Timer

Image Added


Periodic (no initial delay)

As indicated in the in the figure below, timers can be configured for periodic mode. When the countdown expires, the callback function is called, the timer is automatically reloaded, and the process is repeated. If specifying a delay of zero (i.e., dly == 0) when the timer is created and, when started, the timer immediately uses the “period” as the reload value. You can call OSTmrStart() at any point in the countdown to restart the process.

Anchor
Figure - Periodic Timers (dly == 0, period > 0)
Figure - Periodic Timers (dly == 0, period > 0)

Panel
borderWidth0
titleFigure - Periodic Timers (dly == 0, period > 0)

Image Added


Periodic (with initial delay)

As shown in the figure below, timers can be configured for periodic mode with an initial delay that is different than its period. The first countdown count comes from the “dly” argument passed in the OSTmrCreate() call, and the reload value is the “period”. You can call OSTmrStart() to restart the process including the initial delay.

Anchor
Figure - Periodic Timers (dly > 0, period > 0)
Figure - Periodic Timers (dly > 0, period > 0)

Panel
borderWidth0
titleFigure - Periodic Timers (dly > 0, period > 0)

Image Added