将本页加入收藏夹

TI-RTOS 定时器的使用

定时器 在TI-RTOS中属于内核的一部分,因此想了解它的使用还是要阅读Bios_User_Guide.pdf.

主要用到这么几个API, 加粗字体 更多的定义可以在 ..\packages\ti\sysbios\knl\Clock中查看。

#define Clock_construct ti_sysbios_knl_Clock_construct
#define Clock_create ti_sysbios_knl_Clock_create
#define Clock_handle ti_sysbios_knl_Clock_handle
#define Clock_struct ti_sysbios_knl_Clock_struct
#define Clock_Handle_label ti_sysbios_knl_Clock_Handle_label
#define Clock_Handle_name ti_sysbios_knl_Clock_Handle_name
#define Clock_Instance_init ti_sysbios_knl_Clock_Instance_init
#define Clock_Object_count ti_sysbios_knl_Clock_Object_count
#define Clock_Object_get ti_sysbios_knl_Clock_Object_get
#define Clock_Object_first ti_sysbios_knl_Clock_Object_first
#define Clock_Object_next ti_sysbios_knl_Clock_Object_next
#define Clock_Object_sizeof ti_sysbios_knl_Clock_Object_sizeof
#define Clock_Params_copy ti_sysbios_knl_Clock_Params_copy
#define Clock_Params_init ti_sysbios_knl_Clock_Params_init

 

工作模式可是单次溢出,也可以是反复溢出。Clock_create()第二个参数用来指定首次溢出的时间,单位应该还是10us, 如果想要反复的事件,则要在第三个参数中指明周期,赋值.period为0,表示单次,非0的值就会产生周期事件了。一旦溢出产生,则会执行一次溢出功能函数, 这个函数是Clock_create的第一个参数所指向的函数。它的工作原理示意图如下:

 

好了,我们老规矩,来演示一下它的应用。这里用定时器来控制灯,500ms状态改变一次。全部的代码如下:

/**************************************************************************************************
Filename:       timerDemoTask.c
Editor:         Tome @ newbit
Revised:        $Date: 2016-8-10 11:20:02 +0800  $
Revision:       $Revision: 00001 $

Description:    了解 TI-RTOS的使用之,Timer
History:       
Notes:          要了解到这部分的接口,可阅读TI文档
                1. TI-RTOS 2.20  User's Guide.pdf
                2. Bios User Guide.pdf

 硬件平台  CC1130_LAUNCHPAD Rev1.3   

**************************************************************************************************/

 

/**************************************************************************************************
// INCLUDES
**************************************************************************************************/
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Error.h> //新加

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Clock.h>       // 新加

/* TI-RTOS Header files */
#include <ti/drivers/PIN.h>

 


#include "board.h"


/**************************************************************************************************
// TYPEDEF
**************************************************************************************************/

 

/**************************************************************************************************
// CONSTANTS
**************************************************************************************************/
#define TASKSTACKSIZE     768


/**************************************************************************************************
// LOCAL VERIABLE
**************************************************************************************************/
Task_Struct timerDemoTaskStruct;
Char timerDemoTaskStack[TASKSTACKSIZE];               // 本任务的栈空间,静态分配

PIN_Handle ledPinHandle;                                // 将LED操作句柄作为全局变量

/* Global memory storage for a PIN_Config table */
static PIN_State ledPinState;


/*
 * Application timerDemo pin configuration table:
 *   - All timerDemos board timerDemos are off.
 */
PIN_Config ledPinTable[] = {
    Board_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    Board_LED2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    PIN_TERMINATE
};


/**************************************************************************************************
// FUNCTIONS DECLERATION
**************************************************************************************************/
Void timerDemoFxn(UArg arg0, UArg arg1);
void myHandler(xdc_UArg arg0);


/**************************************************************************************************
// FUNCTIONS
**************************************************************************************************/

 

/**************************************************************************************************
 * @fn      timerDemoTaskAdd
 *
 * @brief  
 *
 * @param   void
 *
 * @return  void
 **************************************************************************************************/
void timerDemoTaskAdd(void)
{
  Task_Params taskParams;
 
  /* Construct BIOS objects */
    Task_Params_init(&taskParams); // 创建任务所要的参数,都设置为默认值
    taskParams.stackSize = TASKSTACKSIZE; // 栈空间
    taskParams.stack = &timerDemoTaskStack;     // 栈地址
    // 向bios 传递参数,建立控制灯的任务
    Task_construct(&timerDemoTaskStruct, (Task_FuncPtr)timerDemoFxn, &taskParams, NULL);
}

 


// 演示定时器的任务
Void timerDemoFxn(UArg arg0, UArg arg1)
{
 
  // 这里不是为了初始化,而是为了拿到操作的句柄 (handle)
  // 函数说明:Allocate one or more pins for a driver or an application.
  ledPinHandle = PIN_open(&ledPinState, ledPinTable);
  if(!ledPinHandle) {
    System_abort("Error initializing board LED pins\n");
  }   
 
  // 点亮第一个LED灯
  PIN_setOutputValue(ledPinHandle, Board_LED1, 1); 
 
 
  // 开始初始化一个 Timer
  Clock_Params clockParams;
  Clock_Handle myClock;
  Error_Block  eb;
 
  Error_init(&eb);
 
  Clock_Params_init(&clockParams);
  clockParams.period = 100 * 500;       // 500ms, 闪灯的频率为1hz
  clockParams.startFlag = TRUE;
  clockParams.arg  = (UArg)0x5555;
  myClock = Clock_create(myHandler, 5, &clockParams, &eb);
  if ( myClock == NULL )
  {
    System_abort("Clock create failed\n");
  }
  // Timer 配置结束
 
 
  // 该任务的主体只有休眠
  while(1)
  {
    // 任务休眠 1 秒,  1000000us, 下面函数的单位是10us
    Task_sleep(100000);
  }
 
}


/**************************************************************************************************
 * @fn      myHanlder
 *
 * @brief   定时器溢出时的执行函数
 *
 * @param   xdc_UArg arg0
 *
 * @return  void
 **************************************************************************************************/
void myHandler(xdc_UArg arg0)
{
 
  uint_t ledState;
    // 读LED 灯的状态,再设置为相反的状态
    ledState = PIN_getOutputValue(Board_LED2);
    ledState = !ledState;
    // 设置的操作需要 handle
    PIN_setOutputValue(ledPinHandle, Board_LED2, ledState);
   
}


/**************************************************************************************************
Copyright 2016 Newbit Studio. All rights reserved.
**************************************************************************************************/

 

posted on 2016-08-10 19:07  Newbit  阅读(2400)  评论(0编辑  收藏  举报

< p> Copyright ©2015 Newbit