单片机延时函数

  • Post author:
  • Post category:其他


精确的单片机常用延时函数:(c代码误差0us 12M)——并未验证

1、延时0.5ms

void delay0.5ms(void)

//误差0us

{

unsigned char a,b;

for(b=71;b>0;b–)

for(a=2;a>0;a–);

}

2、延时1ms void delay1ms(void)

//误差0us

{

unsigned char a,b,c;

for(c=1;c>0;c–)

for(b=142;b>0;b–)

for(a=2;a>0;a–);

}

3、延时2ms

void delay2ms(void)

//误差0us

{

unsigned char a,b;

for(b=4;b>0;b–)

for(a=248;a>0;a–);

_nop_;

//if Keil,require use intrins.h

}

4、延时3ms

void delay3ms(void)

//误差0us

{

unsigned char a,b;

for(b=111;b>0;b–)

for(a=12;a>0;a–);

}

5、延时4ms

void delay4ms(void)

//误差

0us

{

unsigned char a,b,c;

for(c=7;c>0;c–)

for(b=8;b>0;b–)

for(a=34;a>0;a–);

}

6、延时5ms

void delay5ms(void)

//误差0us

{

unsigned char a,b;

for(b=19;b>0;b–)

for(a=130;a>0;a–);

}

7、延时10ms

void delay10ms(void)

//误差0us

{

unsigned char a,b,c;

for(c=1;c>0;c–)

for(b=38;b>0;b–)

for(a=130;a>0;a–);

}

8、延时15ms

void delay15ms(void) //误差0us

{

unsigned char a,b,c;

for(c=1;c>0;c–)

for(b=238;b>0;b–)

for(a=30;a>0;a–);

}

9、延时20ms

void delay20ms(void) //误差0us

{

unsigned char a,b;

for(b=215;b>0;b–)

for(a=45;a>0;a–);

_nop_;

//if Keil,require use intrins.h

_nop_;

//if Keil,require use intrins.h

}

10、延时50ms

void delay50ms(void) //误差0us

{

unsigned char a,b;

for(b=173;b>0;b–)

for(a=143;a>0;a–);

}

11、延时100ms

void delay100ms(void) //误差0us

{

unsigned char a,b,c;

for(c=19;c>0;c–)

for(b=20;b>0;b–)

for(a=130;a>0;a–);

}

12、延时200ms

void delay200ms(void) //误差0us

{

unsigned char a,b,c;

for(c=4;c>0;c–)

for(b=116;b>0;b–)

for(a=214;a>0;a–);

_nop_;

//if Keil,require use intrins.h

}

13、延时500ms

void delay500ms(void) //误差0us

{

unsigned char a,b,c;

for(c=23;c>0;c–)

for(b=152;b>0;b–)

for(a=70;a>0;a–);

}

14、延时1000ms

void delay(void) //误差0us

{

unsigned char a,b,c;

for(c=167;c>0;c–)

for(b=171;b>0;b–)

for(a=16;a>0;a–);

_nop_;

//if Keil,require use intrins.h

}