用51单片机实现爱心跑马灯程序

用51单片机实现爱心跑马灯程序


本人闲来无事做,见实验室的小伙伴用LED等做了一个跑马灯,顿时引来了一大群妹子,当时就不服了,哼,不就是一个流水灯,说的跟谁不会一样


制作的是32个LED灯的爱心流水灯,至于灯怎么安放,我就不多说了,关键是怎么连接布线。


由于条件有限,没有制作PCB的工具,所以就用的洞洞板,32个LED共阴极,连接在51单片机的GND上,每个LED的正极分别连接单片的一个引脚,


我的所有线路全是用电烙铁焊接的,线路丑爆了,在此就不上图了,为了后续程序好写一点,LED的正极和单片机的引脚的连线最好多考虑一下


好了,废话不多说了,直接上代码,只做了几个动画,可以按照自己的想法再添加


#include 
#include 
#include 

void liushui();
void rjl();	   //渐亮
void rja();	   //渐暗
void huaxin();
void huaxin2();
void huaxin3();
void zkb(unsigned char d);
void ys1ms(unsigned int s);	 //延时声明,修改s的数值,就可以改变时间

unsigned char code table[11]={0xff,0xff ,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};
unsigned char code table6[11]={0xff,0xff ,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff};
unsigned char code table2[8]={0x7f,0x3f ,0x1f,0x0f,0x07,0x03,0x01,0x00};
unsigned char code table3[8]={0xfe,0xfc ,0xf8,0xf0,0xe0,0xc0,0x80,0x00};
unsigned char code table4[9]={0x00,0x01 ,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
unsigned char code table5[9]={0x00,0x80 ,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff};

unsigned char i; 
unsigned char j; 
unsigned char k;
unsigned char m;

int main()
{
	EA=1;//打开中断,使用中断方便调试查看模式
	EX0=1; //允许外部中断0中断
	EX1=1; //允许外部中断1中断
	PX0=1; // 外部中断0位高优先级
	while(1)  //无限循环
	{	
		liushui();   
		ys1ms(50);	
		huaxin2();   
		ys1ms(50);
		rja();
		ys1ms(100);

		huaxin();	 
		rjl();
		rja();
		ys1ms(100);

		huaxin3();   
		rja();
		ys1ms(100);

	}
	 return 0;
}

void liushui()
{
	
		for(i = 9; i > 0; i--)
		{
			P1 = table[i];
			ys1ms(50);
		}
		for(i = 2; i <= 10; i++)
		{
			P0 = table[i];
			 ys1ms(50);
		}
		for(i = 9; i > 0; i--)
		{
			P2 = table[i];
			 ys1ms(50);
		}
		for(i = 9; i > 0; i--)
		{
			P3 = table[i];
			 ys1ms(50);
		}
		
}

 void ys1ms(unsigned int s)	   //定时器精确延时1毫秒函数,改变S数值即改变时间
 {
 	TR0=1;
	while(s!=0)
	{
		TH0=-(7629/256);
		TL0=-(7629%256);
		while(TF0!=1);
		TF0=0;
		s--;
	}
	TR0=0;
 }

 /****************************************************************************/
void rjl()  //渐亮函数 
{   
    unsigned char a, b;//a控制延时长度 b控制循环周期      
	P0 = 0xff;
	P1 = 0xff;
	P2 = 0xff;
	P3 = 0xff;
    a = 0;   
   		for (b=250; b>0; b--)   //循环周期设置
    	{   
            a++;   
            zkb (a);      //延时长度随a而改变    
            P0 = 0xff;
			P1 = 0xff;
			P2 = 0xff;
			P3 = 0xff;
            a = ~a;         //a值取反,决定灯灭时的占空比    
            zkb (a);   
            P0 = 0x00;
			P1 = 0x00;
			P2 = 0x00;
			P3 = 0x00;
	        a = ~a;         //a值取反 使a回到原值继续循环 
         }
 }
/****************************************************************************/
void rja()  //渐暗函数
{
	unsigned char a, b;//a控制延时长度 b控制循环周期    
    P0 = 0x00;
	P1 = 0x00;
	P2 = 0x00;
	P3 = 0x00;            //亮起LED 
	a = 0;   
        for (b=250; b>0; b--)   
        {   
            a++;   
            zkb (a);   
            P0 = 0x00;
			P1 = 0x00;
			P2 = 0x00;
			P3 = 0x00;
	        a = ~a;   
            zkb (a);   
            P0 = 0xff;
			P1 = 0xff;
			P2 = 0xff;
			P3 = 0xff;  
	        a = ~a;   
        }
}
void huaxin()
{
	for(j = 0; j < 2; j++)
	{
	  	for(i = 0; i < 8; i++)
		{
			P1 = table2[i];
			P3 = table3[i];
			ys1ms(50);
		}
		for(i = 0; i < 8; i++)
		{
			P0 = table3[i];
			P2 = table3[i];
			 ys1ms(50);
		}
		for(i = 0; i < 9; i++)  
		{
			P2 = table5[i];
			P0 = table5[i];
			ys1ms(50);
		}
		for(i = 0; i < 9; i++)
		{
			P3 = table5[i];
			P1 = table4[i];
			 ys1ms(50);
		}
		ys1ms(100);
	}
}
void huaxin2()
{
	 	for(i = 0; i < 8; i++)
		{
			P1 = table2[i];
			ys1ms(50);
		}
		for(i = 0; i < 8; i++)
		{
			P0 = table3[i];
			 ys1ms(50);
		}
		 for(i = 0; i < 8; i++)
		{
			P2 = table2[i];
			
			ys1ms(50);
		}
		for(i = 0; i < 8; i++)
		{
			P3 = table2[i];
			
			 ys1ms(50);
		}
}
void huaxin3()
{
	k = 0;
	for(m = 0; m < 2; m++)
	{
		for(j = 0; j < 4; j++)
		{
	 		for(i = 9; i > 0; i--)
			{
				P1 = table[i];
				P0 = table6[i];
				P2 = table[i];
				P3 = table[i];
				ys1ms(50);
			}
		}

			for(i = 0; i < 8; i++)
			{
				P1 = table2[i];
				P0 = table3[i];
				P2 = table2[i];
				P3 = table2[i];
				ys1ms(50);
			}
		if(k == 0)
		{
			for(i = 0; i < 8; i++)
			{
			    P1 = table5[i];
				P0 = table4[i];
				P2 = table5[i];
				P3 = table5[i];
				ys1ms(50);
			}
		}
			ys1ms(100);	
			k++;
	}
}
/****************************************************************************/
void zkb(unsigned char d)   //占空比函数,由d决定延时长度    
{      
    unsigned char i;   
    while( --d != 0)   
    {   
        for(i = 0; i < 1; i++);   //占空比时间设置
    }                         
} 
/****************************************************************************/  

	 		 	
到此,把妹神器就完成了,快去送给妹子吧

你可能感兴趣的:(C/C++)