#include "./bsp_SysTick.h"
static __IO u32 TimingDelay;
void SysTick_Init(void)
{
/* SystemFrequency / 1000 1ms中断一次
* SystemFrequency / 100000 10us中断一次
* SystemFrequency / 1000000 1us中断一次
*/
if (HAL_SYSTICK_Config(SystemCoreClock / 1000000))
{
/* Capture error */
while (1);
}
}
void Delay_us(__IO u32 nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
#ifndef __SYSTICK_H
#define __SYSTICK_H
#include "stm32f1xx.h"
typedef uint32_t u32;
void SysTick_Init(void);
void Delay_us(__IO u32 nTime);
#define Delay_ms(x) Delay_us(1000*x)//单位ms
#define Delay_s(x) Delay_us(1000000*x)//单位s
#endif /* __SYSTICK_H */
在main文件里添加
#include "./bsp_SysTick.h"
SysTick_Init();
就可以使用Delay_us();实现微秒级别延时