STM32---FreeRTOS软件定时器

[复制链接]
发表于 2025-10-12 08:01:05 | 显示全部楼层 |阅读模式
一、简介

1、定时器先容

2、软件定时器优缺点


3、FreeRTOS定时器特点


4、软件定时器相干设置



5、软件定时器的状态 


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


1、举例 


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

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



三、实行


1、代码

main.c 
  1. #include "stm32f10x.h"
  2. #include "FreeRTOS.h"
  3. #include "task.h"
  4. #include "freertos_demo.h"
  5. #include "Delay.h"
  6. #include "sys.h"
  7. #include "usart.h"
  8. #include "LED.h"
  9. #include "Key.h"
  10. int main(void)
  11. {       
  12.          
  13.          NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组 4
  14.          uart_init(115200);         
  15.          delay_init();
  16.          Key_Init();
  17.          LED_Init();
  18.          
  19.             // 创建任务
  20.    FrrrRTOS_Demo();
  21.                            
  22. }
复制代码
freertos_demo.c
  1. #include "FreeRTOS.h"
  2. #include "task.h"
  3. #include "queue.h"
  4. #include "semphr.h"
  5. #include "event_groups.h"
  6. #include "LED.h"
  7. #include "Key.h"
  8. #include "usart.h"
  9. #include "delay.h"
  10. void Timer1Callback( TimerHandle_t pxTimer );
  11. void Timer2Callback( TimerHandle_t pxTimer );
  12. /******************************************************************任务配置****************************************************/
  13. //任务优先级
  14. #define START_TASK_PRIO                                        1
  15. //任务堆栈大小       
  16. #define START_TASK_STACK_SIZE         128  
  17. //任务句柄
  18. TaskHandle_t StartTask_Handler;
  19. //任务函数
  20. void start_task(void *pvParameters);
  21. //任务优先级
  22. #define TASK1_PRIO                                                        2
  23. //任务堆栈大小       
  24. #define TASK1_STACK_SIZE                                 128  
  25. //任务句柄
  26. TaskHandle_t Task1_Handler;
  27. //任务函数
  28. void task1(void *pvParameters);
  29. //任务优先级
  30. /******************************************************************任务函数****************************************************/
  31. TimerHandle_t        Timer1_handle = 0;                                                                                                        //单次定时器
  32. TimerHandle_t        Timer2_handle = 0;                                                                                                        //周期定时器
  33. void FrrrRTOS_Demo(void)
  34. {
  35.                          //创建开始任务
  36.                 xTaskCreate((TaskFunction_t )start_task,                                    //任务函数
  37.                 ( char*         )"start_task",                                  //任务名称
  38.                 (uint16_t       )START_TASK_STACK_SIZE,                         //任务堆栈大小
  39.                 (void*          )NULL,                                          //传递给任务函数的参数
  40.                 (UBaseType_t    )START_TASK_PRIO,                               //任务优先级
  41.                 (TaskHandle_t*  )&StartTask_Handler);                           //任务句柄
  42.           // 启动任务调度
  43.                 vTaskStartScheduler();
  44.          
  45. }
  46. void start_task(void *pvParameters)
  47. {
  48.           taskENTER_CRITICAL();           //进入临界区
  49.        
  50.        
  51.         /**创建单次定时器**/
  52.        
  53.           Timer1_handle  = xTimerCreate( "timer1", 1000,pdFALSE,(void *)1,Timer1Callback);
  54.                                                                                                                                                
  55.         /**创建周期定时器**/               
  56.           Timer2_handle  = xTimerCreate( "timer2", 1000,pdTRUE,(void *)1,Timer2Callback);       
  57.                                                                                                                                
  58.                
  59.     //创建1任务
  60.     xTaskCreate((TaskFunction_t )task1,            
  61.                 (const char*    )"task1",          
  62.                 (uint16_t       )TASK1_STACK_SIZE,
  63.                 (void*          )NULL,                               
  64.                 (UBaseType_t    )TASK1_PRIO,       
  65.                 (TaskHandle_t*  )&Task1_Handler);
  66.   
  67.                
  68.     vTaskDelete(NULL);                                                         //删除开始任务
  69.     taskEXIT_CRITICAL();            //退出临界区
  70. }
  71. //1按键扫描并控制任务定时器
  72. void task1(void *pvParameters)
  73. {
  74.         uint8_t                                key = 0;
  75.        
  76.         while(1)
  77.         {
  78.                 key = Key_GetNum();
  79.                 if(key ==2)
  80.                 {
  81.                         printf("开启定时器\r\n");
  82.                         xTimerStart(Timer1_handle,portMAX_DELAY);
  83.                         xTimerStart(Timer2_handle,portMAX_DELAY);
  84.                 }else if(key ==3){
  85.                         printf("关闭定时器\r\n");
  86.                         xTimerStop(Timer1_handle,portMAX_DELAY);
  87.                         xTimerStop(Timer2_handle,portMAX_DELAY);
  88.                
  89.                 }
  90.                 vTaskDelay(10);
  91.         }
  92. }
  93. //timer1超时回调函数
  94. void Timer1Callback( TimerHandle_t pxTimer )
  95. {
  96.                 static uint32_t        timer = 0;
  97.                 printf("timer1的运行次数%d\r\n",++timer);
  98. }
  99. //timer2超时回调函数
  100. void Timer2Callback( TimerHandle_t pxTimer )
  101. {
  102.                 static uint32_t        timer = 0;
  103.                 printf("timer2的运行次数%d\r\n",++timer);       
  104. }
复制代码
key.c
  1. #include "stm32f10x.h"                  // Device header
  2. #include "FreeRTOS.h"
  3. #include "task.h"
  4. #include "usart.h"
  5. #include "Delay.h"
  6. /**
  7.   * 函    数:按键初始化
  8.   * 参    数:无
  9.   * 返 回 值:无
  10.         * 按键:PB4/PB12/PB14
  11.   */
  12. void Key_Init(void)
  13. {
  14.         GPIO_InitTypeDef GPIO_InitStructure;
  15.        
  16.         /*开启时钟*/
  17.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);                //开启GPIOB的时钟
  18.         /*GPIO初始化*/
  19.         GPIO_InitStructure.GPIO_Mode         = GPIO_Mode_IPU;
  20.         GPIO_InitStructure.GPIO_Pin         = GPIO_Pin_4 | GPIO_Pin_12 | GPIO_Pin_14;
  21.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  22.         GPIO_Init(GPIOB, &GPIO_InitStructure);                                               
  23. }
  24. /**
  25.   * 函    数:按键获取键码
  26.   * 参    数:无
  27.   * 返 回 值:按下按键的键码值,范围:0~3,返回0代表没有按键按下
  28.   * 注意事项:此函数是阻塞式操作,当按键按住不放时,函数会卡住,直到按键松手
  29.   */
  30. uint8_t Key_GetNum(void)
  31. {
  32.         uint8_t KeyNum = 0;                                                                                                                                                                //定义变量,默认键码值为0
  33.        
  34.         if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0)                          //读PB4输入寄存器的状态,如果为0,则代表按键1按下
  35.         {
  36.                 KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4);
  37.                 delay_xms(20);                                                                                                                                                                        //延时消抖
  38.                 while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4) == 0);        //等待按键松手
  39.                 delay_xms(20);                                                                                                                                                                        //延时消抖
  40.                 KeyNum = 1;                                                                                                                                                                                        //置键码为1
  41.         }
  42.        
  43.         if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0)                       
  44.         {
  45.                 KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12);
  46.                 delay_xms(20);                                                                                       
  47.                 while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_12) == 0);       
  48.                 delay_xms(20);                                                                       
  49.                 KeyNum = 2;                                                                                       
  50.         }
  51.        
  52.         if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0)                       
  53.         {
  54.                 KeyNum= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14);
  55.                 delay_xms(20);                                                                                       
  56.                 while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0);       
  57.                 delay_xms(20);                                                                       
  58.                 KeyNum = 3;                                                                                       
  59.         }
  60.        
  61.         return KeyNum;                                                                                                                                                                                //返回键码值,如果没有按键按下,所有if都不成立,则键码为默认值0
  62. }
复制代码
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);

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表