/2016.03.28 ( E:\DPJ_C\1S_x.c )
#include <reg51.h>
void delay_ms(unsigned int ms) // 延时子程序
{
unsigned int a,b;
for(a=ms;a>0;a--)
for(b=123;b>0;b--);
}
void main ()
{
while(1)
{
delay_ms(1000); //1s 延时,数字即为多少毫秒
P1=~P1;
}
}
还有一个更简单的,补在这里。 每秒多了23us ,int 改为 char 。 如果用 int ,ms=1000,居然用了1.495S
// 2010 04 01 E:\DPJ_C\ms.c
#include <reg51.h>
void delay_ms(unsigned int ms)
{
unsigned int a;
while(ms--) //while()内的ms不为0,即为真
for(a=0;a<123;a++);
}
void main()
{
while(1)
{
delay_ms(1000);
P1=~P1;
}
}
|