概述
很多时候,应用场景,是需要分时处理各个事件。本文章就编写点亮LED与打印输出的示例来讲解使用方法。在这直接进入正题。(在此不做创建项目的步骤介绍)
1、STM32CubeIDE 配置
- LED管脚配置
- 串口1配置(也就是debug串口)
2、代码
- usart.c文件
-
/* USER CODE BEGIN 0 */
-
#include "stdio.h"
-
-
/* USER CODE END 0 */
-
.
-
.
-
.
-
-
/* USER CODE BEGIN 1 */
-
#ifdef __GNUC__
-
-
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
-
-
PUTCHAR_PROTOTYPE
-
{
-
// 注意下面第一个参数是&huart1,因为cubemx配置了串口1自动生成的
-
HAL_UART_Transmit(&huart1, ( uint8_t*)&ch, 1, HAL_MAX_DELAY);
-
return ch;
-
}
-
#endif
-
/* USER CODE END 1 */
-
- 创建software_Time.c文件
-
/*
-
* software_Time.c
-
*
-
* Created on: 2020年4月10日
-
* Author: champion
-
*/
-
#include "stm32l0xx.h"
-
#include "software_Time.h"
-
-
void SetTime(TIME_TYPE_ST *TimeType,uint32_t TimeInter)
-
{
-
TimeType->TimeStart = HAL_GetTick(); //获取起始时间
-
TimeType->TimeInter = TimeInter; //获取间隔时间
-
}
-
-
uint32_t CompareTime(TIME_TYPE_ST *TimeType) //每隔1毫秒,计数器就会增加1
-
{
-
return ((HAL_GetTick()-TimeType->TimeStart) >= TimeType->TimeInter);
-
}
-
-
- 创建software_Time.h文件
-
/*
-
* software_Time.h
-
*
-
* Created on: 2020年4月10日
-
* Author: champion
-
*/
-
-
#ifndef INC_SOFTWARE_TIME_H_
-
#define INC_SOFTWARE_TIME_H_
-
-
#include "stdint.h"
-
-
-
typedef struct
-
{
-
uint32_t TimeStart; //获取起始时间
-
uint32_t TimeInter; //间隔时间
-
}TIME_TYPE_ST;
-
-
void SetTime(TIME_TYPE_ST *TimeType,uint32_t TimeInter); //打开超时定时器
-
uint32_t CompareTime(TIME_TYPE_ST *TimeType); //比较函数
-
-
#endif /* INC_SOFTWARE_TIME_H_ */
-
- main.c文件
-
/* USER CODE END Header */
-
-
/* Includes ------------------------------------------------------------------*/
-
#include "main.h"
-
#include "usart.h"
-
#include "gpio.h"
-
-
/* Private includes ----------------------------------------------------------*/
-
/* USER CODE BEGIN Includes */
-
#include "stdio.h"
-
#include "software_Time.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 PV */
-
static TIME_TYPE_ST TimeLED;
-
static TIME_TYPE_ST TimePrint_1;
-
static TIME_TYPE_ST TimePrint_2;
-
-
/* USER CODE END PV */
-
-
/* Private function prototypes -----------------------------------------------*/
-
void SystemClock_Config(void);
-
/* USER CODE BEGIN PFP */
-
-
/* USER CODE END PFP */
-
-
/* Private user code ---------------------------------------------------------*/
-
/* USER CODE BEGIN 0 */
-
-
/* USER CODE END 0 */
-
-
/**
-
* @brief The application entry point.
-
* @retval int
-
*/
-
int main(void)
-
{
-
/* USER CODE BEGIN 1 */
-
-
/* USER CODE END 1 */
-
-
/* MCU Configuration--------------------------------------------------------*/
-
-
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
-
HAL_Init();
-
-
/* USER CODE BEGIN Init */
-
-
/* USER CODE END Init */
-
-
/* Configure the system clock */
-
SystemClock_Config();
-
-
/* USER CODE BEGIN SysInit */
-
-
/* USER CODE END SysInit */
-
-
/* Initialize all configured peripherals */
-
MX_GPIO_Init();
-
MX_LPUART1_UART_Init();
-
MX_USART1_UART_Init();
-
/* USER CODE BEGIN 2 */
-
-
-
/* USER CODE END 2 */
-
-
/* Infinite loop */
-
/* USER CODE BEGIN WHILE */
-
while ( 1)
-
{
-
if(CompareTime(&TimeLED))
-
{
-
SetTime(&TimeLED, 500); //500ms
-
HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
-
}
-
-
if(CompareTime(&TimePrint_1))
-
{
-
SetTime(&TimePrint_1, 1000); //1s打印
-
printf( "TimePrint_1s \r\n");
-
}
-
-
if(CompareTime(&TimePrint_2))
-
{
-
SetTime(&TimePrint_2, 2000); //2s打印
-
printf( "TimePrint_2s \r\n");
-
}
-
/* USER CODE END WHILE */
-
-
/* USER CODE BEGIN 3 */
-
}
-
/* USER CODE END 3 */
-
}
-
- 运行效果
总结:通过结果可以看出,是可以模拟出多任务处理的机制,其实也就是时间片轮法形式的多线程任务。
转载:https://blog.csdn.net/qq_36075612/article/details/105432947
查看评论