- /*********************************************************************
- 文件名:矩阵键盘密码锁.c
- 晶振频率:12MHz
- 内容:模拟密码锁,密码固化到ROM 不能通过按键输入更改
- 键值分布图:
- 0 4 8 12
- 1 5 9 13
- 2 6 10 14
- 3 7 11 15
- **********************************************************************/
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define dataPort P0
- #define keyPort P1
- uchar tempdata[8];
- uchar code weima[]={
- 0xfe,0xfd,0xfb,0xf7,
- 0xef,0xdf,0xbf,0x7f};
- uchar code duanma[]={
- 0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- uchar code password[8]={
- 1,2,3,4,5,6,7,8};
- sbit dula=P2^2;
- sbit wela=P2^3;
- uchar keyScan(void);
- uchar keyPro(void);
- void delayUs2x(uchar);
- void delayMs(uchar);
- void Init_T0(void);
- void display(uchar,uchar);
- void main(void)
- {
- uchar num,i,j;
- uchar temp[8];
- bit flag;
- Init_T0();
- while(1)
- {
- num=keyPro();
- if(num!=0xff)
- {
- if(i==0)
- for(j=0;j<8;j++) //清屏
- tempdata[j]=0;
- if(i<8)
- {
- temp[i]=duanma[num];
- for(j=0;j<=i;j++)
- tempdata[7-i+j]=temp[j];
- }
- i++;
- if(i==9)
- {
- i=0;
- flag=1;
- for(j=0;j<8;j++)
- flag=flag&&(temp[j]==duanma[password[j]]);
- for(j=0;j<8;j++) //清屏
- tempdata[j]=0;
- if(flag)
- {
- tempdata[0]=0x3f;//0
- tempdata[1]=0x73;//P
- tempdata[2]=0x79;//E
- tempdata[3]=0x54;//n
- }
- else
- {
- tempdata[0]=0x79;//E
- tempdata[1]=0x50;//r
- tempdata[2]=0x50;//r
- }
- }
- }
- }
- }
- uchar keyScan(void)
- {
- uchar cord_h,cord_l;
- keyPort=0x0f; //行作为输入线,列作为输出线
- cord_l=keyPort&0x0f;
- if(cord_l!=0x0f)
- {
- delayMs(10);
- if(cord_l!=0x0f)
- cord_l=keyPort&0x0f;
- keyPort=cord_l|0xf0;
- cord_h=keyPort&0xf0;
- while(cord_h!=0xf0);
- return (cord_l+cord_h);
- }
- return 0xff;
- }
- uchar keyPro(void)
- {
- switch(keyScan())
- {
- case 0x7e:return 0;break;
- case 0x7d:return 1;break;
- case 0x7b:return 2;break;
- case 0x77:return 3;break;
- case 0xbe:return 4;break;
- case 0xbd:return 5;break;
- case 0xbb:return 6;break;
- case 0xb7:return 7;break;
- case 0xde:return 8;break;
- case 0xdd:return 9;break;
- case 0xdb:return 10;break;
- case 0xd7:return 11;break;
- case 0xee:return 12;break;
- case 0xed:return 13;break;
- case 0xeb:return 14;break;
- case 0xe7:return 15;break;
- default :return 0xff;break;
- }
- }
- void Init_T0(void)
- {
- TMOD|=0x01;
- TH0=(65536-2000)/256;
- TL0=(65536-2000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void T0_isr(void)interrupt 1
- {
- TH0=(65536-2000)/256;
- TL0=(65536-2000)%256;
- display(0,8);
- }
- void delayUs2x(uchar Us)
- {
- while(Us--);
- }
- void delayMs(uchar Ms)
- {
- while(Ms--)
- {
- delayUs2x(245);
- delayUs2x(245);
- }
- }
- void display(uchar firstBit,uchar num)
- {
- uchar i;
-
- dataPort=0;
- dula=1;
- dula=0;
- dataPort=weima[i+firstBit];
- wela=1;
- wela=0;
- dataPort=tempdata[i];
- dula=1;
- dula=0;
- i++;
- if(i==num)
- i=0;
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
改成
- if((keyPort&0xf0)!=0xf0)
复制代码即- uchar keyScan(void)
- {
- uchar cord_h,cord_l;
- keyPort=0x0f; //行作为输入线,列作为输出线
- cord_l=keyPort&0x0f;
- if(cord_l!=0x0f)
- {
- delayMs(10);
- if(cord_l!=0x0f)
- cord_l=keyPort&0x0f;
- keyPort=cord_l|0xf0;
- cord_h=keyPort&0xf0;
- while((keyPort&0xf0)!=0xf0);
- return (cord_l+cord_h);
- }
- return 0xff;
- }
复制代码一周热门 更多>