generated from hulk/gd32e23x_template_cmake_vscode
add delay ms safe & add led heart beat
This commit is contained in:
@@ -30,6 +30,10 @@ void systick_config(void)
|
||||
count_1us = (float)SystemCoreClock/8000000;
|
||||
//计算了每毫秒所需的 SysTick 计数值
|
||||
count_1ms = (float)count_1us * 1000;
|
||||
|
||||
// 配置SysTick为1ms周期中断
|
||||
// 注意:SysTick_Config会自动设置时钟源为HCLK,所以需要使用SystemCoreClock/1000
|
||||
SysTick_Config(SystemCoreClock / 1000); // 1ms中断
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,4 +92,22 @@ void delay_ms(uint32_t count)
|
||||
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
|
||||
//将 SysTick 计数器的当前值清零,以便下次使用
|
||||
SysTick->VAL = 0x0000U;
|
||||
}
|
||||
|
||||
/**
|
||||
* ************************************************************************
|
||||
* @brief delay_ms_safe 毫秒延时函数(不干扰SysTick中断)
|
||||
* @details 使用简单循环实现延时,不会重新配置SysTick
|
||||
* @param[in] count 毫秒值
|
||||
* ************************************************************************
|
||||
*/
|
||||
void delay_ms_safe(uint32_t count)
|
||||
{
|
||||
// 基于系统时钟的简单循环延时
|
||||
// 这是一个粗略的估计,实际延时可能有偏差
|
||||
uint32_t loops_per_ms = SystemCoreClock / 8000; // 粗略估计
|
||||
|
||||
for(uint32_t i = 0; i < count; i++) {
|
||||
for(volatile uint32_t j = 0; j < loops_per_ms; j++);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user