本帖最后由 dukelec 于 2026-1-16 12:27 编辑
楼主这种做法不准,譬如 dma 传输会让 cpu 运行速度降低
用 systick 做 us 级别的精准延迟,我的代码如下(不影响 systick 全局 1ms 计时):
[C] 纯文本查看 复制代码
// from: [url]https://github.com/dukelec/cdnet/blob/master/arch/stm32/arch_wrapper.c[/url]
void delay_us(uint32_t us)
{
uint32_t cnt_1ms = SysTick->LOAD + 1;
uint32_t last = SysTick->VAL;
uint32_t total = 0;
uint32_t target = cnt_1ms / 1000 * us;
while (total < target) {
uint32_t cur = SysTick->VAL;
int32_t diff = last - cur;
if (diff < 0)
diff += cnt_1ms;
total += diff;
last = cur;
}
}
|