资源描述:
《基于51单片机的密码锁》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#defineuintunsignedint#defineucharunsignedcharsbitled=P3^4;ucharflag,flag1,flag2,flag3,conflag;ucharcodeTab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x8e,0xff};voiddelay(ucharms)//扫描法实现对矩阵键盘键值的扫描,右更为简单的线反转法也可实现{uchari;while
2、(ms--){for(i=0;i<250;i++){_nop_();_nop_();_nop_();_nop_();}}}ucharkeyscan(){uchartemp,key;P1=0xfe;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;temp=temp&0xf0;if(temp!=0xf0){temp=P1;switch(temp){case0xee:key=1;break;case0xde:key=2;break;case0xbe:key=3;break;cas
3、e0x7e:key=4;break;}while(temp!=0xf0)//等待按键松开{temp=P1;temp=temp&0xf0;}}}P1=0xfd;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;temp=temp&0xf0;if(temp!=0xf0){temp=P1;switch(temp){case0xed:key=5;break;case0xdd:key=6;break;case0xbd:key=7;break;case0x7d:key=8;break;}
4、while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}P1=0xfb;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;temp=temp&0xf0;if(temp!=0xf0){temp=P1;switch(temp){case0xeb:key=9;break;case0xdb:key=10;break;case0xbb:key=11;break;case0x7b:key=12;break;}while(temp!=0xf0){temp=P
5、1;temp=temp&0xf0;}}}P1=0xf7;temp=P1;temp=temp&0xf0;if(temp!=0xf0){delay(10);temp=P1;temp=temp&0xf0;if(temp!=0xf0){temp=P1;switch(temp){case0xe7:key=13;break;case0xd7:key=14;break;case0xb7:key=15;break;case0x77:key=16;break;}while(temp!=0xf0){temp=P1;temp=temp&0xf0;}}}ret
6、urnkey;}voiddisplay(ucharm,ucharn,ucharx,uchary){P0=Tab[m];P2=0xfe;delay(1);//小于1ms才能产生人眼的视觉暂留P0=Tab[n];P2=0xfd;delay(1);P0=Tab[x];P2=0xfb;delay(1);P0=Tab[y];P2=0xf7;delay(1);}voidmain(){uchartemp,key_Value,key_Value1,key_Value2,key_Value3;while(1){temp=keyscan();if(temp
7、==1){key_Value=temp;display(key_Value,11,11,11);flag=1;}if(flag==1){temp=keyscan();if(temp==2){key_Value1=temp;display(key_Value,key_Value1,11,11);flag1=1;}}if(flag1==1){temp=keyscan();if(temp==3){key_Value2=temp;display(key_Value,key_Value1,key_Value2,11);flag2=1;}}if(f
8、lag2==1){temp=keyscan();if(temp==4){key_Value3=temp;display(key_Value,key_Value1,key_Value2,key_Value3)