51软件延时函数编写

51软件延时函数编写

****************************************************

void delay(uint z)

{

uint x,y;

for(x=z;x>0;x--)

for(y=114;y>0;y--);

}

z赋值多少,此函数循环时间就为多少ms;

Pasted Graphic.tiff

进入for循环时时刻为0.00043294s

然后让他执行完for循环

Pasted Graphic 1.tiff

执行完for循环后的时刻为0.00143338s

将数值相减后得到0.00100044s约等于1s

以上延时函数仅适用于晶振频率为11.0592MHZ情况下的1ms延时程序。

 

********************************************************************

自适应晶振频率ms延时函数:

#define MAIN_Fose    11059200UL

#define INT16U unsigned int

void delay_MS(INT16U ms)      

{

INT16U i;

do

{

i=MAIN_Fose /9600;

while(—i);

}

while(—ms);

}

此延时函数仅适用于12T单片机中,适用不同晶振频率时,即在最前面“#define MAIN_Fose    11059200UL”宏定义此系统的晶振频率即可。 

注:12T单片机指的是  12个时钟周期=1个机器周期的51单片机。

 

posted @ 2017-06-08 16:23  Brianzhangzhang  阅读(2328)  评论(0编辑  收藏  举报