51单片机如何延时1s,让LED灯闪烁

精确地延时1s需要准确地计算,粗略地延迟1s可以自定义一个delay函数,下面我们让第一个LED灯延迟1s闪烁:

//本题的delay函数参数为1时延迟的是1ms,1000是1s
#include<reg52.h>    
#define uchar unsigned char
#define uint unsigned int
void delay(uint z);
sbit LED=P1^0;   //定义管脚
void main()
{
    while(1)   //让体系一直循环下去
	    {
		LED=0;    //我的单片机默认接的是高电平,给个低电平就可以亮啦
		delay(1000); //延迟1s
		LED=1;      //LED灯熄灭
		delay(1000);  //这个延迟不要忘了,不然的话灯会一直亮着
		}             //灯亮1s,熄灭1s,所以周期是2s
}
void delay(uint z)
{
 	uint x,y;
 	for(x=z;x>0;x--)
 	for(y=110;y>0;y--);  //y的数值是模拟推算出来的
}

解释都在注释里啦,烧录到你的单片机里就可以闪烁喽~~

猜你喜欢

转载自blog.csdn.net/followtheheart/article/details/107879615