我的C51延时程序

基础延时程序经过了严格计算得出的,保证精准。

#include <intrins.h>

/********************************
*            基础延时           *
********************************/

//适用于12MHz晶振,如果不需要请注释掉
void delay1s(void)   //延时1秒 误差 0us
{
    unsigned char a,b,c;
    for(c=46;c>0;c--)
        for(b=152;b>0;b--)
            for(a=70;a>0;a--);
    _nop_();  //if Keil,require use intrins.h
}
void delay1ms(void)   //延时1毫秒 误差 0us
{
    unsigned char a,b;
    for(b=199;b>0;b--)
        for(a=1;a>0;a--);
}

//适用于11.0592MHZ晶振,如果不需要请注释掉
void delay1s(void)   //误差 -0.00000000024us

{
    unsigned char a,b,c;
    for(c=95;c>0;c--)
        for(b=26;b>0;b--)
            for(a=185;a>0;a--);
}
void delay1ms(void)   //误差 -0.651041666667us
{
    unsigned char a,b;
    for(b=4;b>0;b--)
        for(a=113;a>0;a--);
}

常用延时才是日常应用的主角,通过调用基础延时来实现,误差很小。

/********************************
*            常用延时           *
********************************/
void delay(unsigned int time)	//延时time秒
{
	do
		delay1s();
	while(--time);
}

void delay_ms(unsigned int time)	//延时time毫秒
{
	do
		delay1ms();
	while(--time);	 
}					  
原文地址:https://www.cnblogs.com/catmelo/p/2255976.html