本文引用地址:http://m.butianyuan.cn/article/201611/321703.htm
#define SYSTICK_COUNTFLAG 16
//初始化延遲函數(shù)
void delay_init(void)
{
uint32_t ticks;
ticks = SystemFrequency/1000;
if (ticks > SYSTICK_MAXCOUNT)while (1);
SysTick->LOAD=(ticks & SYSTICK_MAXCOUNT) - 1;
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
SysTick->VAL=(0x00);
SysTick->CTRL = (1 << SYSTICK_CLKSOURCE);//選擇內(nèi)核時鐘
}
//ms延時
void delay_nms(__IO uint32_t nms)
{
u32 temp;
if(nms == 0)return;
SysTick->VAL = 0x00;//清空計數(shù)器
SysTick->CTRL |= (1 << SYSTICK_ENABLE);
while(nms--)
{
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));//等待時間到達
}
SysTick->CTRL &= ~(1 << SYSTICK_ENABLE);
SysTick->VAL =0X00;//清空計數(shù)器
}
評論