generated from hulk/gd32e23x_template_cmake_vscode
add delay us safe
This commit is contained in:
@@ -27,4 +27,7 @@ void delay_us(uint32_t count);
|
|||||||
/* delay function that doesn't interfere with SysTick interrupt */
|
/* delay function that doesn't interfere with SysTick interrupt */
|
||||||
void delay_ms_safe(uint32_t count);
|
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 */
|
#endif /* SYS_TICK_H */
|
@@ -106,8 +106,26 @@ void delay_ms_safe(uint32_t count)
|
|||||||
// 基于系统时钟的简单循环延时
|
// 基于系统时钟的简单循环延时
|
||||||
// 这是一个粗略的估计,实际延时可能有偏差
|
// 这是一个粗略的估计,实际延时可能有偏差
|
||||||
uint32_t loops_per_ms = SystemCoreClock / 8000; // 粗略估计
|
uint32_t loops_per_ms = SystemCoreClock / 8000; // 粗略估计
|
||||||
|
|
||||||
for(uint32_t i = 0; i < count; i++) {
|
for(uint32_t i = 0; i < count; i++) {
|
||||||
for(volatile uint32_t j = 0; j < loops_per_ms; j++);
|
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++);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user