STM32精确延时

uint64_t delay_ticks;

void SysTick_Handler(void) {
	if (delay_ticks > 0) {
		delay_ticks--;
	} else {
		SysTick->CTRL = 0x00;
		SysTick->VAL = 0x00;
	}
}

void delay_us(unsigned long n) {
	delay_ticks = n;
	SysTick_Config(SystemCoreClock/1000000);
	while(delay_ticks);
}

void delay_ms(unsigned long n) {
	delay_ticks = n;
	SysTick_Config(SystemCoreClock/1000);
	while(delay_ticks);
}

注:SystemCoreClock在头文件system_stm32xxx.h中有定义。