FreeRTOS(二)软件定时器

本文介绍了FreeRTOS的软件定时器,包括单次触发和自动重装载定时器的区别,以及如何创建、开启、关闭、分配ID、更改周期和重启定时器的详细步骤。
摘要由CSDN通过智能技术生成

FreeRTOS参考文档

一、单次触发和自动重装载的软件定时器的区别

单次触发(one-shot)定时器启动后只会执行一次,不会自动重启。
自动重装载(auto-load)定时器会周期执行,执行完后会自动重启。

软件定时器两种状态:

  • 休眠(dormant)
  • 运行(running)

在这里插入图片描述

二、创建软件定时器

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

三、开启定时器

BaseType_t xTimerStart( TimerHandle_t xTimer, TickType_t xTicksToWait );

四、关闭定时器

BaseType_t xTimerStop( TimerHandle_t xTimer, TickType_t xTicksToWait );

#include "rtos_test.h"

//one-shot & periodic
u32 ulCallCount=0;
static void prvOneShotTimerCallback( TimerHandle_t xTimer )
{
   
    TickType_t xTimeNow;
    /* Obtain the current tick count. */
    xTimeNow = xTaskGetTickCount();
    /* Output a string to show the time at which the callback was executed. */
    vPrintStringAndNumber( "One-shot timer callback executing :%d", xTimeNow );
    /* File scope variable. */
    ulCallCount++;
}


static void prvAutoReloadTimerCallback( TimerHandle_t xTimer )
{
   
    TickType_t xTimeNow;
    /* Obtain the current tick count. */
    xTimeNow = xTaskGetTickCount();
    /* Output a string to show the time at which the callback was executed. */
    vPrintStringAndNumber( "Auto-reload timer callback executing :%d", xTimeNow );
    ulCallCount++;
}

/* The periods assigned to the one-shot and auto-reload timers are 3.333 second and half a
second respectively. */
#define mainONE_SHOT_TIMER_PERIOD  pdMS_TO_TICKS( 3333 )
#define mainAUTO_RELOAD_TIMER_PERIOD pdMS_TO_TICKS( 500 )
int main( void
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值