软件定时器
1. 定时器使能
#define OS_CFG_TMR_EN 1u /* Enable (1) or Disable (0) code generation for TIMERS */
#define OS_CFG_TMR_DEL_EN 1u /* Enable (1) or Disable (0) code generation for OSTmrDel() */
2. 定时器的工作频率
#define OS_CFG_TMR_TASK_RATE_HZ 10u /* Rate for timers (10 Hz Typ.)
OSTmrCreate
Description
OSTmrCreate() allows the user to create a software timer. The timer can be configured to run
continuously (opt set to OS_TMR_OPT_PERIODIC), or only once (opt set to OS_TMR_OPT_ONE_SHOT).
When the timer counts down to 0 (from the value specified in period), an optional “callback”
function can be executed. The callback can be used to signal a task that the timer expired, or
perform any other function. However, it is recommended to keep the callback function as short
as possible.
The timer is created in the “stop” mode and therefore the user must call OSTmrStart() to
actually start the timer. If configuring the timer for ONE-SHOT mode, and the timer expires,
you need to call OSTmrStart() to retrigger the timer, call OSTmrDel() to delete the timer if it is
not necessary to retrigger it, or not use the timer anymore. Note: you can use the callback
function to delete the timer if using the ONE-SHOT mode.
void OSTmrCreate (OS_TMR *p_tmr,
CPU_CHAR *p_name,
OS_TICK dly,
OS_TICK period,
OS_OPT opt,
OS_TMR_CALLBACK_PTR p_callback,
void *p_callback_arg,
OS_ERR *p_err)
Arguments
p_tmr //定义一个定时器
is a pointer to the timer-control block of the desired timer. It is assumed that storage for
the timer will be allocated in the application. In other words, you should declare a
“global” variable as follows, and pass a pointer to this variable to OSTmrCreate():
OS_TMR MyTmr;
p_name // 定时器起名
is a pointer to an ASCII string (NUL terminated) used to assign a name to the timer. The
name can be displayed by debuggers or µC/Probe.
dly //初始延时的值(首次花费时间)
specifies the initial delay (specified in timer tick units) used by the timer (see drawing
above). If the timer is configured for ONE-SHOT mode, this is the timeout used. If the
timer is configured for PERIODIC mode, this is the timeout to wait before the timer
enters periodic mode. The units of this time depends on how often the user will call
OSTmrSignal() (see OSTimeTick()). If OSTmrSignal() is called every 1/10 of a second (i.e.,
OS_CFG_TMR_TASK_RATE_HZ set to 10), dly specifies the number of 1/10 of a second before
the delay expires.
period //周期性的时间
specifies the period repeated by the timer if configured for PERIODIC mode. You would
set the “period” to 0 when using ONE-SHOT mode. The units of time depend on how
often OSTmrSignal() is called. If OSTmrSignal() is called every 1/10 of a second (i.e.,
OS_CFG_TMR_TASK_RATE_HZ set to 10), the period specifies the number of 1/10 of a second
before the timer repeats.
opt // 选择是单次还是周期性
is used to specify whether the timer is to be ONE-SHOT or PERIODIC:
OS_OPT_TMR_ONE_SHOT specifies ONE-SHOT mode
OS_OPT_TMR_PERIODIC specifies PERIODIC mode
p_callback // 回调函数的指针
is a pointer to a function that will execute when the timer expires (ONE-SHOT mode), or
every time the period expires (PERIODIC mode). A NULL pointer indicates that no action
is to be performed upon timer expiration. The callback function must be declared as
follows:
void MyCallback (OS_TMR *p_tmr, void *p_arg);
When called, the callback will be passed the pointer to the timer as well as an argument (
p_callback_arg), which can be used to indicate to the callback what to do. Note that the
user is allowed to call all of the timer related functions (i.e., OSTmrCreate(), OSTmrDel(),
OSTmrStateGet(), OSTmrRemainGet(), OSTmrStart(), and OSTmrStop()) from the callback
function.
Do not make blocking calls within callback functions.
p_callback_arg
is an argument passed to the callback function when the timer expires (ONE-SHOT
mode), or every time the period expires (PERIODIC mode). The pointer is declared as a “
void *” so it can point to any data.
OSTmrStart
Description
OSTmrStart() allows the user to start (or restart) the countdown process of a timer. The timer
must have previously been created.
CPU_BOOLEAN OSTmrStart (OS_TMR *p_tmr,
OS_ERR *p_err)
Arguments
p_tmr
is a pointer to the timer to start (or restart).
OSTmrStop
Description
OSTmrStop() allows the user to stop a timer. The user may execute the callback function of the
timer when it is stopped, and pass this callback function a different argument than was
specified when the timer was started. This allows the callback function to know that the timer
was stopped since the callback argument can be set to indicate this (this is application
specific). If the timer is already stopped, the callback function is not called.
CPU_BOOLEAN OSTmrStop (OS_TMR *p_tmr,
OS_OPT opt,
void *p_callback_arg,
OS_ERR *p_err)
Arguments
p_tmr
is a pointer to the timer control block of the desired timer.
opt
is used to specify options:
OS_OPT_TMR_NONE
No option
OS_OPT_TMR_CALLBACK
Run the callback function with the argument specified when the timer was created.
OS_OPT_TMR_CALLBACK_ARG
Run the callback function, but use the argument passed in OSTmrStop() instead of the
one specified when the task was created.
p_callback_arg
is a new argument to pass the callback functions (see options above).