Files
FilmForge/Src/systick.c
T
2026-10-09 18:45:50 +08:00

84 lines
2.3 KiB
C

/**
* ************************************************************************
*
* @file systick.c
* @author GD32
* @brief 通过 SysTick 定时器进行微秒级别和毫秒级别的延时函数
*
* ************************************************************************
* @copyright Copyright (c) 2024 GD32
* ************************************************************************
*/
#include "gd32f4xx.h"
#include "systick.h"
static __IO uint32_t delay_count = 0;
/**
* ************************************************************************
* @brief 配置 SysTick 定时器
*
*
* ************************************************************************
*/
void systick_config(void)
{
//设置了 SysTick 定时器的时钟源为 HCLK
systick_clksource_set(SYSTICK_CLKSOURCE_HCLK);
// 配置SysTick为1ms周期中断
// 注意:SysTick_Config会自动设置时钟源为HCLK,所以需要使用SystemCoreClock/1000
SysTick_Config(SystemCoreClock / 1000U); // 1ms中断
NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
/**
* ************************************************************************
* @brief delay_10us 10微秒延时函数
*
* @param[in] count 10微秒值
* @note 基于循环的粗略延时(Cortex-M4 @168MHz 约 3 个周期/次迭代),
* 实际延时需上板实测校准分母系数
*
* ************************************************************************
*/
void delay_10us(uint32_t count)
{
// 168MHz 下每 10us 约 560 次迭代 → 分母取 300000
uint32_t loops_per_10us = SystemCoreClock / 300000;
for(uint32_t i = 0; i < count; i++) {
for(__IO uint32_t j = 0; j < loops_per_10us; j++);
}
}
/**
* ************************************************************************
* @brief delay_ms 毫秒延时函数
*
* @param[in] count 毫秒值
*
* ************************************************************************
*/
void delay_ms(uint32_t count)
{
delay_count = count; // 设置延时计数
while (delay_count != 0U);
}
/**
* ************************************************************************
* @brief 每个 SysTick 中断调用时,减少延时计数
*
* @param[in] void
*
* ************************************************************************
*/
void delay_decrement(void)
{
if (delay_count != 0U)
{
delay_count--;
}
}