//还有主函数定时器部分,见另一篇文章
//下面的if语句可改成switch-case
//有问题可以评论或私信
//学习自b站江科大自化协
#include <REGX52.H>
unsigned char Key_KeyNumber;
unsigned char Key_Back()
{
unsigned char temp;
temp=Key_KeyNumber;
Key_KeyNumber=0;
return temp;
}
unsigned char KEY()
{
unsigned char KeyNumber=0;
P1=0xFF;
P1_3=0;
if(P1_4==0) KeyNumber=1;
if(P1_5==0) KeyNumber=5;
if(P1_6==0) KeyNumber=9;
if(P1_7==0) KeyNumber=13;
P1=0xFF;
P1_2=0;
if(P1_4==0) KeyNumber=2;
if(P1_5==0) KeyNumber=6;
if(P1_6==0) KeyNumber=10;
if(P1_7==0) KeyNumber=14;
P1=0xFF;
P1_1=0;
if(P1_4==0) KeyNumber=3;
if(P1_5==0) KeyNumber=7;
if(P1_6==0) KeyNumber=11;
if(P1_7==0) KeyNumber=15;
P1=0xFF;
P1_0=0;
if(P1_4==0) KeyNumber=4;
if(P1_5==0) KeyNumber=8;
if(P1_6==0) KeyNumber=12;
if(P1_7==0) KeyNumber=16;
return KeyNumber;
}
void KEY_main()
{
static unsigned char Nowdata,Lastdata;
Lastdata=Nowdata;
Nowdata=KEY();
if(Nowdata==0&&Lastdata==1) Key_KeyNumber=1;
if(Nowdata==0&&Lastdata==2) Key_KeyNumber=2;
if(Nowdata==0&&Lastdata==3) Key_KeyNumber=3;
if(Nowdata==0&&Lastdata==4) Key_KeyNumber=4;
if(Nowdata==0&&Lastdata==5) Key_KeyNumber=5;
if(Nowdata==0&&Lastdata==6) Key_KeyNumber=6;
if(Nowdata==0&&Lastdata==7) Key_KeyNumber=7;
if(Nowdata==0&&Lastdata==8) Key_KeyNumber=8;
if(Nowdata==0&&Lastdata==9) Key_KeyNumber=9;
if(Nowdata==0&&Lastdata==10) Key_KeyNumber=10;
if(Nowdata==0&&Lastdata==11) Key_KeyNumber=11;
if(Nowdata==0&&Lastdata==12) Key_KeyNumber=12;
if(Nowdata==0&&Lastdata==13) Key_KeyNumber=13;
if(Nowdata==0&&Lastdata==14) Key_KeyNumber=14;
if(Nowdata==0&&Lastdata==15) Key_KeyNumber=15;
if(Nowdata==0&&Lastdata==16) Key_KeyNumber=16;
}