STM32---FreeRTOS软件定时器

一、简介

1、定时器介绍

2、软件定时器优缺点

3、FreeRTOS定时器特点

4、软件定时器相关配置

5、软件定时器的状态

6、单次定时器和周期定时器

1、举例

2、软件定时器状态转换图

二、 FreeRTOS软件定时器相关API函数

三、实验

1、代码

main.c

cs 复制代码
#include "stm32f10x.h"
#include "FreeRTOS.h"
#include "task.h"
#include "freertos_demo.h"
#include "Delay.h"
#include "sys.h"
#include "usart.h"
#include "LED.h"
#include "Key.h"

 
 int main(void)
 {	
	 
	 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组 4 
	 uart_init(115200);	 
	 delay_init();
	 Key_Init();
	 LED_Init();
	 
	    // 创建任务
   FrrrRTOS_Demo();
		 	  
}

freertos_demo.c

cs 复制代码
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "LED.h"
#include "Key.h"
#include "usart.h"
#include "delay.h"



void Timer1Callback( TimerHandle_t pxTimer );
void Timer2Callback( TimerHandle_t pxTimer );


/******************************************************************任务配置****************************************************/
//任务优先级
#define START_TASK_PRIO					1
//任务堆栈大小	
#define START_TASK_STACK_SIZE 	128  
//任务句柄
TaskHandle_t StartTask_Handler;
//任务函数
void start_task(void *pvParameters);


//任务优先级
#define TASK1_PRIO							2
//任务堆栈大小	
#define TASK1_STACK_SIZE 				128  
//任务句柄
TaskHandle_t Task1_Handler;
//任务函数
void task1(void *pvParameters);
 
//任务优先级



/******************************************************************任务函数****************************************************/
TimerHandle_t	Timer1_handle = 0;													//单次定时器
TimerHandle_t	Timer2_handle = 0;													//周期定时器

void FrrrRTOS_Demo(void)
{
			 //创建开始任务
		xTaskCreate((TaskFunction_t )start_task,            			//任务函数
                ( char*         )"start_task",          			//任务名称
                (uint16_t       )START_TASK_STACK_SIZE, 			//任务堆栈大小
                (void*          )NULL,                  			//传递给任务函数的参数
                (UBaseType_t    )START_TASK_PRIO,       			//任务优先级
                (TaskHandle_t*  )&StartTask_Handler);   			//任务句柄 
	  // 启动任务调度
		vTaskStartScheduler();
	 
}


 void start_task(void *pvParameters)
{
	  taskENTER_CRITICAL();           //进入临界区
	
	
	/**创建单次定时器**/
	
	  Timer1_handle  = xTimerCreate( "timer1", 1000,pdFALSE,(void *)1,Timer1Callback);
																		
	/**创建周期定时器**/		

	  Timer2_handle  = xTimerCreate( "timer2", 1000,pdTRUE,(void *)1,Timer2Callback);	
																
		


    //创建1任务
    xTaskCreate((TaskFunction_t )task1,     	
                (const char*    )"task1",   	
                (uint16_t       )TASK1_STACK_SIZE, 
                (void*          )NULL,				
                (UBaseType_t    )TASK1_PRIO,	
                (TaskHandle_t*  )&Task1_Handler); 
  
		
    vTaskDelete(NULL); 							//删除开始任务
    taskEXIT_CRITICAL();            //退出临界区
}


//1按键扫描并控制任务定时器
void task1(void *pvParameters)
{
	uint8_t				key = 0;
	
	while(1)
	{
		key = Key_GetNum();
		if(key ==2)
		{
			printf("开启定时器\r\n");
			xTimerStart(Timer1_handle,portMAX_DELAY);
			xTimerStart(Timer2_handle,portMAX_DELAY);
		}else if(key ==3){
			printf("关闭定时器\r\n");
			xTimerStop(Timer1_handle,portMAX_DELAY);
			xTimerStop(Timer2_handle,portMAX_DELAY);
		
		}
		vTaskDelay(10);
	}
}

//timer1超时回调函数
void Timer1Callback( TimerHandle_t pxTimer )
{
		static uint32_t	timer = 0;
		printf("timer1的运行次数%d\r\n",++timer);
}

//timer2超时回调函数
void Timer2Callback( TimerHandle_t pxTimer )
{
		static uint32_t	timer = 0;
		printf("timer2的运行次数%d\r\n",++timer);	

}

key.c

cs 复制代码
#include "stm32f10x.h"                  // Device header
#include "FreeRTOS.h"
#include "task.h"
#include "usart.h"
#include "Delay.h"
 
/**
  * 函    数:按键初始化
  * 参    数:无
  * 返 回 值:无
	* 按键:PB4/PB12/PB14
  */
void Key_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	/*开启时钟*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);		//开启GPIOB的时钟
 
	/*GPIO初始化*/
	GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin 	= GPIO_Pin_4 | GPIO_Pin_12 | GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);						
}
 
 
/**
  * 函    数:按键获取键码
  * 参    数:无
  * 返 回 值:按下按键的键码值,范围:0~3,返回0代表没有按键按下
  * 注意事项:此函数是阻塞式操作,当按键按住不放时,函数会卡住,直到按键松手
  */
uint8_t Key_GetNum(void)
{
	uint8_t KeyNum = 0;																				//定义变量,默认键码值为0
	
	if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0)			  //读PB4输入寄存器的状态,如果为0,则代表按键1按下
	{
		KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4);
		delay_xms(20);																					//延时消抖
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0);	//等待按键松手
		delay_xms(20);																					//延时消抖
		KeyNum = 1;																							//置键码为1
	}
	
	if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0)			
	{
		KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12);
		delay_xms(20);											
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0);	
		delay_xms(20);									
		KeyNum = 2;											
	}
	
	if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0)			
	{
		KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
		delay_xms(20);											
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0);	
		delay_xms(20);									
		KeyNum = 3;											
	}
	
	return KeyNum;																						//返回键码值,如果没有按键按下,所有if都不成立,则键码为默认值0
}
 
 
 
 

2、实验解析

四、重点

使用函数前,需要先将宏置1(默认是1)

#define configUSE_TIMERS 1

创建软件定时函数:

TimerHandle_t xTimerCreate( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload,

void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction );

开启软件定时器函数:

BaseType_t xTimerStart( TimerHandle_t xTimer,

const TickType_t xTicksToWait );

停止软件定时器函数:

BaseType_t xTimerStop( TimerHandle_t xTimer,

const TickType_t xTicksToWait);

复位软件定时器函数:

BaseType_t xTimerReset( TimerHandle_t xTimer,

const TickType_t xTicksToWait);

更改软件定时器超时时间API函数 :

BaseType_t xTimerChangePeriod( TimerHandle_t xTimer,

const TickType_t xNewPeriod, const TickType_t xTicksToWait);

相关推荐
智购科技自动售卖机厂家2 小时前
从STM32到RK3588——自动售货机嵌入式主控方案的架构演进~YH
stm32·嵌入式硬件·物联网·架构·lua·零售·symfony
振南的单片机世界3 小时前
2MHz/50MHz玄机:GPIO速度档选错,信号直接“烂尾”
arm开发·stm32·单片机·嵌入式硬件
Run_Teenage3 小时前
嵌入式:理解STM32文档和社区生态,工具链的选择与安装
stm32·单片机·嵌入式硬件
ACP广源盛139246256733 小时前
DeepSeek‑V4‑Flash 公测@ACP#昇腾 950 国产算力组合落地,国产 PCIe 交换芯片 IX9104 有哪些硬件机会
大数据·数据库·人工智能·分布式·单片机·嵌入式硬件·microsoft
铅笔小新z3 小时前
【stm32】RCC 与时钟树
stm32·单片机·嵌入式硬件
时空自由民.4 小时前
STM32 IIC HAL库,LL库
stm32·单片机·嵌入式硬件
txh05074 小时前
不用变压器也能把220V交流电变为5V直流电?
单片机·嵌入式硬件
辰哥单片机设计5 小时前
STM32智能门禁系统(语音版)
stm32·单片机·嵌入式硬件
映翰通朱工7 小时前
Windows 上位机 + EC3320:USB 转 CAN 通信实测(从接线到 SSH 收包)
windows·stm32·ssh
Industio_触觉智能7 小时前
瑞芯微ISP内核驱动深度解析|RKISP39从架构到Probe初始化全流程(上篇)
linux·嵌入式硬件·硬件架构·摄像头·isp·瑞芯微·rk3576