FreeRTOS学习笔记4——Timer

FreeRTOS Software Timer Note

TOC

Timer回调函数原型

void ATimerCallback( TimerHandle_t xTimer );

Timer State

Auto Reload Timer

one-Short Timer

Timer command queue

Timer API

Timer create

TimerHandle_t xTimerCreate( const char * const pcTimerName,
                            TickType_t xTimerPeriodInTicks,
                            UBaseType_t uxAutoReload,
                            void * pvTimerID,
                            TimerCallbackFunction_t pxCallbackFunction );

Timer Start

BaseType_t xTimerStart( TimerHandle_t xTimer, TickType_t xTicksToWait );

Set Timer ID

void vTimerSetTimerID( const TimerHandle_t xTimer, void *pvNewID );

Get Timer ID

void *pvTimerGetTimerID( TimerHandle_t xTimer );

Change Timer Period

BaseType_t xTimerChangePeriod( TimerHandle_t xTimer,
                                TickType_t xNewTimerPeriodInTicks,
                                TickType_t xTicksToWait );

Reset Timer

BaseType_t xTimerReset( TimerHandle_t xTimer, TickType_t xTicksToWait );

Timer examples

Timer create example

/* Create the one shot timer, storing the handle to the created timer in xOneShotTimer. */
xOneShotTimer = xTimerCreate(
                /* Text name for the software timer - not used by FreeRTOS. */
                "OneShot",
                /* The software timer's period in ticks. */
                mainONE_SHOT_TIMER_PERIOD,
                /* Setting uxAutoRealod to pdFALSE creates a one-shot software timer. */
                pdFALSE,
                /* This example does not use the timer id. */
                0,
                /* The callback function to be used by the software timer being created. */
                prvOneShotTimerCallback );


                /* Create the auto-reload timer, storing the handle to the created timer in xAutoReloadTimer. */
xAutoReloadTimer = xTimerCreate(
                /* Text name for the software timer - not used by FreeRTOS. */
                "AutoReload",
                /* The software timer's period in ticks. */
                mainAUTO_RELOAD_TIMER_PERIOD,
                /* Setting uxAutoRealod to pdTRUE creates an auto-reload timer. */
                pdTRUE,
                /* This example does not use the timer id. */
                0,
                /* The callback function to be used by the software timer being created. */
                prvAutoReloadTimerCallback );

Timer start examples

/* Start the software timers, using a block time of 0 (no block time). The scheduler has
not been started yet so any block time specified here would be ignored anyway. */
xTimer1Started = xTimerStart( xOneShotTimer, 0 );
xTimer2Started = xTimerStart( xAutoReloadTimer, 0 );

Timer Set ID and Get ID

ulExecutionCount = ( uint32_t ) pvTimerGetTimerID( xTimer );
ulExecutionCount++;
vTimerSetTimerID( xTimer, ( void * ) ulExecutionCount );

// stop timer
xTimerStop( xTimer, 0 );

Timer Reset examples

/* Reset the software timer. If the backlight was previously off, then this call
will start the timer. If the backlight was previously on, then this call will
restart the timer. A real application may read key presses in an interrupt. If
this function was an interrupt service routine then xTimerResetFromISR() must be
used instead of xTimerReset(). */
xTimerReset( xBacklightTimer, xShortDelay );




posted @ 2021-01-06 13:24  JerryZheng2020  阅读(1625)  评论(0编辑  收藏  举报