⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lcd_key_1.c

📁 基于Proteus的交通灯控制系统
💻 C
字号:
#include <reg51.h>
#define uchar unsigned char
#include <1602lcd.ini>
#define ns P2
#define ew P3
uchar led[6]={0xdd,0xfd,0xed,0xfb,0xf6,0xfe};
uchar ctime[5]={8,2,5,2,5};
uchar key,x=0,tround=0,sed=0,min=0,sedpre;
uchar ledstate=0,ledsed=0;
bit nsew=0;
bit execute=0;		   //执行
bit upled=0;
bit d_pause=0;
uchar manage=0;
uchar cnt1,cnt2,indata;
uchar insed[3],inN,discount;
//-----------------------------------------------------------------------------------
void IT0R(void) interrupt 1 using 1
{
TH0=0x3c;
TL0=0xb0;
tround++;
if (tround==10) {                        //1S时,更新时间
                 tround=0;
                 sed++;ledsed--;
				 if (ledsed==255) ledsed=0;
                 if (sed==60) {sed=0;min++;}
                 }

}

//------------------------------------------------------------------------------------

void delay(uchar ms){
uchar i;
while(ms--){
for(i=0;i<125;i++);
}
}

//------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------
int getkey(){
return ((~P1)&0xf0);
}
//-----------------------------------------------------------------------------------
//判断是否有键按下
void chkkey(){
if (getkey()) {
delay(0x2);          //延时,去抖
delay(0x02);
}
}

//-----------------------------------------------------------------------------------
void findkey(){ //扫描法得出键值
uchar i,k,j;
i=(~P1)&0xf0;
j=0x10;
for (k=0;k<4;k++) {
if (i&j) key=k;      //得出行号
j<<=1;
}
key=key*4;
P1=0xfe;
if ((~P1)&0xf0) key=key;    //得出行键值=行号+列号
P1=0xfd;
if ((~P1)&0xf0) key=key+1;
P1=0xfb;
if ((~P1)&0xf0) key=key+2;
P1=0xf7;
if ((~P1)&0xf0) key=key+3;
//}
P1=0xf0;
}
//------------------------------------------------------------------------------------
void disstate(){
Set_LCM_XY(0,0);
if (!nsew) Send_String_LCM("NS:");
    else Send_String_LCM("EW:");
Set_LCM_XY(3,0);
switch (ledstate){
case 0:{Send_String_LCM("G+M     ");break;}
case 1:{Send_String_LCM("G       ");break;}
case 2:{Send_String_LCM("G+Rt    ");break;}
case 3:{Send_String_LCM("Y       ");break;}
case 4:{Send_String_LCM("R+Lt    ");break;}
case 5:{Send_String_LCM("StopAll ");break;}



}
}

//------------------------------------------------------------------------------------
void Updatedis(uchar i){
if (i!=5){
ledsed=ctime[i];
if (!nsew) {ns=led[i];ew=led[5];}	   //2
    else {ew=led[i];ns=led[5];}
if (manage==0) disstate();
ledstate++;
}
  else {  
  	    nsew=~nsew;
		ledstate=0;
		}
}
//------------------------------------------------------------------------------------
void trafficlamp()
{
if (ledstate==0) Updatedis(0);
  else { if (ledsed==0)  Updatedis(ledstate);}

}
//-----------------------------------------------------------------------------------
void discnt(){
Send_Command_LCM(0x01);
Set_LCM_XY(0,0);
if (manage==1) {
                Send_String_LCM("Regulate Ex-Time:");
				inN=0;
				Set_LCM_XY(0,1);
				switch (cnt1){
				case 1:{Send_String_LCM("Green+M    :");break;}
				case 2:{Send_String_LCM("Green      :");break;}
				case 3:{Send_String_LCM("Green+Right:");break;}
				case 4:{Send_String_LCM("Yellow     :");break;}
				case 5:{Send_String_LCM("Red+Light  :");break;}
				case 6:{Send_String_LCM("StopAll    :");break;}
				}
				Send_Command_LCM(0x0f);
				Send_Num_LCM(ctime[cnt1-1]/100);	//X=12
				Send_Num_LCM((ctime[cnt1-1]%100)/10);	  //X=13
				Send_Num_LCM(ctime[cnt1-1]%10);	  //X=14
				Set_LCM_XY(12,1);
				 return;
                 }
if (manage==2) {
                Send_String_LCM("Emergency:");
				Set_LCM_XY(0,1);
				switch (cnt2){
				case 1:{Send_String_LCM("1.All Red ");break;}
				case 2:{Send_String_LCM("2.EW Stopped");break;}
				case 3:{Send_String_LCM("3.NS Stopped");break;}
				 }

				 return;
                 }
if (manage==3) {
                Send_String_LCM("Pause:");
				Set_LCM_XY(0,1);
				Send_String_LCM("Are you sure?");
				return;
                 }

}
//-----------------------------------------------------------------------------------
void keyfun(){
uchar i;
if (key<=9) {if (manage!=1) return;
               else {			 //输入数字
			        Send_Num_LCM(key);
					insed[inN]=key;
					inN++;
					if (inN>=3) {inN=0;Set_LCM_XY(12,1);}



					}

			 return;
			 }
if (key==10) {						   //调整交通灯切换时间
              
              manage=1;
			  cnt1++;
			  if (cnt1>=6) cnt1=1;
			  discnt();
			  return;
			  }
if (key==11) {
			  manage=2;				   //紧急状态
			  cnt2++;
			  if (cnt2>=4) cnt2=1;
			  discnt();

              return;
		      }
if (key==12) {						  //清零
              manage=3;
			  discnt();
			  return;
			  }

if (key==13) {return;}				 //预留


if (key==14) {						 //确定键
			  switch (manage){
							 case 0:{return;}
							 case 1:{manage=0;
									 i=insed[0]*100+insed[1]*10+insed[2];
									 if (i>255) i=255;
									 ctime[cnt1-1]=i;
									 Send_Command_LCM(0x0c);
									 Send_Command_LCM(0x01);
									 cnt1=0;
									 break;
									 }
							 case 2:{
							         execute=1;									 
									 upled=1;
									 break;
									 }

							 case 3:{
									 execute=1;
									 upled=1;
									 break;
									 }

							 }	//switch
			 } //if 
if (key==15) {
			  Send_Command_LCM(0x0c);
			  Send_Command_LCM(0x01);
			  execute=0;
			  manage=0;
			  upled=0;
			  ctime[0]=8;
			  ctime[1]=2;
			  ctime[2]=5;
			  ctime[3]=2;
			  ctime[4]=5;
			  //ctime[5]={8,2,5,2,5}; //恢复正常工作
              return;
			  }						  //恢复键,取消键
}
//-----------------------------------------------------------------------------------
void distime(){
if (sedpre!=sed) {
 				  Set_LCM_XY(0,1);
				  Send_Num_LCM(min/10);
				  Send_Num_LCM(min%10);
				  Send_String_LCM(":");
				  //Send_String_LCM(sed);
				  Send_Num_LCM(sed/10);
				  Send_Num_LCM(sed%10);
				  sedpre=sed;
				  Set_LCM_XY(13,0);
				  Send_Num_LCM(ledsed/100);
				  Send_Num_LCM((ledsed%100)/10);
				  Send_Num_LCM(ledsed%10);
				  }
}
//-----------------------------------------------------------------------------------
dis_emergency(){
Send_Command_LCM(0x01);
Set_LCM_XY(0,0);
Send_String_LCM("Now Emergency:");
Set_LCM_XY(0,1);
switch (cnt2){
				case 1:{Send_String_LCM("All Red ");break;}
				case 2:{Send_String_LCM("EW Stopped");break;}
				case 3:{Send_String_LCM("NS Stopped");break;}
			 }

}
//-----------------------------------------------------------------------------------
dis_pause(){
if (upled) {
Send_Command_LCM(0x01);
Set_LCM_XY(0,0);
Send_String_LCM("Now Pause");
upled=0;
}

}
//-----------------------------------------------------------------------------------
void emergency(){
if (manage==0) {execute=0;return;}
if (!upled) return;

dis_emergency();
upled=0;
if (cnt2==1) {ns=0xfe;ew=0xfe;return;}
if (cnt2==2) {ns=0xdd;ew=0xfe;return;}
if (cnt2==1) {ns=0xfe;ew=0xdd;return;}


}
//------------------------------------------------------------------------------------
void pause(){
if (!execute)  return;
discount++;
if (discount>=80) {d_pause=!d_pause;discount=0;}
if (d_pause) {ns=0xfb;ew=0xfb;}
   else {ns=0xff;ew=0xff;}

}
//------------------------------------------------------------------------------------
main(){
Init_LCM();
P1=0xf0;
Set_LCM_XY(0,0);
Send_String_LCM("Initing.");
//Set_LCM_XY(0,1);
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
ET0=1;
EA=1;
P2=0x00;
P3=0x00;
TR0=1;
while (1){
if (!getkey()) P1=0xf0;
chkkey();      //判断是否有键按下
if (getkey()) {    //若有键按下
              findkey();    //求键值
              keyfun();
            }
while (getkey()) {};
if (!execute) trafficlamp();

if (manage==0) distime();
if (manage==2) emergency();
if (manage==3) {pause();dis_pause();}

} //while(1)




} //main()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -