资讯详情

矩阵键盘密码锁

#include<reg52.h>#include<intrins.h>#define uint unsigned int#define uchar unsigned char#define ulong unsigned long//这三个引脚参考资料sbit E=P2^7; //1602使能引脚sbit RW=P2^6; //1602读写引脚sbit RS=P2^5; //1602数据/命令选择引脚void Delay_1ms(uint i){uint x,j;for(j=0;j<i;j++)for(x=0;x<=148;x++);}void delay(){_nop_();_nop_();_nop_();_nop_();_nop_();}bit Busy(void)//状态判断{bit busy_flag = 0;RS = 0;RW = 1;E = 1;delay(); busy_flag = (bit)(P0 & 0x80); E = 0;return busy_flag;}void wcmd(uchar del) // 功能 : 1602命令函数{while(Busy());RS = 0;RW = 0;E = 0;delay();P0 = del;delay();E = 1;delay();E = 0;}void wdata(uchar del) //功能 : 1602写数据函数{while(Busy());RS = 1;RW = 0;E = 0;delay();P0 = del;delay();E = 1;delay();E = 0;}void L1602_init(void) // 功能 : 1602初始化{wcmd(0x38);Delay_1ms(5);wcmd(0x38);Delay_1ms(5);wcmd(0x38);Delay_1ms(5);wcmd(0x38);wcmd(0x08);wcmd(0x0c);wcmd(0x04);wcmd(0x01);}void L1602_char(uchar hang,uchar lie,char sign){uchar a;if(hang == 1) a = 0x80;if(hang == 2) a = 0xc0;a = a + lie - 1;wcmd(a);wdata(sign);}void L1602_string(uchar hang,uchar lie,uchar *p){uchar a,b=0;if(hang == 1)a = 0x80;if(hang == 2)a = 0xc0;a = a + lie - 1;while(1){wcmd(a++);b++;if((*p == '\0')||(b==16)) break;wdata(*p);p++;}}uchar Keyscan(){uchar num,temp;while(1){P3=0xfe;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay();temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case 0xee:num=0;break;case 0xde:num=1;break;case 0xbe:num=2;break;case 0x7e:num=3;break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}return num;}}P3=0xfd;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay();temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case 0xed:num=4;break;case 0xdd:num=5;break;case 0xbd:num=6;break;case 0x7d:num=7;break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}return num;}}P3=0xfb;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay();temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case 0xeb:num=8;break;case 0xdb:num=9;break;case 0xbb:num=10;break;case 0x7b:num=11;break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}return num;}}P3=0xf7;temp=P3;temp=temp&0xf0;while(temp!=0xf0){delay();temp=P3;temp=temp&0xf0;while(temp!=0xf0){temp=P3;switch(temp){case 0xe7:num=12;break;case 0xd7:num=13;break;case 0xb7:num=14;break;case 0x77:num=15;break;}while(temp!=0xf0){temp=P3;temp=temp&0xf0;}return num;}}}}void Main(void){uchar i=6,j=0;uchar SLED[6]={0}; //读出的键值ulong Key_Value;ulong code6wei = 0x123456; //16进制6位数ulong codebijiao = 0;L1602_init();L1602_string(1,1," The code is:");while(1){Key_Value=10;Key_Value = Keyscan();if(Key_Value < 10)L1602_char(2,i,Key_Value + 48);elseL1602_char(2,i,Key_Value + 87);codebijiao = codebijiao | (Key_Value <<((5-j)*4));i++;j++;Delay_1ms(300);if(j==6){if(codebijiao == code6wei){wcmd(0x01);L1602_string(1,1,"right!!!");L1602_string(2,1,"welcome back!!!");while(1);}else{wcmd(0x01);L1602_string(1,1,"wrong!!!");L1602_string(2,1,"Input afresh!!!");Delay_1ms(2000);j = 0;i = 6;wcmd(0x01);L1602_string(1,1," The code is:");}}}}
-电子元器件采购网(www.ruidan.com)是本土元器件目录分销商,采用“小批量、现货、样品”销售模式,致力于满足客户多型号、高质量、快速交付的采购需求。 自建高效智能仓储,拥有自营库存超过50,000种,提供一站式正品现货采购、个性化解决方案、选型替代等多元化服务。
锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台