UCOSIII---软件定时器

1、创建一个软件定时器:OSTmrCreate();
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)
/*
************************************************************************************************************************
*                                                   CREATE A TIMER 
*                                                  创建一个软件定时器
*
* Description: This function is called by your application code to create a timer. 
* 描述:这个函数被你的应用层代码调用,用来创建一个软件定时器
* Arguments  参数:
*              p_tmr           Is a pointer to a timer control block  
*                              一个指向软件定时器控制块的指针
*              p_name          Is a pointer to an ASCII string that is used to name the timer.  Names are useful for
*                              debugging.
*                               一个字符串指针用来命名这个软件定时器 在调试时用到
*              dly             Initial delay.
*                              初始延迟
*                              If the timer is configured for ONE-SHOT mode, this is the timeout used
*                              如果这个软件定时器被配置为ONE-SHOT mode(单次模式),这个值就是定时器的溢出值
*                              If the timer is configured for PERIODIC mode, this is the first timeout to wait for
*                              before the timer starts entering periodic mode
*                               如果这个定时器被配置为PERIODIC mode(循环模式),这个值就是软件定时器进入周期模式之前定时器第一次的溢出值
*              period          The 'period' being repeated for the timer.
*                              定时器周期模式的周期
*                              If you specified 'OS_OPT_TMR_PERIODIC' as an option, when the timer expires, it will
*                              automatically restart with the same period.
*                              如果你选择了周期模式,当定时时间到时,将会自动重新开始
*              opt             Specifies either:
*                                  OS_OPT_TMR_ONE_SHOT       The timer counts down only once 单次模式
*                                  OS_OPT_TMR_PERIODIC       The timer counts down and then reloads itself 周期模式
*              p_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 *p_tmr, void *p_arg);
*              p_callback_arg  Is an argument (a pointer) that is passed to the callback function when it is called.
*                              传递给回调函数的参数
*              p_err           Is a pointer to an error code.  '*p_err' will contain one of the following:
*                                 错误码
*                                 OS_ERR_NONE
*                                 OS_ERR_ILLEGAL_CREATE_RUN_TIME if you are trying to create the timer after you called
*                                                                  OSSafetyCriticalStart().
*                                 OS_ERR_OBJ_CREATED             if the timer has already been created
*                                 OS_ERR_OBJ_PTR_NULL            is 'p_tmr' is a NULL pointer
*                                 OS_ERR_OBJ_TYPE                if the object type is invalid
*                                 OS_ERR_OPT_INVALID             you specified an invalid option
*                                 OS_ERR_TMR_INVALID_DLY         you specified an invalid delay
*                                 OS_ERR_TMR_INVALID_PERIOD      you specified an invalid period
*                                 OS_ERR_TMR_ISR                 if the call was made from an ISR
* Returns    : none
* Note(s)    : 1) This function only creates the timer.  In other words, the timer is not started when created.  To
*                 start the timer, call OSTmrStart().
* 注意:这个函数仅仅是创建了一个软件定时器 换句话说 当创建软件定时器的时候它并没有启动。要启动一个软件定时器必须调用函数:OSTmrStart()
************************************************************************************************************************
*/
示例:
OS_TMR  OS_tmr1_100ms;                                                                     //软件定时器的声明
void  OS_tmr1Callback (OS_TMR *p_tmr, void *p_arg);                                         //回调函数的声明
//创建一个软件定时器
OSTmrCreate (     (OS_TMR               *)&OS_tmr1_100ms,                                 //软件定时器地址            
                 (CPU_CHAR             *)"OS_tmr1 time1",                                //软件定时器名字
                 (OS_TICK               )0,                                              //初始延迟
                 (OS_TICK               )100,                                            //周期 单位:时钟节拍 10*10ms=100ms
                 (OS_OPT                )OS_OPT_TMR_PERIODIC,                            //周期模式
                 (OS_TMR_CALLBACK_PTR   )&OS_tmr1Callback,                               //定时器的回调函数
                 (void                 *)0,                                              //传递给定时器的参数
                 (OS_ERR               *)&err);  
//开启软件定时器                 
OSTmrStart(&OS_tmr1_100ms,&err);
//软件定时器的回调函数
void  OS_tmr1Callback (OS_TMR *p_tmr, void *p_arg)
{
    printf("\r\n********************************\r\n");
}      

2、软件定时器的启动函数:OSTmrStart ()

创建好的软件定时器需要调用OSTmrStart()函数来启动软件定时器

函数原型:CPU_BOOLEAN  OSTmrStart (OS_TMR  *p_tmr,
                         OS_ERR  *p_err)

功能:This function is called by your application code to start a timer.   启动一个软件定时器。

参数:p_tmr:Is a pointer to an OS_TMR   定时器控制块指针

            p_err: Is a pointer to an error code.  '*p_err' will contain one of the following:  返回错误类型

                           OS_ERR_NONE
                           OS_ERR_OBJ_TYPE                       if 'p_tmr' is not pointing to a timer
                           OS_ERR_TMR_INVALID    
                           OS_ERR_TMR_INACTIVE              if the timer was not created
                           OS_ERR_TMR_INVALID_STATE   the timer is in an invalid state
                           OS_ERR_TMR_ISR                          if the call was made from an ISR

返回值:DEF_TRUE,执行成功  is the timer was started

                DEF_FALSE,执行失败。 if not or upon an error

注意事项:

1、创建前必须先申明一个定时器对象

2、一次性定时dly不能为0

3、周期定时period不能为0

4、创建完一个软件定时器之后,只有开启之后才会工作,但是不可以在中断中调用函数OSTmrStart()

5、os_cfg_app.h中配置定时器的节拍:

#define  OS_CFG_TMR_TASK_RATE_HZ         100u               /* Rate for timers (100 Hz Typ.)                          */

//配置定时器的时钟节拍是100Hz 即周期是10ms

 

实验现象:

从上图可以看到打印函数1s执行一次,和程序设计的是一致的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值