stm32 freeRTOS 软件定时器实验 (HAL版)

stm32 freeRTOS 软件定时器实验 (HAL版)

此实验是通过创建两个定时器,其中一个定时器为100ms,一个为1000ms,分别用来控制两个LED灯的闪烁,并且在闪烁的同时,通过串口输出一共执行过多少次了。

HAL配置:

在这里插入图片描述
在这里插入图片描述

在这里分别初始化两个定时器,然后根据自己的需求来配置LED的GPIO和串口。

主要需要调用的API

在这里插入图片描述

实验主要代码:

osStatus_t xReturn_1;
  osStatus_t xReturn_2;
  xReturn_1=osTimerStart(myTimer_Test_1Handle,100);
  xReturn_2=osTimerStart(myTimer_Test_2Handle,1000);
  if(xReturn_1==osOK)
  {
    printf("定时器1启动成功\r\n");
  }
  if(xReturn_2==osOK)
  {
    printf("定时器2启动成功\r\n");
  }
int timer_1,timer_2;
/* Callback_Test_1 function */
void Callback_Test_1(void *argument)
{
  /* USER CODE BEGIN Callback_Test_1 */
  timer_1++;
  HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_0);
  printf("timer_1执行了:");printf("%d\r\n",timer_1);
  /* USER CODE END Callback_Test_1 */
}

/* Callback_Test_2 function */
void Callback_Test_2(void *argument)
{
  /* USER CODE BEGIN Callback_Test_2 */
  timer_2++;
  HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
  printf("timer_2执行了:");printf("%d\r\n",timer_2);
  /* USER CODE END Callback_Test_2 */
}

freertos.c

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usart.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 128 * 4
};
/* Definitions for myTimer_Test_1 */
osTimerId_t myTimer_Test_1Handle;
const osTimerAttr_t myTimer_Test_1_attributes = {
  .name = "myTimer_Test_1"
};
/* Definitions for myTimer_Test_2 */
osTimerId_t myTimer_Test_2Handle;
const osTimerAttr_t myTimer_Test_2_attributes = {
  .name = "myTimer_Test_2"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void Callback_Test_1(void *argument);
void Callback_Test_2(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* Create the timer(s) */
  /* creation of myTimer_Test_1 */
  myTimer_Test_1Handle = osTimerNew(Callback_Test_1, osTimerPeriodic, NULL, &myTimer_Test_1_attributes);

  /* creation of myTimer_Test_2 */
  myTimer_Test_2Handle = osTimerNew(Callback_Test_2, osTimerPeriodic, NULL, &myTimer_Test_2_attributes);

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  osStatus_t xReturn_1;
  osStatus_t xReturn_2;
  xReturn_1=osTimerStart(myTimer_Test_1Handle,100);
  xReturn_2=osTimerStart(myTimer_Test_2Handle,1000);
  if(xReturn_1==osOK)
  {
    printf("定时器1启动成功\r\n");
  }
  if(xReturn_2==osOK)
  {
    printf("定时器2启动成功\r\n");
  }
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}
int timer_1,timer_2;
/* Callback_Test_1 function */
void Callback_Test_1(void *argument)
{
  /* USER CODE BEGIN Callback_Test_1 */
  timer_1++;
  HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_0);
  printf("timer_1执行了:");printf("%d\r\n",timer_1);
  /* USER CODE END Callback_Test_1 */
}

/* Callback_Test_2 function */
void Callback_Test_2(void *argument)
{
  /* USER CODE BEGIN Callback_Test_2 */
  timer_2++;
  HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
  printf("timer_2执行了:");printf("%d\r\n",timer_2);
  /* USER CODE END Callback_Test_2 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

实验现象:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值