FreeRTOS軟體定時器

來源:互聯網
上載者:User

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個時鐘節拍    }}

實驗現象

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.