FreeRTOS软件定时器
API函数
//创建
TimerHandle_t xTimerCreate( const char * const pcTimerName,
const TickType_t xTimerPeriodInTicks,
const UBaseType_t uxAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction )
//开始
#define xTimerStart( xTimer, xTicksToWait ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_START,
( xTaskGetTickCount() ), NULL, ( xTicksToWait ) )
BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID,
const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken,
const TickType_t xTicksToWait )
//停止
#define xTimerStop( xTimer, xTicksToWait ) xTimerGenericCommand( ( xTimer ), tmrCOMMAND_STOP,
0U, NULL, ( xTicksToWait ) )
举例
void start_task(void *pvParameters)
{
//创建软件周期定时器。周期1s(1000个时钟节拍),周期模式
AutoReloadTimer_Handle=xTimerCreate((const char* )"AutoReloadTimer",
(TickType_t )1000,
(UBaseType_t )pdTRUE,
(void* )1,
(TimerCallbackFunction_t)AutoReloadCallback);
//创建单次定时器。周期2s(2000个时钟节拍),单次模式
OneShotTimer_Handle=xTimerCreate((const char* )"OneShotTimer",
(TickType_t )2000,
(UBaseType_t )pdFALSE,
(void* )2,
(TimerCallbackFunction_t)OneShotCallback);
}
//周期定时器的回调函数
void AutoReloadCallback(TimerHandle_t xTimer)
{
static u8 tmr1_num=0;
tmr1_num++; //周期定时器执行次数加1
printf("AutoReloadCallback %d\r\n", tmr1_num);
}
//单次定时器的回调函数
void OneShotCallback(TimerHandle_t xTimer)
{
static u8 tmr2_num = 0;
tmr2_num++; //单次定时器执行次数加1
printf("OneShotCallback %d\r\n", tmr2_num);
}
void timercontrol_task(void *pvParameters)
{
while(1)
{
key = KEY_Scan(0);
switch(key)
{
case WKUP_PRES: //当key_up按下的话打开周期定时器
xTimerStart(AutoReloadTimer_Handle,0); //开启周期定时器
printf("开启定时器1\r\n");
break;
case KEY0_PRES: //当key0按下的话打开单次定时器
xTimerStart(OneShotTimer_Handle,0); //开启单次定时器
printf("开启定时器2\r\n");
break;
case KEY1_PRES: //当key1按下话就关闭定时器
xTimerStop(AutoReloadTimer_Handle,0); //关闭周期定时器
xTimerStop(OneShotTimer_Handle,0); //关闭单次定时器
printf("关闭定时器1和2\r\n");
break;
}
vTaskDelay(10); //延时10ms,也就是10个时钟节拍
}
}
实验现象