栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

STM32CubeMX freeRTOS定时器应用

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

STM32CubeMX freeRTOS定时器应用

一、freeRTOS定时器配置需要注意的地方

1、在STM32CubeMX 首先找到Config parameters这个选项下的Software timer definitions,然后将USE_TIMERS设置为Enable

2、这个时候将其它的配置保持默认 ,将时钟配置好后 就能够生成代码了。(我是在这里配置了两颗 LED灯,1颗使用系统默认的一个线程,让它每200ms进行闪烁一次,另一个则是使用定时器定时每200ms闪烁一次)。

3、生成代码后,我们需要自己创建一个定时器使用的系统API函数为:

a、创建定时器的API函数
**
* name          name of the timer object.
* param         function      name of the timer call back function.
*/
osTimerDef(name, function)

osTimerDef(LEDTimer, osTimerCallback);
**
* @brief  Create a timer.
* @param  timer_def     timer object referenced with ref osTimer.
* @param  type          osTimeronce for one-shot or osTimerPeriodic for periodic behavior.
* @param  argument      argument to the timer call back function.
* @retval  timer ID for reference by other functions or NULL in case of error.
* @note   MUST REMAIN UNCHANGED: b osTimerCreate shall be consistent in every CMSIS-RTOS.
*/
osTimerId osTimerCreate (const osTimerDef_t *timer_def, os_timer_type type, void *argument)

b、创建定时器句柄
osTimerId osTimer = osTimerCreate (osTimer(LEDTimer), osTimerPeriodic, NULL);

创建完定时器后,我们现在就可以来设置定时器的周期和开启定时器了(这里我设置的是定时器周期100ms) 。

  osTimerStart(osTimer, 100);

4、下面我们在主函数main.c前面声明该定时器的回调函数

static void osTimerCallback(void const *argument);

5、在回调函数中的实现,其实这就很简单了

static void osTimerCallback (void const *argument)
{
  (void) argument;  
  
  
  HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_9);
}

6、默认线程中的也是将LED进行闪烁



void StartDefaultTask(void const * argument)
{
  
  
  for(;;)
  {
    osDelay(200);
		HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_10);
  }
  
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/698361.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号