/*---------------------------------
延時模塊函數(shù)
說明:只需在工程中加入delay.c和delay.h
文件,即可用 Delayms(__IO uint32_t nTime);
Delayus(__IO uint32_t nTime)
-----------------------------------*/
#ifndef __DELAY_H
#define __DELAY_H
#include "stm32f10x.h"
/*---------------------------------
描 述:參數(shù)1即為1ms,1000即為1s;只有幾
us的誤差;
-----------------------------------*/
extern void Delayms(__IO uint32_t nTime);
/*---------------------------------
描 述:參數(shù)1即為1us,1000即為1ms;只有幾
us的誤差;
-----------------------------------*/
extern void Delayus(__IO uint32_t nTime);
/*---------------------------------
函數(shù)名:延時調(diào)整形式的delaynus函數(shù),
描 述:參數(shù)1即為1us,1000即為1ms;
-----------------------------------*/
extern void delay_nus(unsigned long n);
/*---------------------------------
函數(shù)名:延時調(diào)整形式的delaynms函數(shù),
描 述:參數(shù)1即為1ms,1000即為1s;
-----------------------------------*/
extern void delay_nms(unsigned long n);
#endif
C 函數(shù)
/*---------------------------------
延時模塊函數(shù)
說明:只需在工程中加入delay.c和delay.h
文件,即可用 Delayms(__IO uint32_t nTime);
Delayus(__IO uint32_t nTime)
-----------------------------------*/
#include"delay.h"
static __IO uint32_t TimingDelay;
/* Private function prototypes -----------------------------------------------*/
/*---------------------------------
函數(shù)名:ms延時函數(shù)
描 述:參數(shù)1即為1ms,1000即為1s;只有幾
us的誤差;
-----------------------------------*/
void Delayms(__IO uint32_t nTime)
{
while(SysTick_Config(SystemCoreClock/1000));
TimingDelay = nTime;
while(TimingDelay != 0);
SysTick->CTRL=0x00; //關(guān)閉計數(shù)器
SysTick->VAL =0X00; //清空計數(shù)器
}
/*---------------------------------
函數(shù)名:us延時函數(shù)
描 述:參數(shù)1即為1us,1000即為1ms;只有幾
us的誤差;
-----------------------------------*/
void Delayus(__IO uint32_t nTime)
{
while(SysTick_Config(SystemCoreClock/1000000));
TimingDelay = nTime;
while(TimingDelay != 0);
SysTick->CTRL=0x00; //關(guān)閉計數(shù)器
SysTick->VAL =0X00; //清空計數(shù)器
}
/*---------------------------------
函數(shù)名:延時輔助函數(shù)
描 述:
-----------------------------------*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
/*---------------------------------
函數(shù)名:systick的中斷函數(shù)
描 述:參數(shù)1即為1us,1000即為1ms;只有幾
us的誤差;
-----------------------------------*/
void SysTick_Handler(void)
{
TimingDelay_Decrement();
}
/*---------------------------------
函數(shù)名:延時調(diào)整形式的delaynus函數(shù),
描 述:參數(shù)1即為1us,1000即為1ms;
-----------------------------------*/
void delay_nus(unsigned long n)
{
unsigned long j;
while(n--)
{
j=12;
while(j--);
}
}
/*---------------------------------
函數(shù)名:延時調(diào)整形式的delaynms函數(shù),
描 述:參數(shù)1即為1ms,1000即為1s;
-----------------------------------*/
void delay_nms(unsigned long n)
{
while(n--)
delay_nus(1030);
}