|
#include <reg52.h>typedef unsigned char u8;typedef unsigned int u16;//sbit 特殊功能位声明:声明某特殊功能寄存器中的一位#define key P1 //按键io口sbit fen=P3^0; //蜂鸣器io口sbit rs=P3^1; //LCD控制口sbit rw=P3^2;sbit e=P3^3;sbit led=P3^4; //LED灯io口//bit 位变量声明:定义一个位变量的值u8 keyz=17,j=0,biao1=0,a=10,b=10,c=10,d=10;bit biao=0;u16 zh;u8 shu[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};u8 zi[]={"lnput password:"};u8 num[]={"0123456789ABCDEF"};void delay(u16 i){ while(i--); }/*蜂鸣器*/void Fen(u8 a,b) //蜂鸣器发声函数{ u8 i,j; for(i=0;i<a;i++) { for(j=0;j<100;j++) {fen=~fen;delay(b);} } }/*LCD*/void writecom(u8 com){ //LCD写指令 rs=0; rw=0; e=0; P0=com; delay(5); e=1; e=0; }void writedat(u8 dat){ //LCD写数据 rs=1; rw=0; e=0; P0=dat; delay(5); e=1; e=0; }void initlcd(){ //LCD初始化函数 writecom(0x38); //显示模式设置 writecom(0x0c); writecom(0x06); }void xian(){ //LCD显示数字函数 writedat(num[keyz]); if(a==10){a=keyz; } //对abcd,四位数的单独赋值 else if(b==10){b=keyz;} else if(c==10){c=keyz;} else{d=keyz;} if(j!=3){j++;}biao=0;}/*键盘*/void Key(){u8 a=0; //矩形式键盘函数 if(key!=0x0f){delay(1000); if(key!=0x0f){ key=0x0f; switch(key){ case 0x07: keyz=0;biao=1;break; case 0x0b: keyz=1;biao=1;break; case 0x0d: keyz=2;biao=1;break; case 0x0e: keyz=3;biao=1;break; } key=0xf0; switch(key){ case 0x70: keyz=keyz;biao=1;break; case 0xb0: keyz=keyz+4;biao=1;break; case 0xd0: keyz=keyz+8;biao=1;break; case 0xe0: keyz=keyz+12;biao=1;break; } } while((a<50)&&(key!=0xf0)){a++;delay(10000);} } } void shi(){u8 i; // 按键值判断函数 if(biao&&keyz!=15){ //判断标志位biao是否为1,为1则有按键操作并且按键的值不等于15 writecom(0x80+0x40+j); //数字显示位置进行累加操作 if(j<0){j=0;} //数字显示位置边界 switch(keyz){ //switch判断按键并执行对应操作 case 0:xian(); break; case 1:xian(); break; case 2:xian(); break; case 3:xian(); break; case 4:xian(); break; case 5:xian(); break; case 6:xian(); break; case 7:xian(); break; case 8:xian(); break; case 9:xian(); break; case 10:writedat(num[keyz]); j++;biao=0;break; case 11:writedat(num[keyz]); j++;biao=0;break; case 12: biao1=0; //重置标志位biao1,上锁 biao=0;break; case 13:writecom(0x01); delay(1000); //全屏清除 for(i=0;i<sizeof(zi);i++){ writecom(0x80+i); writedat(zi[i]); } j=0;biao=0;break; case 14: //对当前数字位删除的操作 writedat(' '); j--; writecom(0x80+0x40+j);writedat(' '); biao=0;break; case 15: //确定按键 j++;biao=0;break; } } if(keyz==15){ //判断按键值如果等于15,便视为按下确定键 writecom(0x01);delay(1000); zh=a*1000+b*100+c*10+d; //将ABCD,四位数合并并进行判断是否为正确密码 if(zh==1234){ //设密码为1234 j=0; biao1=1; //将biao1标志位置1便为密码正确 keyz=17; //使按键值跳出最大值15便不会循坏显示 } else{ j=0; biao1=0; //反之密码错误 keyz=17; Fen(20,200); //蜂鸣器报警 } zh=0;a=10,b=10,c=10,d=10; //重置ABCD变量 for(i=0;i<sizeof(zi);i++){ writecom(0x80+i); writedat(zi[i]); } } } void main(){u8 i; led=1; initlcd(); //LCD初始化函数 for(i=0;i<sizeof(zi);i++){ writecom(0x80+i); writedat(zi[i]); } while(1){ Key(); //定义键盘函数 shi(); //按键判断函数,灯亮或报警 P2=shu[keyz]; //数码管显示当前输入值 if(biao1==1){ led=0; //判断标志位biao1 为1则密码正确显示ok writecom(0x80+0x40+10); writedat('O'); writecom(0x80+0x41+10); writedat('K'); } else if(biao1==0){led=1; //biao1为0则显示密码错误显示no writecom(0x80+0x40+10); writedat('N'); writecom(0x80+0x41+10); writedat('O');} } }
|
|