您现在的位置是:首页 >其他 >STM32G0x0系列-软件定时器解决HAL_Delay问题网站首页其他

STM32G0x0系列-软件定时器解决HAL_Delay问题

SouthernBird 2024-07-15 00:01:02
简介STM32G0x0系列-软件定时器解决HAL_Delay问题

目录

HAL_Delay函数源码

软件定时器

创建工程试验

1.将对应文件移动到项目位置

2.添加现有文件放到工程中 

 3.重定向printf

3.验证自己的软件定时器

4.查看现象


        

HAL_Delay函数使用了中断机制来实现延时功能,当在中断函数中调用HAL_Delay函数需要考虑中断优先级问题。这样无疑让开发难度增加,我们可以通过分析HAL_Delay的源代码写出一个自己的软件定时器,来解决这个问题。

HAL_Delay函数源码

__weak void HAL_Delay(uint32_t Delay)
{
  uint32_t tickstart = HAL_GetTick();//获取当前时间存储到开始时间
  uint32_t wait = Delay;//计时时间

  /* Add a freq to guarantee minimum wait */
  if (wait < HAL_MAX_DELAY)
  {
    wait += (uint32_t)(uwTickFreq);
  }

  while ((HAL_GetTick() - tickstart) < wait)//当前时间与开始时间差值小与计数时间
  {
  }
}

上面代码牵扯到了函数HAL_GetTick

__weak uint32_t HAL_GetTick(void)//获取当前uwTick值
{
  return uwTick;
}

我们可以参照程序源码的思想书写自己的软件定时器

软件定时器

声明文件

#ifndef __SOFTTIME__H__
#define __SOFTTIME__H__

#include "stm32g0xx_hal.h"

typedef struct time
{
uint32_t start;//存储当前时间
uint32_t delay;//存储定时时间
}MyTime;

void SetTime(MyTime *timer,uint32_t delayms);//设置初始时间和定时时间
uint32_t competaTime(MyTime *timer);//比较时间
#endif

定义文件

#include "softtime.h"
void SetTime(MyTime *timer,uint32_t delayms)//设置初始时间和定时时间
{
timer->start = HAL_GetTick();
timer->delay = delayms;
}
uint32_t competaTime(MyTime *timer)//比较时间
{
if(HAL_GetTick()-timer->start>=timer->delay)
{
return 1;
}else
{
return 0;
}
}

创建工程试验

1.将对应文件移动到项目位置

 

2.添加现有文件放到工程中 

 

 3.重定向printf

 

 将fputc重写,此时printf随着改变

int fputc(int ch,FILE*f)
{
	while(!(USART1->ISR&(1<<7)));
	USART1->TDR=ch;
	return ch;
}

3.验证自己的软件定时器

 

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include "softtime.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 */

/* 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 */
int fputc(int ch,FILE*f)
{
	while(!(USART1->ISR&(1<<7)));
	USART1->TDR=ch;
	return ch;
}

/* 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 */
	MyTime time1; //将结构体实例化
	SetTime(&time1,1000);//设置时间为1000ms
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		if(competaTime(&time1) == 1)//比较时间
		{
			printf("SouthernBird
");
			SetTime(&time1,1000);//重置时间
			
		}
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

4.查看现象

 

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。