stm32TestTempl/Src/Delay.cpp
2025-04-06 14:30:52 +00:00

25 lines
893 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "stm32f10x.h"
// 仿原子延时不进入systic中断
void delay_us(u32 nus) {
u32 temp;
SysTick->LOAD = 9 * nus;
SysTick->VAL = 0X00; // 清空计数器
SysTick->CTRL = 0X01; // 使能,减到零是无动作,采用外部时钟源
do {
temp = SysTick->CTRL; // 读取当前倒计数值
} while ((temp & 0x01) && (!(temp & (1 << 16)))); // 等待时间到达
SysTick->CTRL = 0x00; // 关闭计数器
SysTick->VAL = 0X00; // 清空计数器
}
void delay_ms(u16 nms) {
u32 temp;
SysTick->LOAD = 9000 * nms;
SysTick->VAL = 0X00; // 清空计数器
SysTick->CTRL = 0X01; // 使能,减到零是无动作,采用外部时钟源
do {
temp = SysTick->CTRL; // 读取当前倒计数值
} while ((temp & 0x01) && (!(temp & (1 << 16)))); // 等待时间到达
SysTick->CTRL = 0x00; // 关闭计数器
SysTick->VAL = 0X00; // 清空计数器
}