单片机心形灯

有很多朋友想让出一个关于心形灯的教程,忙活了两天终于完成,代码略长,电路图仿真图已备好。

所需元器件如下:
STC89C52RC芯片 *1
10K电阻 *1
10uF 电解电容 *1
22pF 瓷片电容 *2
12MHz晶振 *1
按键 *1
LED灯 *32
330Ω电阻 *32

电路图如下:
单片机心形灯_第1张图片
代码如下:

 /*********************************************************************************
 *********************************************************************************/
 //------------------------------------------------------------------------------//
 //																				                                       //
 //标题:心形灯亮灭效果程序									 									 									 //
 //创建人:朽木自雕i		[email protected]														 							 //
 //日期:2019/07/09					修改日期:2020/03/08													 		   //
 //描述:																										 									 	 //
 //      本程序实现的是由4组I/O接口控制32个LED灯亮灭,实现LED心形环绕。			     //
 //声明:									 									 									 									 //
 //      以下代码仅免费提供给学习用途,但引用或修改后必须在文件中声明出处,		   //
 //      如有商业用途请与作者联系。																					     //
 //																													  							 	   //
 //------------------------------------------------------------------------------//
 /*********************************************************************************
 *********************************************************************************/
 
 
#include"reg52.h"
#include		//因为要用到左右移函数,所以加入这个头文件

typedef unsigned int u16;	  //对数据类型进行声明定义
typedef unsigned char u8;
unsigned int x,z;
#define LED0 P0 			  //把PO端口设置为LED控制口
#define LED1 P1				  //把P1端口设置为LED控制口
#define LED2 P2 			  //把P2端口设置为LED控制口
#define LED3 P3				  //把P3端口设置为LED控制口

void delay(unsigned int x)//定义延时函数
{
     
	unsigned char i;
	while(x--)
	{
     
		for(i=0;i<120;i++);//大约1ms
	}
}
void Chu()//初始化LED灯,让所有LED保持熄灭状态
{
     
	LED0=0XFF;
	LED1=0XFF;
	LED2=0XFF;
	LED3=0XFF;
}
/*-----------------样式1------------------------------
	效果:所有LED灯挨个点亮,每次只显示一颗灯亮
  功能:主要为LED检测,检查所有LED好坏,作为第一样式
-----------------------------------------------------*/
void YANG_1()
{
     
	{
     
		u8 i;
		LED0=~0x01;
		//delay(50);     //大约延时90ms	
		for(i=0;i<8;i++)
		{
     
			LED0=~(0x01<<i);	 //将1右移i位,然后将结果赋值到P0口
			delay(50);        //大约延时90ms
		}
	}
	LED0=0xff;
	{
     
		u8 i;
		LED1=~0x01;
		//delay(50);     //大约延时90ms	
		for(i=0;i<8;i++)
		{
     
			LED1=~(0x01<<i);	 //将1右移i位,然后将结果赋值到P1口
			delay(50); //大约延时90ms
		}
	}
	LED1=0xff;
	{
     
		u8 i;
		LED2=~0x01;
		//delay(50); //大约延时90ms	
		for(i=0;i<8;i++)
		{
     
			LED2=~(0x01<<i);	 //将1右移i位,然后将结果赋值到P2口
			delay(50); //大约延时90ms
		}
	}
	LED2=0xff;
	{
     
		u8 i;
		LED3=~0x01;
		//delay(50); //大约延时90ms	
		for(i=0;i<8;i++)
		{
     
			LED3=~(0x01<<i);	 //将1右移i位,然后将结果赋值到P3口
			delay(50); //大约延时90ms
		}
	}
	LED3=0xff;
	{
     
		u8 i;
		LED3=~0x80;
		//delay(50); //大约延时90ms	
		for(i=0;i<8;i++)
		{
     
			LED3=~(0x80>>i);	 //将1右移i位,然后将结果赋值到P3口
			delay(50); //大约延时90ms
		}
	}
	LED3=0xff;
	{
     
		u8 i;
		LED2=~0x80;
		//delay(50); //大约延时90ms	
		for(i=0;i<8;i++)
		{
     
			LED2=~(0x80>>i);	 //将1右移i位,然后将结果赋值到P2口
			delay(50); //大约延时90ms
		}
	}
	LED2=0xff;
	{
     
		u8 i;
		LED1=~0x80;
		//delay(50); //大约延时90ms	
		for(i=0;i<8;i++)
		{
     
			LED1=~(0x80>>i);	 //将1右移i位,然后将结果赋值到P1口
			delay(50); //大约延时90ms
		}
	}
	LED1=0xff;
	{
     
		u8 i;
		LED0=~0x80;
		//delay(50); //大约延时90ms	
		for(i=0;i<8;i++)
		{
     
			LED0=~(0x80>>i);	 //将1右移i位,然后将结果赋值到P0口
			delay(50); //大约延时90ms
		}
	}
	LED0=0xff;
}
/*-----------------样式2------------------------------
	效果:所有LED灯挨个点亮,每次多一颗灯亮,等所有LED全
	      亮后,每组I/O口倒循环熄灭再挨个点亮
-----------------------------------------------------*/
void YANG_2()
{
     
	{
     
		u8 i;
		for(i=0;i<8;i++)	  //利用for函数实现循环
		{
     
			LED0=0xfe<<i;	  //LED灯从第一个向右移挨个点亮(每次移动多亮一颗)
			delay(100);	  //延时,大约90ms
		} 
	}	
	{
     
		u8 i;
		for(i=0;i<8;i++)
		{
     
			LED1=0xfe<<i;
			delay(100);
		} 
	}
	{
     
		u8 i;
		for(i=0;i<8;i++)
		{
     
			LED2=0xfe<<i;
			delay(100);
		} 
	}
	{
     
		u8 i;
		for(i=0;i<8;i++)
		{
     
			LED3=0xfe<<i;
			delay(100);
		} 
	}
	{
     
		u8 i;
		for(i=0;i<8;i++)
		{
     
			LED3=0x7f>>i;
			delay(100);
		} 
	}
	{
     
		u8 i;
		for(i=0;i<8;i++)
		{
     
			LED2=0x7f>>i;
			delay(100);
		} 
	}
	{
     
		u8 i;
		for(i=0;i<8;i++)
		{
     
			LED1=0x7f>>i;
			delay(100);
		} 
	}
	{
     
		u8 i;
		for(i=0;i<8;i++)
		{
     
			LED0=0x7f>>i;
			delay(100);
		} 
	}
}
/*-----------------样式3------------------------------
	效果:所有LED灯挨个点亮,每次多一颗灯亮,等所有LED灯
	      全亮后再每次熄灭一颗
-----------------------------------------------------*/
void YANG_3()
{
     
	LED0=0xfe;
	delay(50);
	LED0=0xfc;
	delay(50);
	LED0=0xf8;
	delay(50);
	LED0=0xf0;
	delay(50);
	LED0=0xe0;
	delay(50);
	LED0=0xc0;
	delay(50);
	LED0=0x80;
	delay(50);
	LED0=0x00;
	delay(50);

	LED1=0xfe;
	delay(50);
	LED1=0xfc;
	delay(50);
	LED1=0xf8;
	delay(50);
	LED1=0xf0;
	delay(50);
	LED1=0xe0;
	delay(50);
	LED1=0xc0;
	delay(50);
	LED1=0x80;
	delay(50);
	LED1=0x00;
	delay(50);

	LED2=0xfe;
	delay(50);
	LED2=0xfc;
	delay(50);
	LED2=0xf8;
	delay(50);
	LED2=0xf0;
	delay(50);
	LED2=0xe0;
	delay(50);
	LED2=0xc0;
	delay(50);
	LED2=0x80;
	delay(50);
	LED2=0x00;
	delay(50);

	LED3=0xfe;
	delay(50);
	LED3=0xfc;
	delay(50);
	LED3=0xf8;
	delay(50);
	LED3=0xf0;
	delay(50);
	LED3=0xe0;
	delay(50);
	LED3=0xc0;
	delay(50);
	LED3=0x80;
	delay(50);
	LED3=0x00;
	delay(50);

	LED3=0x00;
	delay(50);
	LED3=0x80;
	delay(50);
	LED3=0xc0;
	delay(50);
	LED3=0xe0;
	delay(50);
	LED3=0xf0;
	delay(50);
	LED3=0xf8;
	delay(50);
	LED3=0xfc;
	delay(50);
	LED3=0xfe;
	delay(50);
	LED3=0xff;
	delay(50);
	
	LED2=0x00;
	delay(50);
	LED2=0x80;
	delay(50);
	LED2=0xc0;
	delay(50);
	LED2=0xe0;
	delay(50);
	LED2=0xf0;
	delay(50);
	LED2=0xf8;
	delay(50);
	LED2=0xfc;
	delay(50);
	LED2=0xfe;
	delay(50);
	LED2=0xff;
	delay(50);
	
	LED1=0x00;
	delay(50);
	LED1=0x80;
	delay(50);
	LED1=0xc0;
	delay(50);
	LED1=0xe0;
	delay(50);
	LED1=0xf0;
	delay(50);
	LED1=0xf8;
	delay(50);
	LED1=0xfc;
	delay(50);
	LED1=0xfe;
	delay(50);
	LED1=0xff;
	delay(50);
	
	LED0=0x00;
	delay(50);
	LED0=0x80;
	delay(50);
	LED0=0xc0;
	delay(50);
	LED0=0xe0;
	delay(50);
	LED0=0xf0;
	delay(50);
	LED0=0xf8;
	delay(50);
	LED0=0xfc;
	delay(50);
	LED0=0xfe;
	delay(50);
	LED0=0xff;
	delay(50);
}
/*-----------------样式4------------------------------
	效果:从P0>P3每组每次亮只显示两颗灯,先是顺序,而后
	      首尾,全部I/O口执行一遍后从P0口开始,首尾灯向
				中间循环点亮再回到首尾,直至每组I/O口全部执行
				一遍后结束
-----------------------------------------------------*/
void YANG_4()
{
     
	{
     
	LED0=0xfc;
	delay(100);
		LED0=0xff;
		delay(100);	
	LED0=0xf3;
	delay(100);
		LED0=0xff;
		delay(100);	
	LED0=0xcf;
	delay(100);
		LED0=0xff;
		delay(100);
	LED0=0x3f;
	delay(100);
		LED0=0xff;
		delay(100);
	LED0=0x7e;
	delay(100);
		
	LED1=0xfc;
	delay(100);
		LED1=0xff;
		delay(100);	
	LED1=0xf3;
	delay(100);
		LED1=0xff;
		delay(100);	
	LED1=0xcf;
	delay(100);
		LED1=0xff;
		delay(100);
	LED1=0x3f;
	delay(100);	
		LED1=0xff;
		delay(100);
	LED1=0x7e;
	delay(100);
	
	LED2=0xfc;
	delay(100);
		LED2=0xff;
		delay(100);	
	LED2=0xf3;
	delay(100);
		LED2=0xff;
		delay(100);	
	LED2=0xcf;
	delay(100);
		LED2=0xff;
		delay(100);
	LED2=0x3f;
	delay(100);
		LED2=0xff;
		delay(100);
	LED2=0x7e;
	delay(100);	
		
	LED3=0xfc;
	delay(100);
		LED3=0xff;
		delay(100);	
	LED3=0xf3;
	delay(100);
		LED3=0xff;
		delay(100);	
	LED3=0xcf;
	delay(100);
		LED3=0xff;
		delay(100);
	LED3=0x3f;
	delay(100);	
		LED3=0xff;
		delay(100);
	LED3=0x7e;
	delay(100);
	}
 {
     
	LED0=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED0=0xbd;
	delay(100);
			LED0=0xff;
			delay(100);
	LED0=0xdb;
	delay(100);
			LED0=0xff;
			delay(100);
	LED0=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);

	LED0=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED0=0xdb;
	delay(100);
			LED0=0xff;
			delay(100);
	LED0=0xbd;
	delay(100);
			LED0=0xff;
			delay(100);
	LED0=0x7e;
	delay(100);
			LED0=0xff;
			delay(100);

	LED1=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED1=0xbd;
	delay(100);
			LED0=0xff;
			delay(100);
	LED1=0xdb;
	delay(100);
			LED0=0xff;
			delay(100);
	LED1=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);

	LED1=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED1=0xdb;
	delay(100);
			LED0=0xff;
			delay(100);
	LED1=0xbd;
	delay(100);
			LED0=0xff;
			delay(100);
	LED1=0x7e;
	delay(100);
			LED0=0xff;
			delay(100);

	LED2=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED2=0xbd;
	delay(100);
			LED0=0xff;
			delay(100);
	LED2=0xdb;
	delay(100);
			LED0=0xff;
			delay(100);
	LED2=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED2=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED2=0xdb;
	delay(100);
			LED0=0xff;
			delay(100);
	LED2=0xbd;
	delay(100);
			LED0=0xff;
			delay(100);
	LED2=0x7e;
	delay(100);
			LED0=0xff;
			delay(100);

	LED3=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED3=0xbd;
	delay(100);
			LED0=0xff;
			delay(100);
	LED3=0xdb;
	delay(100);
			LED0=0xff;
			delay(100);
	LED3=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED3=0xe7;
	delay(100);
			LED0=0xff;
			delay(100);
	LED3=0xdb;
	delay(100);
			LED0=0xff;
			delay(100);
	LED3=0xbd;
	delay(100);
			LED0=0xff;
			delay(100);
	LED3=0x7e;
	delay(100);
			LED0=0xff;
			delay(100);
	}
}
/*-----------------样式5----------------------
	效果:所有LED灯全亮,延时0.5S后熄灭,循环3次
--------------------------------------------*/
void YANG_5()    	
{
     
	for(z=0;z<3;z++)
	{
     
	LED0=0x00;
	LED1=0x00;
	LED2=0x00;
	LED3=0x00;
	delay(500);
	LED0=0xFF;
	LED1=0xFF;
	LED2=0xFF;
	LED3=0xFF;
	delay(500);
	}
}
/*-----------------样式6----------------------
	效果:先是P3口实现落幕灯效果,其次是P2,P1,P0
--------------------------------------------*/
void YANG_6()
{
     
	LED3=0XFE;   //第一颗LED灯(D1)亮
	delay(100);//延时
	LED3=0XFD;       //D2亮
	delay(100);//延时
	LED3=0XFB;       //D3亮
	delay(100);//延时
	LED3=0XF7;       //D4亮
	delay(100);//延时
	LED3=0XEF;       //D5亮
	delay(100);//延时
	LED3=0XDF;       //D6亮
	delay(100);//延时
	LED3=0XBF;       //D7亮
	delay(100);//延时
	LED3=0X7F;       //D8亮
	delay(100);//延时
	LED3=0X7E;       //D1和D8亮
	delay(100);//延时
	LED3=0X7D;       //D2和D8亮
	delay(100);//延时
	LED3=0X7B;       //D3和D8亮
	delay(100);//延时
	LED3=0X77;       //D4和D8亮
	delay(100);//延时
	LED3=0X6F;       //D5和D8亮
	delay(100);//延时
	LED3=0X5F;       //D6和D8亮
	delay(100);//延时
	LED3=0X3F;       //D7和D8亮
	delay(100);//延时
	LED3=0X3E;        //D1和D7、D8亮
	delay(100);//延时
	LED3=0X3D;        //D2和D7、D8亮
	delay(100);//延时
	LED3=0X3B;        //D3和D7、D8亮
	delay(100);//延时
	LED3=0X37;        //D4和D7、D8亮
	delay(100);//延时
	LED3=0X2F;        //D5和D7、D8亮
	delay(100);//延时
	LED3=0X1F;        //D6和D7、D8亮
	delay(100);//延时
	LED3=0X1E;        //D1和D6、D7、D8亮
	delay(100);//延时
	LED3=0X1D;        //D2和D6、D7、D8亮
	delay(100);//延时
	LED3=0X1B;        //D3和D6、D7、D8亮
	delay(100);//延时
	LED3=0X17;        //D4和D6、D7、D8亮
	delay(100);//延时
	LED3=0X0F;        //D5和D6、D7、D8亮
	delay(100);//延时
	LED3=0X0E;        //D1和D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X0D;        //D2和D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X0B;        //D3和D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X07;        //D4和D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X06;        //D1和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X05;        //D2和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X03;        //D3和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X02;        //D1和D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X01;        //D2和D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED3=0X00;        //D1、D2、D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	
	LED2=0XFE;   //第一颗LED灯(D1)亮
	delay(100);//延时
	LED2=0XFD;       //D2亮
	delay(100);//延时
	LED2=0XFB;       //D3亮
	delay(100);//延时
	LED2=0XF7;       //D4亮
	delay(100);//延时
	LED2=0XEF;       //D5亮
	delay(100);//延时
	LED2=0XDF;       //D6亮
	delay(100);//延时
	LED2=0XBF;       //D7亮
	delay(100);//延时
	LED2=0X7F;       //D8亮
	delay(100);//延时
	LED2=0X7E;       //D1和D8亮
	delay(100);//延时
	LED2=0X7D;       //D2和D8亮
	delay(100);//延时
	LED2=0X7B;       //D3和D8亮
	delay(100);//延时
	LED2=0X77;       //D4和D8亮
	delay(100);//延时
	LED2=0X6F;       //D5和D8亮
	delay(100);//延时
	LED2=0X5F;       //D6和D8亮
	delay(100);//延时
	LED2=0X3F;       //D7和D8亮
	delay(100);//延时
	LED2=0X3E;        //D1和D7、D8亮
	delay(100);//延时
	LED2=0X3D;        //D2和D7、D8亮
	delay(100);//延时
	LED2=0X3B;        //D3和D7、D8亮
	delay(100);//延时
	LED2=0X37;        //D4和D7、D8亮
	delay(100);//延时
	LED2=0X2F;        //D5和D7、D8亮
	delay(100);//延时
	LED2=0X1F;        //D6和D7、D8亮
	delay(100);//延时
	LED2=0X1E;        //D1和D6、D7、D8亮
	delay(100);//延时
	LED2=0X1D;        //D2和D6、D7、D8亮
	delay(100);//延时
	LED2=0X1B;        //D3和D6、D7、D8亮
	delay(100);//延时
	LED2=0X17;        //D4和D6、D7、D8亮
	delay(100);//延时
	LED2=0X0F;        //D5和D6、D7、D8亮
	delay(100);//延时
	LED2=0X0E;        //D1和D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X0D;        //D2和D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X0B;        //D3和D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X07;        //D4和D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X06;        //D1和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X05;        //D2和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X03;        //D3和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X02;        //D1和D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X01;        //D2和D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED2=0X00;        //D1、D2、D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	
	LED1=0XFE;   //第一颗LED灯(D1)亮
	delay(100);//延时
	LED1=0XFD;       //D2亮
	delay(100);//延时
	LED1=0XFB;       //D3亮
	delay(100);//延时
	LED1=0XF7;       //D4亮
	delay(100);//延时
	LED1=0XEF;       //D5亮
	delay(100);//延时
	LED1=0XDF;       //D6亮
	delay(100);//延时
	LED1=0XBF;       //D7亮
	delay(100);//延时
	LED1=0X7F;       //D8亮
	delay(100);//延时
	LED1=0X7E;       //D1和D8亮
	delay(100);//延时
	LED1=0X7D;       //D2和D8亮
	delay(100);//延时
	LED1=0X7B;       //D3和D8亮
	delay(100);//延时
	LED1=0X77;       //D4和D8亮
	delay(100);//延时
	LED1=0X6F;       //D5和D8亮
	delay(100);//延时
	LED1=0X5F;       //D6和D8亮
	delay(100);//延时
	LED1=0X3F;       //D7和D8亮
	delay(100);//延时
	LED1=0X3E;        //D1和D7、D8亮
	delay(100);//延时
	LED1=0X3D;        //D2和D7、D8亮
	delay(100);//延时
	LED1=0X3B;        //D3和D7、D8亮
	delay(100);//延时
	LED1=0X37;        //D4和D7、D8亮
	delay(100);//延时
	LED1=0X2F;        //D5和D7、D8亮
	delay(100);//延时
	LED1=0X1F;        //D6和D7、D8亮
	delay(100);//延时
	LED1=0X1E;        //D1和D6、D7、D8亮
	delay(100);//延时
	LED1=0X1D;        //D2和D6、D7、D8亮
	delay(100);//延时
	LED1=0X1B;        //D3和D6、D7、D8亮
	delay(100);//延时
	LED1=0X17;        //D4和D6、D7、D8亮
	delay(100);//延时
	LED1=0X0F;        //D5和D6、D7、D8亮
	delay(100);//延时
	LED1=0X0E;        //D1和D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X0D;        //D2和D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X0B;        //D3和D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X07;        //D4和D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X06;        //D1和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X05;        //D2和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X03;        //D3和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X02;        //D1和D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X01;        //D2和D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED1=0X00;        //D1、D2、D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	
	LED0=0XFE;   //第一颗LED灯(D1)亮
	delay(100);//延时
	LED0=0XFD;       //D2亮
	delay(100);//延时
	LED0=0XFB;       //D3亮
	delay(100);//延时
	LED0=0XF7;       //D4亮
	delay(100);//延时
	LED0=0XEF;       //D5亮
	delay(100);//延时
	LED0=0XDF;       //D6亮
	delay(100);//延时
	LED0=0XBF;       //D7亮
	delay(100);//延时
	LED0=0X7F;       //D8亮
	delay(100);//延时
	LED0=0X7E;       //D1和D8亮
	delay(100);//延时
	LED0=0X7D;       //D2和D8亮
	delay(100);//延时
	LED0=0X7B;       //D3和D8亮
	delay(100);//延时
	LED0=0X77;       //D4和D8亮
	delay(100);//延时
	LED0=0X6F;       //D5和D8亮
	delay(100);//延时
	LED0=0X5F;       //D6和D8亮
	delay(100);//延时
	LED0=0X3F;       //D7和D8亮
	delay(100);//延时
	LED0=0X3E;        //D1和D7、D8亮
	delay(100);//延时
	LED0=0X3D;        //D2和D7、D8亮
	delay(100);//延时
	LED0=0X3B;        //D3和D7、D8亮
	delay(100);//延时
	LED0=0X37;        //D4和D7、D8亮
	delay(100);//延时
	LED0=0X2F;        //D5和D7、D8亮
	delay(100);//延时
	LED0=0X1F;        //D6和D7、D8亮
	delay(100);//延时
	LED0=0X1E;        //D1和D6、D7、D8亮
	delay(100);//延时
	LED0=0X1D;        //D2和D6、D7、D8亮
	delay(100);//延时
	LED0=0X1B;        //D3和D6、D7、D8亮
	delay(100);//延时
	LED0=0X17;        //D4和D6、D7、D8亮
	delay(100);//延时
	LED0=0X0F;        //D5和D6、D7、D8亮
	delay(100);//延时
	LED0=0X0E;        //D1和D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X0D;        //D2和D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X0B;        //D3和D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X07;        //D4和D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X06;        //D1和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X05;        //D2和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X03;        //D3和D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X02;        //D1和D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X01;        //D2和D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
	LED0=0X00;        //D1、D2、D3、D4、D5、D6、D7、D8亮
	delay(100);//延时
}
/*-----------------样式7----------------------
	效果:心形左右半边亮灭循环3次,然后上下半边
	      再循环亮灭3次
--------------------------------------------*/
void YANG_7()
{
     
	for(z=0;z<3;z++)
	{
     
		LED3=0X00;
		LED2=0X00;
		LED1=0XFF;
		LED0=0XFF;
		delay(300);
		LED0=0X00;
		LED1=0X00;
		LED2=0XFF;
		LED3=0XFF;
		delay(300);
	}
	for(z=0;z<3;z++)
	{
     
		LED3=0X00;
		LED0=0X00;
		LED1=0XFF;
		LED2=0XFF;
		delay(300);
		LED2=0X00;
		LED1=0X00;
		LED0=0XFF;
		LED3=0XFF;
		delay(300);
	}
}
/*-----------------样式8-------------------------
	效果:每隔1颗LED一颗灯亮,然后取反,循环效果5次
------------------------------------------------*/
void YANG_8()
{
     
	for(z=0;z<5;z++)
	{
     
		LED0=0X55;
		LED1=0X55;
		LED2=0X55;
		LED3=0X55;
		delay(200);
		LED0=~0X55;
		LED1=~0X55;
		LED2=~0X55;
		LED3=~0X55;
		delay(200);
	}
}
/*-----------------样式9-------------------------
	效果:左上右下一颗LED绕半边心往返点亮循环
------------------------------------------------*/
void YANG_9()
{
     
	for(z=0;z<5;z++)
	{
     
		LED3=0X7F;
		LED1=0X7F;
		delay(50);
		LED3=0XBF;
		LED1=0XBF;
		delay(50);
		LED3=0XDF;
		LED1=0XDF;
		delay(50);
		LED3=0XEF;
		LED1=0XEF;
		delay(50);
		LED3=0XF7;
		LED1=0XF7;
		delay(50);
		LED3=0XFB;
		LED1=0XFB;
		delay(50);
		LED3=0XFD;
		LED1=0XFD;
		delay(50);
		LED3=0XFE;
		LED1=0XFE;		
		delay(50);
		LED3=0XFF;
		LED1=0XFF;		
		LED2=0X7F;
		LED0=0X7F;
		delay(50);
		LED2=0XBF;
		LED0=0XBF;
		delay(50);
		LED2=0XDF;
		LED0=0XDF;
		delay(50);
		LED2=0XEF;
		LED0=0XEF;
		delay(50);
		LED2=0XF7;
		LED0=0XF7;
		delay(50);
		LED2=0XFB;
		LED0=0XFB;
		delay(50);
		LED2=0XFD;
		LED0=0XFD;
		delay(50);
		LED2=0XFE;
		LED0=0XFE;
		delay(50);
		LED2=0XFE;
		LED0=0XFE;
		delay(50);
		LED2=0XFD;
		LED0=0XFD;
		delay(50);
		LED2=0XFB;
		LED0=0XFB;
		delay(50);
		LED2=0XF7;
		LED0=0XF7;
		delay(50);
		LED2=0XEF;
		LED0=0XEF;
		delay(50);
		LED2=0XDF;
		LED0=0XDF;
		delay(50);
		LED2=0XBF;
		LED0=0XBF;
		delay(50);
		LED2=0X7F;
		LED0=0X7F;
		delay(50);
		LED2=0XFF;
		LED0=0XFF;
		LED3=0XFE;
		LED1=0XFE;		
		delay(50);
		LED3=0XFD;
		LED1=0XFD;
		delay(50);
		LED3=0XFB;
		LED1=0XFB;
		delay(50);
		LED3=0XF7;
		LED1=0XF7;
		delay(50);
		LED3=0XBF;
		LED1=0XBF;
		delay(50);
		LED3=0X7F;
		LED1=0X7F;
		delay(50);
	}
}
/*-----------------样式10-------------------------
	效果:P3、P1和P2、P0口所有灯光交替闪烁
------------------------------------------------*/
void YANG_10()
{
     
	for(z=0;z<3;z++)
	{
     
		LED2=0X00;
		LED0=0X00;
		delay(100);
		LED2=0XFF;
		LED0=0XFF;
		LED3=0X00;
		LED1=0X00;
		delay(100);
		LED3=0XFF;
		LED1=0XFF;
	}
}
/*-----------------样式11-------------------------
	效果:左右扭腰动作
------------------------------------------------*/
void YANG_11()
{
     
	for(z=0;z<5;z++)
	{
     
		LED3=0X7F;
		LED2=0XFE;
		LED0=0X7E;
		LED1=0XFE;
		delay(50);
		LED3=0XBF;
		LED2=0XFD;
		LED0=0XBF;
		LED1=0XFD;
		delay(50);
		LED3=0XDF;
		LED2=0XFB;
		LED0=0XDF;
		LED1=0XFB;
		delay(50);
		LED3=0XEF;
		LED2=0XF7;
		LED0=0XEF;
		LED1=0XF7;
		delay(50);
		LED3=0XF7;
		LED2=0XEF;
		LED0=0XF7;
		LED1=0XEF;
		delay(50);
		LED3=0XFB;
		LED2=0XDF;
		LED0=0XFB;
		LED1=0XDF;
		delay(50);
		LED3=0XFD;
		LED2=0XBF;
		LED0=0XFD;
		LED1=0XBF;
		delay(50);
		LED3=0XFE;
		LED2=0X7F;
		LED0=0XFE;
		LED1=0X7F;
		delay(50);
		LED3=0XFE;
		LED2=0X7F;
		LED0=0XFE;
		LED1=0X7F;
		delay(50);
		LED3=0XFD;
		LED2=0XBF;
		LED0=0XFD;
		LED1=0XBF;
		delay(50);
		LED3=0XFB;
		LED2=0XDF;
		LED0=0XFB;
		LED1=0XDF;
		delay(50);
		LED3=0XF7;
		LED2=0XEF;
		LED0=0XF7;
		LED1=0XEF;
		delay(50);
		LED3=0XEF;
		LED2=0XF7;
		LED0=0XEF;
		LED1=0XF7;
		delay(50);
		LED3=0XDF;
		LED2=0XFB;
		LED0=0XDF;
		LED1=0XFB;
		delay(50);
		LED3=0XBF;
		LED2=0XFD;
		LED0=0XBF;
		LED1=0XFD;
		delay(50);
		LED3=0X7F;
		LED2=0XFE;
		LED0=0X7E;
		LED1=0XFE;
		delay(50);
	}
}
/*-----------------样式12-------------------------
	效果:风火轮1
------------------------------------------------*/
void YANG_12()
{
     
	for(z=0;z<5;z++)
	{
     
		LED0=0X77;
		LED1=0X77;
		LED2=0X77;
		LED3=0X77;
		delay(50);
		LED0=0XBB;
		LED1=0XBB;
		LED2=0XBB;
		LED3=0XBB;
		delay(50);
		LED0=0XDD;
		LED1=0XDD;
		LED2=0XDD;
		LED3=0XDD;
		delay(50);
		LED0=0XEE;
		LED1=0XEE;
		LED2=0XEE;
		LED3=0XEE;
		delay(50);
	}
}
/*-----------------样式13-------------------------
	效果:风火轮2
------------------------------------------------*/
void YANG_13()
{
     
	for(z=0;z<5;z++)
	{
     
		LED0=0X7F;
		LED1=0X7F;
		LED2=0X7F;
		LED3=0X7F;
		delay(50);
		LED0=0XBF;
		LED1=0XBF;
		LED2=0XBF;
		LED3=0XBF;
		delay(50);
		LED0=0XDF;
		LED1=0XDF;
		LED2=0XDF;
		LED3=0XDF;
		delay(50);
		LED0=0XEF;
		LED1=0XEF;
		LED2=0XEF;
		LED3=0XEF;
		delay(50);
		LED0=0XF7;
		LED1=0XF7;
		LED2=0XF7;
		LED3=0XF7;
		delay(50);
		LED0=0XFB;
		LED1=0XFB;
		LED2=0XFB;
		LED3=0XFB;
		delay(50);
		LED0=0XFD;
		LED1=0XFD;
		LED2=0XFD;
		LED3=0XFD;
		delay(50);
		LED0=0XFE;
		LED1=0XFE;
		LED2=0XFE;
		LED3=0XFE;
		delay(50);
	}
}
/*-----------------样式14-------------------------
	效果:风火轮大集合
------------------------------------------------*/
void YANG_14()
{
     
	for(z=0;z<3;z++)
	{
     
		LED0=~0X55;
		LED1=~0X55;
		LED2=~0X55;
		LED3=~0X55;
		delay(200);
		LED0=0X55;
		LED1=0X55;
		LED2=0X55;
		LED3=0X55;
		delay(200);
	}
	for(z=0;z<3;z++)
	{
     
		LED0=0X77;
		LED1=0X77;
		LED2=0X77;
		LED3=0X77;
		delay(50);
		LED0=0XBB;
		LED1=0XBB;
		LED2=0XBB;
		LED3=0XBB;
		delay(50);
		LED0=0XDD;
		LED1=0XDD;
		LED2=0XDD;
		LED3=0XDD;
		delay(50);
		LED0=0XEE;
		LED1=0XEE;
		LED2=0XEE;
		LED3=0XEE;
		delay(50);
	}
	for(z=0;z<3;z++)
	{
     
		LED0=0X7F;
		LED1=0X7F;
		LED2=0X7F;
		LED3=0X7F;
		delay(50);
		LED0=0XBF;
		LED1=0XBF;
		LED2=0XBF;
		LED3=0XBF;
		delay(50);
		LED0=0XDF;
		LED1=0XDF;
		LED2=0XDF;
		LED3=0XDF;
		delay(50);
		LED0=0XEF;
		LED1=0XEF;
		LED2=0XEF;
		LED3=0XEF;
		delay(50);
		LED0=0XF7;
		LED1=0XF7;
		LED2=0XF7;
		LED3=0XF7;
		delay(50);
		LED0=0XFB;
		LED1=0XFB;
		LED2=0XFB;
		LED3=0XFB;
		delay(50);
		LED0=0XFD;
		LED1=0XFD;
		LED2=0XFD;
		LED3=0XFD;
		delay(50);
		LED0=0XFE;
		LED1=0XFE;
		LED2=0XFE;
		LED3=0XFE;
		delay(50);
	}
	for(z=0;z<3;z++)
	{
     
		LED0=0XFE;
		LED1=0XFE;
		LED2=0XFE;
		LED3=0XFE;
		delay(50);
		LED0=0XFD;
		LED1=0XFD;
		LED2=0XFD;
		LED3=0XFD;
		delay(50);
		LED0=0XFB;
		LED1=0XFB;
		LED2=0XFB;
		LED3=0XFB;
		delay(50);
		LED0=0XF7;
		LED1=0XF7;
		LED2=0XF7;
		LED3=0XF7;
		delay(50);
		LED0=0XEF;
		LED1=0XEF;
		LED2=0XEF;
		LED3=0XEF;
		delay(50);
		LED0=0XDF;
		LED1=0XDF;
		LED2=0XDF;
		LED3=0XDF;
		delay(50);
		LED0=0XBF;
		LED1=0XBF;
		LED2=0XBF;
		LED3=0XBF;
		delay(50);
		LED0=0X7F;
		LED1=0X7F;
		LED2=0X7F;
		LED3=0X7F;
		delay(50);
	}
	for(z=0;z<3;z++)
	{
     
		LED0=~0X55;
		LED1=~0X55;
		LED2=~0X55;
		LED3=~0X55;
		delay(200);
		LED0=0X55;
		LED1=0X55;
		LED2=0X55;
		LED3=0X55;
		delay(200);
	}
}
/*-----------------样式15----------------------
	效果:所有LED灯全亮,延时0.5S后熄灭,循环5次
--------------------------------------------*/
void YANG_15()    	
{
     
	for(z=0;z<5;z++)
	{
     
	LED0=0x00;
	LED1=0x00;
	LED2=0x00;
	LED3=0x00;
	delay(50);
	LED0=0xFF;
	LED1=0xFF;
	LED2=0xFF;
	LED3=0xFF;
	delay(50);
	}
}
void main()
{
     
	Chu();
	YANG_1();
	Chu();
	YANG_2();
	Chu();
	YANG_3();
	Chu();
	YANG_4();
	Chu();
	YANG_5();
	Chu();
	YANG_8();
	Chu();
	YANG_6();
	Chu();
	YANG_7();
	Chu();
	YANG_8();
	Chu();
	YANG_9();
	Chu();
	YANG_10();
	Chu();
	YANG_11();
	Chu();
	YANG_12();
	Chu();
	YANG_13();
	Chu();
	YANG_14();
	Chu();
	YANG_15();
}

仿真电路图如下:
单片机心形灯_第2张图片

你可能感兴趣的:(#,7.1,似水的年华流水的灯,单片机)