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個時鐘節拍 }}
實驗現象