基于AT89C51单片机的节日彩灯门设计

1.设计任务

      本设计采用单片机为主控芯片,结合外围电路组成彩灯门的控制系统器,用来控制16个彩色的LED发光,实现彩色亮点的循环移动;通过软件编程实现各种各样的彩色亮点平面循环移动,该彩色控制器可以通过输入按钮、自动模式按钮和手动模式按钮来控制实现LED彩色十种灯光间的效果切换;还可以通过按钮暂停彩灯效果,使16个彩灯灯处于全亮的状态,再次按下相同按钮后继续之前的灯光闪烁效果。其中十种灯光效果分别是:顺时针流水灯、逆时针流水灯、交替闪烁、顺时针对角灯、逆时针对角灯、顺时针逐个点亮、顺时针逐个熄灭、逆时针逐个点亮、逆时针逐个熄灭、二进制加法。

2. 设计要求

2.1系统方案论证

根据设计任务,分析设计系统的组成,给出实现设计任务的几种方案,分析比较几种设计方案的优略,本着尽量以软件代替硬件,同时力求电路简单,工作可靠的原则,确定总体设计方案。

2.2系统硬件电路设计

根据系统设计方案进行软、硬件的分配,软、硬件设计分别进行。硬件设计包括单片机最小系统和扩展接口及配置,硬件结构在设计时要选择合适的元器件,硬件电路要简洁、工作可靠,需用Proteus绘制整个系统的电路仿真原理图。

2.3软件设计

根据该系统要求的功能进行软件设计,简述软件的功能,并根据每个模块的功能绘制软件流程图,根据流程图编写程序并汇编调试通过;列出软件清单,软件清单要求加以注释。

2.4 软硬件系统仿真

将编译后的程序软件加载到Proteus软件仿真的单片机ROM中,运行系统,实现软件程序对单片机系统的硬件电路的控制,并调试仿真结果,直至与设计任务相符。

基于AT89C51单片机的节日彩灯门设计_第1张图片

/*******************?????********************/
#include 
#define false 0
#define true 1
#define uchar unsigned char
#define uint unsigned int
sbit pause_key = P3^0;//????
sbit auto_key = P3^1;//????????
sbit change_key = P3^2; //????????

int ledCode[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//led??(????)
int ledCode2[8]={0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00};// led??(?????)
int disCode[10]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};//?????0-9
void displayLed(void);//??Led????
void keyScan(void);//????????
void Delay10ms(unsigned int n);//??10ms
bit isPause = false;//????
bit isAuto = true;//??????
bit isChange = false;//??????????
uchar time;//???0.5s
uchar types;//?????????
uint counts;//??????
void T0_INT(void) interrupt 1
{
	TL0=(65536-50000)/256;
	TH0=(65536-50000)%256;
	time++;
	if(time >= 10)//????:0.5s
	{
		time=0;
	if(isChange == true)//????????????
	{
		counts = 0;
		types++;//?????????
		if(types > 9)types = 0;
		P0 = disCode[types]; 
		isChange = false;
	}
  displayLed();
	counts++;
}
	}
void main(void)
{
	TMOD=0x61;//0110 0001 //???
	TL0=(65536-50000)/256;//50ms
	TH0=(65536-50000)%256;
	TR0=1; //??T0
	ET0=1; //T0????
	EA=1; //?????
	time = 0; //???????(0.5s)
	counts = 0; //??????
	types = 0; //???????
	pauseLed = 0; //??????
	P0=disCode[types];
	while(1)
	{
		keyScan(); //???????
	}
}
void keyScan(void)
{
	if(pause_key == 0) //???????
	{
		Delay10ms(1);
		if(pause_key == 0)
		{
			isPause = ~isPause;
			pauseLed = isPause;
			if(isPause == true)
			{
				ET0 = 0; //??T0??
				P0 = 0xfd; 
				P1 = 0x00; //????????
				P2 = 0x00;
			}else{
				ET0 =1; //T0????
				P0 = disCode[types];
				displayLed();
			}
			while(pause_key == 0); //????????
		}
	}
	if(auto_key == 0) //???????????
	{
		Delay10ms(1);
		if(auto_key == 0)
		{
			isAuto = ~isAuto;
			autoLed = isAuto;
		}
		while(auto_key == 0); //????????
	}
	if(change_key == 0&& isAuto ==false) //????,????????
	{
		Delay10ms(1);
		if(change_key == 0)
		{
			isChange = true;
		}
		while(change_key == 0); //????????
	}
}
void displayLed(void)
{
	switch(types)
	{
		case 0: //?????led?
		{
			if(counts >= 16)counts =0;
			if(counts >= 15)
			{
				if(isAuto == true)isChange = true;
			}
			if(counts <8)
			{
				P1 = 0xff;
				P2 = ledCode[7-counts];
			}
			else
			{
				P1 = ledCode[15-counts];
				P2 = 0xff;
			}
			break;
		}
		case 1: //?????led?
		{
			if(counts >= 16)counts = 0;
			if(counts >= 15)
			{
				if(isAuto == true)isChange = true;
			}
			if(counts <8)
			{
				P1 = ledCode[counts];
				P2 = 0xff;
			}
			else
			{
				P1 = 0xff;
				P2 = ledCode[counts-8];
			}
		case 2: //????
		{
			if(counts >=16)counts = 0;
			if(counts >=15)
			{
				if(isAuto == true)isChange = true;
			}
			if(counts %2 == 0) //??
			{
				P1 = 0xaa;
				P2 = 0xaa;
			}
			else
			{
				P1 = 0x55;
				P2 = 0x55;
			}
			break;
		}
		case 3: //?????
		{
			if(counts >=8)counts = 0;
			if(counts >=7)
			{
				if(isAuto == true)isChange = true;
			}
			P1 = ledCode[7- counts];
			P2 = ledCode[7- counts];
			break;
		}
		case 4: //?????
		{
			if(counts >=8)counts = 0;
			if(counts >=7)
			{
				if(isAuto == true)isChange = true;
			}
			P1 = ledCode[counts];
			P2 = ledCode[counts];
			break;
		}
		case 5: //???????
		{
			if(counts >=17)counts = 0;
			if(counts <8)
			{
				P1 = ~ledCode2[7- counts];
				P2 = 0xff;
			}
			else if(counts <16)
			{
				P1 = 0x00;
				P2 = ~ledCode2[15-counts];
			}
			else //??
			{
				P1 = 0x00;
				P2 = 0x00;
				if(isAuto == true)isChange = true;
			}
			break;
		}
		case 6: //????????
			{
			if(counts >=17)counts = 0;
			if(counts <8)
			{
				P1 = ledCode2[7- counts];
				P2 = 0x00;
			}
			else if(counts <16)
			{
				P1 = 0xff;
				P2 = ledCode2[15-counts];
			}
			else //??
			{
				P1 = 0xff;
				P2 = 0xff;
				if(isAuto == true)isChange = true;
			}
			break;
		}
		case 7: //???????
			{
			if(counts >=17)counts = 0;
			if(counts <8)
			{
				P1 = 0xff;
				P2 = ledCode2[counts];
			}
			else if(counts <16)
			{
				P1 = ledCode2[counts -7];
				P2 = 0x00;
			}
			else //??
			{
				P1 = 0x00;
				P2 = 0x00;
				if(isAuto == true)isChange = true;
			}
			break;
		}
		case 8: //???????
			{
			if(counts >=17)counts = 0;
			if(counts <8)
			{
				P1 = 0x00;
				P2 = ~ledCode2[counts];
			}
			else if(counts <16)
			{
				P1 = ~ledCode2[counts -7];
				P2 = 0xff;
			}
			else //??
			{
				P1 = 0xff;
				P2 = 0xff;
				if(isAuto == true)isChange = true;
			}
			break;
		}
		case 9: //?????
			{
			if(counts >=255)counts = 0;
			if(counts ==254 && isAuto == true)isChange = true;
			P1 = ~counts;
			P2 = ~counts;
			break;
		}
		default:
			types = 0;
		  P0 = disCode[types];
	}
}
	}
	void Delay10ms(unsigned int n)
	{
		unsigned int a,b;
		for(;n>0;n--)
		{
			for(b=38;b>0;b--)
			{
				for(a=130;a>0;a--);
			}
		}
	}

完整代码点开链接私信  免费  获取。

【iBot机器人工作室的个人空间-哔哩哔哩】 https://b23.tv/ryUWVKa

你可能感兴趣的:(单片机,节日,嵌入式硬件)