本帖最后由 lmn2005 于 2023-7-12 14:12 编辑
CSDN博客的一篇文章( https://blog.csdn.net/m0_53904877/article/details/128375870
)详细介绍了SysTick的工作原理和如何用SysTick进行微秒级或毫秒级延时的方法,本人用STM32CubeMX初始化工程模板(芯片用GD32F103C8T6或STM32F401RCT8),用文章中介绍的延时方法进行延时,结果延时不正常(比正常的时间长),不知道是什么原因,请各位大佬指教,谢谢!
附:int main代码:
/* USER CODE BEGIN Header */
/**
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.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 ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int32_t fac_us, fac_ms ;
//STM32 printf重定义
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
return ch;
}
int fgetc(FILE *f)
{
uint8_t ch = 0;
HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
return ch;
}
void delay_init()
{
/* 配置时钟源 --> 48MHz */
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK );//配置时钟源为HCLK
/* fac_us 和 fac_ms 是定义的全局变量,是倍频因子*/
fac_us = SystemCoreClock / 1000000;
fac_ms = 1000 * fac_us;
}
void delay_us(unsigned int us)
{
unsigned int temp = 0;
SysTick->LOAD = fac_us * us;//设置重装载值.1us需要计数48次,计数初值为48
SysTick->VAL = 0x00;//当前值寄存器清0,即清空计数器
SysTick->CTRL |= 0x01;//滴答定时器控制寄存器,使能滴答定时器
do
{
temp = SysTick->CTRL;//获取控制寄存器的当前状态
}while((temp & 0x01) && !(temp & (0x01 << 16)));
SysTick->CTRL &= 0x00;
SysTick->VAL = 0x00;
}
void delay_ms(unsigned int ms)
{
unsigned int temp = 0;
/*设置重装载值*/
if(fac_ms * ms <= ((0x01 << 24) - 1))
{
SysTick->LOAD = fac_ms * ms;
}
else
{
return ;
}
/*设置重装载值*/
SysTick->VAL = 0x00;
/*滴答定时器控制寄存器,使能滴答定时器*/
SysTick->CTRL |= 0x01;
do
{
/*获取控制寄存器的当前状态*/
temp = SysTick->CTRL;
}while((temp & 0x01) && !(temp & (0x01 << 16)));
SysTick->CTRL &= 0x00;
SysTick->VAL = 0x00;
}
/* USER CODE END 0 */
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
//int32_t SysClk;
/* 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 */
delay_init();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
//HAL_Delay(1000);
delay_ms(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
|