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 @   JerryZheng2020  阅读(1708)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示