[img]file:///C:UsersAdministratorAppDataRoamingTencentUsers954386791QQWinTempRichOle)CTNI[2P@894F06B4(81_BH.png[/img]
电子密码锁程序代码感觉是没错的,不过也附上
#include<reg51.h>#define uchar unsigned char#define uint unsigned int #define LCDDataBus P2 //定义LCD数据总线sbit RS = P0^1;sbit RW = P0^2;sbit EP = P0^3;sbit unlock = P3^0;uchar Keyword[6]="111211"; //初始密码 // 延时函数void delay(uint k){ unsigned char i; for(; k > 0; k--) for(i = 121; i > 0; i--);} //===================================// LCD部分//=================================== // 写数据函数void LCDWriteData(uchar Data){ RS=1; //传输数据 RW=0; //写 LCDDataBus=Data; EP=1; delay(1); EP=0;} // 写指令函数void LCDWriteCmd(uchar Cmd){ RS=0; //传输指令 RW=0; LCDDataBus=Cmd; EP=1; delay(1); EP=0;} // ========将坐标转换为LCD的坐标===========void LCDPosi
tion(uchar row,uchar column){ char position; if(row == 1) // row = 0x00在第一行显示,row = 0x01在第二行显示 position = 0x80 + column - 1; // column = 0~15在LCD上的可见域显示 else if(row == 2) position = 0xC0 + column - 1; LCDWriteCmd(position); delay(10);} // ==========在指定位置写数据函数=========== void writeChar(uchar row,uchar column, char c) // row指定行,column指定列位置,c为待写入的数据。{ LCDPosition(row,column); LCDWriteData(c); delay(200);} // ============写字符串函数================= void writeString(uchar row,uchar column1,uchar column2, char *s) // row表示行,column1表示第一行起始地址,column2表示下一行起始地址,均从1开始。{ char i; if((*s)==' ') return; for(i = 0; ; i++) { if((*(s+i)) == ' ') break; if(i <= 15) { writeChar(row,column1,*(s+i)); delay(2); column1++; } if(i > 15 && i< 32) { writeChar(row + 1,column2,*(s+i)); delay(2); column2++; } }} // 移动字符串函数void moveString(char *s){ uchar i; writeString(1,25,25,s); for(i = 16; i > 0; i--) { LCDWriteCmd(0x18); //屏幕移动 delay(900); }} // 液晶初始化函数void LCDInit(void){ LCDWriteCmd(0x38); //设置16*2显示,5*7点阵,8位数据接口 delay(30); LCDWriteCmd(0x01);//清屏 delay(30); LCDWriteCmd(0x06);//地址加一,整屏右移 delay(30); LCDWriteCmd(0x0f);//开显示,光标闪烁,显示光标} //外部中断初始化void InuptInit(void){ IT0=1; EX0=1; EA =1;} //===================================// 键盘部分//=================================== uchar code rowScan[]={0xFE,0xFD,0xFB,0xF7}; //行扫描码//uchar code columnScan[]={0xEF,0xDF,0xBF,0x7F};uchar code key12[]={0xEE,0xDE,0xBE,0xED,0xDD,0xBD,0xEB,0xDB,0xBB,0xE7,0xD7,0xB7}; // 1 2 3 4 5 6 7 8 9 * 0 #uchar code keyValue[]="123456789*0#"; #define KEY P1 //=====键盘扫描函数==========uchar keyScan(void) //返回值:有键按下时获得的键盘值(1234567890*#),无键按下时返回字符‘$’{ uchar k = 0,flag = 0; for(k = 0;k < 4; k++) { KEY = rowScan[k]; //逐行扫描 delay(10); //按键扫描 if(KEY != rowScan[k]) { uchar i=0; for(i = 0; i < 12; i++) { if(KEY == key12
) { //比较键码 flag=1; break; } } while(KEY != rowScan[k]); //等待按键释放 if(flag) return keyValue; } } return '$';} //===================================// 警铃部分//====================================sbit Beep = P3^1;uchar code scalesH[] = {0xf7,0xf8,0xfc,0xf9,0xfa,0xfa,0xfb}; //7个音符所对应的计数器初值uchar code scalesL[] = {0x20,0x88,0x44,0x59,0x13,0x67,0x04};uchar note; //初始化函数void AlARMInit(){ TMOD = 0x01; //T/C0工作在方式一 ET0 = 1; //开定时器中断 EA = 1; //开总中断 TR0 = 0; //关闭定时器0} //播放声音void play(uchar c){ uint i; switch( c ) { case 0 : note = 6; for(i=0;i<3;i++) { TR0 = 1; delay(300); TR0 = 0; delay(300); } break; case 1 : for(note=2;note<6;note++) { TR0 = 1; delay(300); TR0 = 0; delay(300); } break; } Beep = 1;} //定时器0的中断服务函数void time0() interrupt 1{ TH0 = scalesH[note]; TL0 = scalesL[note]; Beep = ~Beep;} //====================================// 主函数//==================================== void main(void){ uchar value,allValue[6],column =4; uint i=0; LCDInit(); InuptInit(); AlarmInit(); unlock = 0; writeString(1,1,1,"Enter Password:"); while(1) { value = keyScan(); if ((column < 10) && (value >= 48)) //48是“0”的ASCII值 { allValue[column - 4] = value; writeChar(2,column,value); column++; } if (value == '#') //按下取消键 { LCDWriteCmd(0x01); writeString(1,1,1,"Enter Password:"); column = 4; } if (value == '*') //按下确认键 { for(i=0;i<6;i++) { if (allValue != Keyword) //解锁失败 { unlock = 0; play(0); LCDWriteCmd(0x01); writeString(1,1,1,"Wrong Password"); writeString(2,1,1,"Unlock Failed!"); delay(600000); //3秒后清屏,等待下次输入??? LCDWriteCmd(0x01); writeString(1,1,1,"Enter Password:"); column = 4; break; } LCDWriteCmd(0x01); //解锁成功 unlock = 1; play(1); writeString(1,1,1,"Unlock Succeed!"); while(1); } } }} //====================================//外部中断0服务程序 (更改密码)//==================================== void exInt0(void) interrupt 0{ uchar j,key,index=4,allkey[6]; LCDWriteCmd(0x01); writeString(1,1,1,"Change Password? (Y/N)"); while(1) //等待用户确认 { key = keyScan(); if (key == '#') return; //有问题!!返回后怎么处理? if (key == '*') break; } LCDWriteCmd(0x01); writeString(1,1,1,"Old Password:"); while(1) { key = keyScan(); if ((index < 10) && (key >= 48)) //48是“0”的ASCII值 { allkey[index - 4] = key; writeChar(2,index,key); index++; } if (key == '#') //按下取消键 { LCDWriteCmd(0x01); writeString(1,1,1,"Old Password:"); index = 4; } if (key == '*') //按下确认键 { for(j=0;j<6;j++) { if (allkey[j] != Keyword[j]) //验证失败 { LCDWriteCmd(0x01); writeString(1,1,1,"Wrong Password"); writeString(2,1,1,"Match Failed!"); play(0); delay(600000); //3秒后清屏,等待下次输入 LCDWriteCmd(0x01); writeString(1,1,1,"Old Password:"); index = 4; break; } else{ LCDWriteCmd(0x01); //旧密码验证通过 writeString(1,1,1,"Enter New PW:"); index = 4; break; } } while(1) { key = keyScan(); if ((index < 10) && (key >= 48)) //输入新密码 { allkey[index - 4] = key; writeChar(2,index,key); index++; } if (key == '#') //按下取消键 { LCDWriteCmd(0x01); writeString(1,1,1,"Enter New PW:"); index = 4; } if( key =='*') { if (index != 10) { LCDWriteCmd(0x01); //若位数不足6位 writeString(1,1,1,"The Password must be 6 bits!"); delay(600000); //3秒后清屏,等待下次输入 LCDWriteCmd(0x01); writeString(1,1,1,"Enter New PW:"); index = 4; continue; } for(j=0;j<6;j++) Keyword[j] = allkey[j]; //存储新密码 break; } } break; } } writeString(1,1,1,"The Password has changed!"); play(1); return;}
一周热门 更多>