上下文
守护任务
优先级 <b>configTIMER_TASK_PRIORITY</b>
定时器命令队列长度 <b>configTIMER_QUEUE_LENGTH</b><br>
守护任务调度
处理命令:从命令队列里取出命令、处理<br>
执行定时器的回调函数
回调函数
void <b><font color="#64b5f6">ATimerCallback</font></b>( TimerHandle_t <b>xTimer </b>)
函数
创建
TimerHandle_t <b><font color="#64b5f6">xTimerCreate</font></b>( const char * const <b>pcTimerName</b>, // 定时器名字<br> const TickType_t <b>xTimerPeriodInTicks</b>, // 周期, 以Tick为单位<br> const UBaseType_t <b>uxAutoReload</b>, //类型, <b>pdTRUE </b>--> 自动加载, <b>pdFALSE </b>--> 一次性<br> void * const <b>pvTimerID</b>, // 回调函数可以使用此参数, 比如分辨是哪个定时器<br> TimerCallbackFunction_t <b>pxCallbackFunction </b>); //回调函数<br>
TimerHandle_t <b><font color="#64b5f6">xTimerCreateStatic</font></b>(const char * const <b>pcTimerName</b>, <br> TickType_t <b>xTimerPeriodInTicks</b>,<br> UBaseType_t <b>uxAutoReload</b>,<br> void * <b>pvTimerID</b>,<br> TimerCallbackFunction_t <b>pxCallbackFunction</b>,<br> StaticTimer_t *<b>pxTimerBuffer </b>);<br>
回调函数类型 void <b><font color="#64b5f6">ATimerCallback</font></b>( TimerHandle_t <b>xTimer </b>); typedef void (* TimerCallbackFunction_t)( TimerHandle_t xTimer );
删除
BaseType_t <b><font color="#64b5f6">xTimerDelete</font></b>( TimerHandle_t <b>xTimer</b>, // 要删除哪个定时器 TickType_t <b>xTicksToWait</b> ); // 超时时间
启动/停止<br>
启动
BaseType_t <b><font color="#64b5f6">xTimerStart</font></b>( TimerHandle_t <b>xTimer</b>, TickType_t <b>xTicksToWait </b>);
BaseType_t <b><font color="#64b5f6">xTimerStartFromISR</font></b>( TimerHandle_t <b>xTimer</b>,<br> BaseType_t *<b>pxHigherPriorityTaskWoken </b>); <br>
停止
BaseType_t <b><font color="#64b5f6">xTimerStop</font></b>( TimerHandle_t <b>xTimer</b>, TickType_t <b>xTicksToWait </b>);
BaseType_t <b><font color="#64b5f6">xTimerStopFromISR</font></b>( TimerHandle_t <b>xTimer</b>,<br> BaseType_t *<b>pxHigherPriorityTaskWoken </b>);
参数 <b><font color="#7b1fa2">pxHigherPriorityTaskWoken</font></b>: 向队列发出命令使得守护任务被唤醒,<br> 如果守护任务的优先级比当前任务的高,<br> 则"<b>*pxHigherPriorityTaskWoken = pdTRUE",</b><br> 表示需要进行任务调度<br>
复位
BaseType_t <b><font color="#64b5f6">xTimerReset</font></b>( TimerHandle_t <b>xTimer</b>, TickType_t <b>xTicksToWait </b>);
BaseType_t <b><font color="#64b5f6">xTimerResetFromISR</font></b>( TimerHandle_t <b>xTimer</b>,<br> BaseType_t *<b>pxHigherPriorityTaskWoken </b>);
修改周期
BaseType_t <b><font color="#64b5f6">xTimerChangePeriod</font></b>( TimerHandle_t <b>xTimer</b>,<br> TickType_t <b>xNewPeriod</b>,<br> TickType_t <b>xTicksToWait </b>);
BaseType_t <b><font color="#64b5f6">xTimerChangePeriodFromISR</font></b>( TimerHandle_t <b>xTimer</b>,<br> TickType_t <b>xNewPeriod</b>,<br> BaseType_t *<b>pxHigherPriorityTaskWoken </b>);
定时器ID
void *<b><font color="#64b5f6">pvTimerGetTimerID</font></b>( TimerHandle_t <b>xTimer </b>); (获得定时器的ID)
void <b><font color="#64b5f6">vTimerSetTimerID</font></b>( TimerHandle_t <b>xTimer</b>, (设置定时器的ID) void *<b>pvNewID </b>) //新ID
使用
一般使用
/* 1. 工程中 */<br>添加 timer.c<br><br>/* 2. 配置文件FreeRTOSConfig.h中 */<br><span class="tag">##define</span> configUSE_TIMERS 1 /* 使能定时器 */<br><span class="tag">##define</span> configTIMER_TASK_PRIORITY 31 /* 守护任务的优先级, 尽可能高一些 */<br><span class="tag">##define</span> configTIMER_QUEUE_LENGTH 5 /* 命令队列长度 */<br><span class="tag">##define</span> configTIMER_TASK_STACK_DEPTH 32 /* 守护任务的栈大小 */<br><br>/* 3. 源码中 */<br><span class="tag">##include</span> "timers.h"
消除抖动