高级会员

- 积分
- 566
- 金钱
- 566
- 注册时间
- 2016-9-28
- 在线时间
- 158 小时
|
定义参数:
#define TIMER_ID 1
static xTimerHandle xTimers;
static void vTimerCallback( xTimerHandle pxTimer )
1. 创建定时器,是否要在创建之后启动定时器,可根据实现功能需要选择。 但启动定时器之前,必须创建定时器
static void AppTask_CreateTimer (void)
{
uint8_t x;
const TickType_t xTimerPeriodInTicks = 1000;
/* 创建定时器,如果在RTOS调度开始前初始化定时器,那么系统启动后将立即开始工作 */
xTimers = xTimerCreate( "Timer", /* 定时器名字 */
xTimerPeriodInTicks, /* 定时器周期 The timer period in ticks. */
pdTRUE, /* 周期性 */
( void * ) TIMER_ID, /* 定时器ID */
vTimerCallback /* 定时器回调函数 */
);
if( xTimers == NULL )
{
printf( "定时器没有创建成功 \r\n");
}
}
2. 启动定时器
static void AppTask_StartTimer( void )
{
if( xTimerStart( xTimers, 0 ) != pdPASS )
{
printf( "start time: fail \r\n");
}
}
3. 停止定时器
static void AppTask_StopTimer( void )
{
if( xTimerStop( xTimers, 0 ) != pdPASS )
{
printf( "stop time: fail \r\n");
}
}
4. 定时callback函数
static void vTimerCallback( xTimerHandle pxTimer )
{
long lArrayIndex;
configASSERT( pxTimer );
/* 获取定时器ID */
lArrayIndex = ( long ) pvTimerGetTimerID( pxTimer );
if( lArrayIndex == TIMER_ID )
{
printf(" Timer is running \r\n");
}
}
|
|