三.UCOSII中软件定时器的配置步骤
1.在os_cfg.h文件夹中使能条件编译
OS_TMR_EN=1u
2.创建定时器:
OS_TMR * tmr1; //软件定时器1
OS_TMR * tmr2; //软件定时器2
OS_TMR * tmr3; //软件定时器3
3.创建回调函数:
//软件定时器1的回调函数
//每100ms执行一次
void tmr1_callback(OS_TMR *ptmr,void *p_arg)
{
.....................................................
}
//软件定时器2的回调函数,200ms溢出一次
void tmr2_callback(OS_TMR *ptmr,void *p_arg)
{
.....................................................
}
//软件定时器3的回调函数,100ms溢出一次
void tmr3_callback(OS_TMR *ptmr,void *p_arg)
{
.....................................................
}
4.创建定时器:
OSTmrCreate()函数原型:
/* Description: This function is called by your application code to create a timer.
*
* Arguments : dly Initial delay.
* 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 first timeout to wait for
* before the timer starts entering periodic mode
*
* period The 'period' being repeated for the timer.
* If you specified 'OS_TMR_OPT_PERIODIC' as an option, when the timer expires, it will
* automatically restart with the same period.
*
* opt Specifies either:
* OS_TMR_OPT_ONE_SHOT The timer counts down only once
* OS_TMR_OPT_PERIODIC The timer counts down and then reloads itself
*
* callback Is a pointer to a callback function that will be called when the timer expires. The
* callback function must be declared as follows:
*
* void MyCallback (OS_TMR *ptmr, void *p_arg);
*
* callback_arg Is an argument (a pointer) that is passed to the callback function when it is called.
*
* pname Is a pointer to an ASCII string that is used to name the timer. Names are useful for
* debugging.
*
* perr Is a pointer to an error code. '*perr' will contain one of the following:
* OS_ERR_NONE
* OS_ERR_TMR_INVALID_DLY you specified an invalid delay
* OS_ERR_TMR_INVALID_PERIOD you specified an invalid period
* OS_ERR_TMR_INVALID_OPT you specified an invalid option
* OS_ERR_TMR_ISR if the call was made from an ISR
* OS_ERR_TMR_NON_AVAIL if there are no free timers from the timer pool
*/
#if OS_TMR_EN > 0u //条件编译使能
OS_TMR *OSTmrCreate (INT32U dly, //单次定时延时时长,或循环定时初始延时时长
INT32U period, //周期定时延时时长
INT8U opt, //定时模式选择:单次/循环
OS_TMR_CALLBACK callback,//回调函数
void *callback_arg,
INT8U *pname, //定时器名称
INT8U *perr) //返回错误信息指针
{
OS_TMR *ptmr;
#ifdef OS_SAFETY_CRITICAL
if (perr == (INT8U *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
}
#endif
#ifdef OS_SAFETY_CRITICAL_IEC61508
if (OSSafetyCriticalStartFlag == OS_TRUE) {
OS_SAFETY_CRITICAL_EXCEPTION();
}
#endif
#if OS_ARG_CHK_EN > 0u
switch (opt) { //Validate arguments
case OS_TMR_OPT_PERIODIC:
if (period == 0u) {
*perr = OS_ERR_TMR_INVALID_PERIOD;
return ((OS_TMR *)0);
}
break;
case OS_TMR_OPT_ONE_SHOT:
if (dly == 0u) {
*perr = OS_ERR_TMR_INVALID_DLY;
return ((OS_TMR *)0);
}
break;
default:
*perr = OS_ERR_TMR_INVALID_OPT;
return ((OS_TMR *)0);
}
#endif
if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
*perr = OS_ERR_TMR_ISR;
return ((OS_TMR *)0);
}
OSSchedLock();
ptmr = OSTmr_Alloc(); /* Obtain a timer from the free pool */
if (ptmr == (OS_TMR *)0) {
OSSchedUnlock();
*perr = OS_ERR_TMR_NON_AVAIL;
return ((OS_TMR *)0);
}
ptmr->OSTmrState = OS_TMR_STATE_STOPPED; /* Indicate that timer is not running yet */
ptmr->OSTmrDly = dly;
ptmr->OSTmrPeriod = period;
ptmr->OSTmrOpt = opt;
ptmr->OSTmrCallback = callback;
ptmr->OSTmrCallbackArg = callback_arg;
#if OS_TMR_CFG_NAME_EN > 0u
ptmr->OSTmrName = pname;
#endif
OSSchedUnlock();
*perr = OS_ERR_NONE;
return (ptmr);
}
#endif
OSTmrCreate()函数调用:
tmr1=OSTmrCreate(10,10,OS_TMR_OPT_PERIODIC,(OS_TMR_CALLBACK)tmr1_callback,0,"tmr1",&err); //100ms执行一次,周期循环模式
tmr2=OSTmrCreate(10,20,OS_TMR_OPT_PERIODIC,(OS_TMR_CALLBACK)tmr2_callback,0,"tmr2",&err); //200ms执行一次,周期循环模式
tmr3=OSTmrCreate(10,10,OS_TMR_OPT_PERIODIC,(OS_TMR_CALLBACK)tmr3_callback,0,"tmr3",&err); //100ms执行一次,周期循环模式
|