【51单片机】LED点亮,闪烁以及流水灯实现的四种方法

【51单片机】LED点亮,闪烁以及流水灯实现的四种方法

点亮LED

#include 
sbit LED=P2^0;
void main()
{
     
	while(1)
	{
     
		LED=0;
	}	
}

LED闪烁(delay函数)

#include 
 sbit LED = P2^0;//管脚定义
 void delay(unsigned char m)//m是形式参数
 {
     
 		unsigned char i,j;
		for (i=110;i>0;i--)
		for (j=m;j>0;j--);
 }
 void main()
 {
     
 	while (1)
	{
     
	 	LED=0;
		delay(100);
		LED=1;
		delay(100); 
	}
 }

LED闪烁

#include 
sbit LED1=P1^0;
unsigned int i;
void main()
{
     
		while(1)
	{
     
	i=65535;
	while(i--);

		LED1=~LED1;
	}
}

流水灯(库函数法)

#include 
#include 
#define uint unsigned int
#define uchar unsigned char
uchar temp;
void delay(uint m)
{
     
	  uint i,j;
	  for (i=m;i>0;i--)
	  for (j=114;j>0;j--);
}
void main()
{
     
	temp = 0xfe;
	P1 = temp;
	delay(100);
	while(1)
	{
     
		temp = _crol_(temp,1);
		P1=temp;
		delay(100);
	}
}

流水灯(左移法)

#include 
#define uint unsigned int
#define uchar unsigned char
void delay(uint m)
{
     
	uint i,j;
	for (i=110;i>0;i--)
		for (j=m;j>0;j--);
}
void main()
{
     
	P1=0x01;
	while(1)
	{
     
		delay(500);
		P1=P1<<1;
		if(P1==0x00)
		P1=0x01;
	}

}

流水灯(查找数组法)

#include 
#define uint unsigned int 
#define uchar unsigned char
uchar table[]={
     0x01,0x02,0x04,0x08,
			   0x10,0x20,0x40,0x80};
			   uchar p;
void delay(uint m)
{
     
	uint i,j;
	for (i=110;i>0;i--)
	  	for (j=m;j>0;j--) ;

}
void main()
{
     
	while (1)
		{
     
		  for(p=0;p<8;p++)
		  {
     
		  	P1=table[p];
		   	delay(500);
		  }
		   	
		}
}

流水灯(右移法)

#include 
#define uint unsigned int
#define uchar unsigned char
void delay(uint m)
{
     
	uint i,j;
	for (i=110;i>0;i--)
		for (j=m;j>0;j--);
}
void main()
{
     
	P1=0x80;
	while(1)
	{
     
		delay(500);
		P1=P1>>1;
		if(P1==0x00)
		P1=0x01;
	}

}

菜鸟刚上路,见笑了

你可能感兴趣的:(51单片机学习,单片机)