diff --git a/Inc/systick.h b/Inc/systick.h index 83d892f..83285bc 100644 --- a/Inc/systick.h +++ b/Inc/systick.h @@ -27,4 +27,7 @@ void delay_us(uint32_t count); /* delay function that doesn't interfere with SysTick interrupt */ void delay_ms_safe(uint32_t count); +/* delay a time in microseconds (safe version) */ +void delay_us_safe(uint32_t count); + #endif /* SYS_TICK_H */ \ No newline at end of file diff --git a/Src/systick.c b/Src/systick.c index 8478805..9a385ac 100644 --- a/Src/systick.c +++ b/Src/systick.c @@ -106,8 +106,26 @@ 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++); } +} + +/** + * ************************************************************************ + * @brief delay_us_safe 微秒延时函数(不干扰SysTick中断) + * @details 使用简单循环实现延时,不会重新配置SysTick + * @param[in] count 微秒值 + * ************************************************************************ + */ +void delay_us_safe(uint32_t count) +{ + // 基于系统时钟的简单循环延时 + // 这是一个粗略的估计,实际延时可能有偏差 + uint32_t loops_per_us = SystemCoreClock / 8000000; // 粗略估计,每微秒的循环次数 + + for(uint32_t i = 0; i < count; i++) { + for(volatile uint32_t j = 0; j < loops_per_us; j++); + } } \ No newline at end of file