新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32 延時(shí)函數(shù)封裝

STM32 延時(shí)函數(shù)封裝

作者: 時(shí)間:2012-09-10 來(lái)源:網(wǎng)絡(luò) 收藏

/*---------------------------------

本文引用地址:http://2s4d.com/article/148436.htm

模塊

說(shuō)明:只需在工程中加入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);

/*---------------------------------

名:調(diào)整形式的delaynus,

描 述:參數(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í)模塊函數(shù)

說(shuō)明:只需在工程中加入delay.c和delay.h

文件,即可用 Delayms(__IO uint32_t nTime);

Delayus(__IO uint32_t nTime)

-----------------------------------*/

#includedelay.h

static __IO uint32_t TimingDelay;

/* Private function prototypes -----------------------------------------------*/

/*---------------------------------

函數(shù)名:ms延時(shí)函數(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)閉計(jì)數(shù)器

SysTick->VAL =0X00; //清空計(jì)數(shù)器

}

/*---------------------------------

函數(shù)名:us延時(shí)函數(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)閉計(jì)數(shù)器

SysTick->VAL =0X00; //清空計(jì)數(shù)器

}

/*---------------------------------

函數(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ù)名:延時(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ù)名:延時(shí)調(diào)整形式的delaynms函數(shù),

描 述:參數(shù)1即為1ms,1000即為1s;

-----------------------------------*/

void delay_nms(unsigned long n)

{

while(n--)

delay_nus(1030);

}



關(guān)鍵詞: 封裝 函數(shù) 延時(shí) STM32

評(píng)論


相關(guān)推薦

技術(shù)專(zhuān)區(qū)

關(guān)閉