51单片机:设计电子密码锁
文章目錄
- 一、課程設(shè)計(jì)內(nèi)容
- 功能闡述
- 二、開發(fā)板原理圖與設(shè)計(jì)流程圖
- 三、設(shè)計(jì)思路和方法
- 1、EEPROM初始化
- 2、LCD1602初始化
- 3、矩陣按鍵掃描
- 4、輸入密碼
- 5、密碼比對(duì)
- 四、源代碼附錄
- 五、經(jīng)驗(yàn)總結(jié)與體會(huì)
- 1、遇到問(wèn)題及解決方法
一、課程設(shè)計(jì)內(nèi)容
選用單片機(jī)開發(fā)板STC89C52作為本設(shè)計(jì)的核心元件,利用編程設(shè)計(jì)、I/O端口及其板上的各種硬件電路,設(shè)計(jì)電子密碼鎖功能
功能闡述
選用單片機(jī)開發(fā)板STC89C52作為本設(shè)計(jì)的核心元件,利用編程設(shè)計(jì)、I/O端口及其板上的各種硬件電路,設(shè)計(jì)密碼鎖功能,以自創(chuàng)的代碼將功能實(shí)現(xiàn),并形成項(xiàng)目報(bào)告。利用單片機(jī)的矩陣鍵盤用于密碼的輸入和一些功能的控制,利用EEPROM用于密碼的存儲(chǔ),外接LCD1602液晶顯示器用于顯示作用。
二、開發(fā)板原理圖與設(shè)計(jì)流程圖
三、設(shè)計(jì)思路和方法
設(shè)計(jì)思路:初始化從EEPROM中讀取密碼,單片機(jī)通電即顯示首頁(yè),按任意鍵進(jìn)入功能選擇界面,可通過(guò)按鍵移動(dòng)光標(biāo)選擇直接輸入密碼登錄,或者進(jìn)行修改密碼操作。①直接輸入密碼登錄:通過(guò)矩陣按鍵輸入6位數(shù)字密碼,并存入數(shù)組,然后通過(guò)該數(shù)組與EEPROM讀取的密碼進(jìn)行比對(duì),從而判斷密碼是否正確,如果密碼正確則顯示登錄成功并觸發(fā)流水燈反饋結(jié)果,如果密碼錯(cuò)誤則顯示輸入錯(cuò)誤并觸發(fā)蜂鳴器警告,當(dāng)輸入密碼次數(shù)超過(guò)3次則直接返回首頁(yè)。②進(jìn)行修改密碼操作:提示輸入原密碼,鍵入6位密碼并判斷,當(dāng)輸入密碼正確時(shí)提示輸入新密碼,鍵入6位新密碼后存入EEPROM,重啟或重新登錄時(shí)從EEPROM讀取密碼,此時(shí)使用為新密碼。
1、EEPROM初始化
將51單片機(jī)的頭文件和i2c.h的頭文件包含進(jìn)來(lái),對(duì)24C02芯片進(jìn)行讀寫操作,調(diào)用At24c02Write函數(shù)將變量、數(shù)據(jù)寫入到對(duì)應(yīng)的地址內(nèi),調(diào)用 At24c02Read 函數(shù)進(jìn)行讀取操作,將從對(duì)應(yīng)地址內(nèi)讀取的值存儲(chǔ)在變量中。
2、LCD1602初始化
LCD1602驅(qū)動(dòng)的底層協(xié)議中幾個(gè)常用的函數(shù):
① LcdWriteCom():寫命令函數(shù),通過(guò)此函數(shù)向LCD1602寫命令。比如:清屏LcdWriteCom(0x01); 設(shè)置數(shù)據(jù)指針起LcdWriteCom(0x80)。
② LcdWriteData():顯示函數(shù),在寫數(shù)據(jù)之前需要通過(guò)LcdWriteCom()函數(shù)告訴要寫數(shù)據(jù)的地址,LCD1602的第一行的16個(gè)顯示位地址是0x80到0x8f;第二行的地址是0xc0到0xcf。比如在1602的第一行第一位顯示數(shù)字8:LcdWriteCom(0x80)或LcdWriteData(‘8’)。
③ showString (unsigned char Coordinate,char *ptr):ShowString (首地址,字符串)函數(shù)在需要顯示字符串時(shí)使用。比如在第二行第3位開始顯示hello:ShowString (0x13,”hello”)。其中首地址的高四位為0則表示在第一排顯示,為“1”則在第二排顯示。低四位為0則在第0位顯示。
3、矩陣按鍵掃描
矩陣按鍵P1口的低四位接的4*4矩陣鍵盤的行,高四位接的矩陣鍵盤的列。檢測(cè)矩陣鍵盤是否有按鍵按下時(shí):先將P1端口的低四位置1,高四位清零,檢測(cè)P1端口的狀態(tài),如果高四位不為零,則表示有按鍵按下,并且可以知道是x0-x3哪一列有按鍵按下,比如P1=0x1f;則第一列有按鍵按下。此時(shí)我們?cè)趯1口第四位清零,高四位置1;檢測(cè)P1的狀態(tài),就知道y0-y3哪一行有按鍵按下。結(jié)合xy就可以知道具體是哪個(gè)按鍵按下。
4、輸入密碼
檢測(cè)按鍵’0’~'9’的按下,將鍵入數(shù)字信息保存至輸入密碼數(shù)組,+‘0’因?yàn)樾枰嫒氲氖茿SCII碼,顯示密碼在Lcd顯示屏第2行,可修改傳入值m改變密碼顯示形式,m=0密碼以’*'顯示,m=1密碼直接顯示,按下return鍵時(shí)返回一步,按下OK鍵時(shí)結(jié)束輸入。
5、密碼比對(duì)
先判斷密碼長(zhǎng)度,如果不為6位直接跳轉(zhuǎn)密碼錯(cuò)誤反饋,滿足密碼位數(shù),再逐位與EEPROM內(nèi)存的密碼進(jìn)行比對(duì),逐一對(duì)應(yīng)則跳轉(zhuǎn)密碼成功反饋,否則跳轉(zhuǎn)密碼錯(cuò)誤反饋。
四、源代碼附錄
/**************************************************************************************Course application design : Electronic code lock Function design: download program and input correct password, receive double lights rolling; input wrong password, receive buzzer alarm Hardware connection:P1 -- > keyboard matrixP20 - > EEPROM module SDAP21 - > EEPROM module SCLMatrix keyboard position corresponding information:S1 S2 S3 S4 0 1 2 3S5 S6 S7 S8 4 5 6 7--->S9 S10 S11 S12 8 9 return OKS13 S14 S15 S16 × × × × ***************************************************************************************/ #include "reg52.h" //This file defines some special function registers of MCU #include "lcd.h" #include "key.h" #include "i2c.h" #include<intrins.h> //The header file required by the left and right shift function#define u16 unsigned int //Declarative definition of data types #define u8 unsigned charsbit beep=P2^5; //define beep# define LCD_LINE1 0x00 //Define the first line of the LCD display # define LCD_LINE2 0xc0 //Define the second line of the LCD display u8 pw_num,Error_Num,PassWord_Length=6; //The password length is 6 u8 PASSWORD[]={'8','8','8','8','8','8'}; //The initial password is 888888 u8 INPUT_PW_Tab[10] = {'-', '-', '-', '-', '-', '-'};//Defines an array to hold passwords u8 key_num,Step,Step5,Load_first_flag;bit result_flag = 0,List1=0,Input_suc_flag;void Step_0(); //Step 0: home page display void Step_1(); //Step 1: display function: input password or modify password void Step_2(); //Step 2: Select function: input password or modify password void Step_3(); //Step 3: display prompt: input password void Step_4(); //Step 4: input the password void Step_5(); //Step 5: change the password void Step5_0(); //Modify password function: step 0: display prompt: input the original password void Step5_1(); //Password modification function: Step 1: enter the password void Step5_2(); //Password modification function: Step 2: password comparison void Step5_3(); //Password modification function: Step 3: processing after password comparison void Step5_4(); //Password modification function: Step 4: enter a new password void Step5_5(); //Password modification function: Step 5: read the new password and save it in EEPROM void Step_6(); //Step 6: the password comparison result is correct or wrong, feedback the corresponding result void CipherComparison(); //Function of password comparison void input_password(bit m); //Function to enter password void init_Password(); //Initialization password: read password from EEPROM void light_run(); //Water lamp: feedback function after inputting correct password void beep_run(); //Beep: feedback function after inputting wrong password/*******************************************************************************/ void main() { u8 data1,a;beep=1;LcdInit();// At24c02Write(0, 0); /*Clear the memory, initialize and reset the password to 888888. This statement is used to write illegal characters to EEPROM during debugging. When the password cannot be modified again, this statement can be used to initialize the password.*/delay(1000);Step=0;Step5=0;Error_Num=0x00;init_Password();while(1){ key_num=KeyDown(); //Read input valueswitch(Step){case 0:{Step_0();break;}case 1:{Step_1();break;}case 2:{Step_2();break;}case 3:{Step_3();break;} case 4:{Step_4();break;} case 5:{Step_5();break;} case 6:{Step_6();break;} }} } /*****************************************************************************/ //Step 0: home page display void Step_0() {char dis[16];int i;//For debugging convenience, the current password is displayed on the home page. dis[0] = Load_first_flag + '0';dis[1] = ':';for(i = 0; i < 6; i++)dis[2 + i] = PASSWORD[i] ;dis[8]=0;LcdInit();if(result_flag == 0) //First time login {ShowString(LCD_LINE1, " Need to Log in "); //The first line shows " Need to Log in " ShowString(LCD_LINE2, dis); //The second line shows the current password }else{ShowString(LCD_LINE1, " Welcome! "); //The first line shows " Welcome! " ShowString(LCD_LINE2, dis); //The second line shows the current password}while(KeyDown()==0xff)Step=1; //Press the detection button to enter the next step } /*****************************************************************************/ //Step 1: display function: input password or modify password void Step_1() {LcdWriteCom(0x01); //Clear screenShowString(0x00,"Input Password"); //The first line of LCD1602 displays "Input Password"ShowString(0x0f,"<"); //Display indicator cursor "<" ShowString(0x10,"Change Password"); //The second line of LCD1602 displays change passwordShowString(0x1f," "); Step=2; //Go to step 2 } /*****************************************************************************/ //Step 2: Select function: input password or modify password void Step_2() {if(key_num!=0x0b) {if((key_num==0x01) ||( key_num==0x09)) //When 1 key or 9 key is pressed: switch cursor position - > Select function {List1=~List1; //Change Passwordif(List1==0){ ShowString(0x0f,"<"); // Input Password <ShowString(0x1f," "); // Change Password }else{ShowString(0x0f," "); // Input Password ShowString(0x1f,"<"); // Change Password <}}}else //When the OK key is pressed{if(List1==0){Step=3;} //When input password is selected, go to step 3 else {Step=5;List1=0;} //When change password is selected, go to step 5 } } /*****************************************************************************/ //Step 3: display prompt: input password void Step_3() {Step=4;pw_num=0;LcdInit();ShowString(0x00,"Pass Word: "); } /*****************************************************************************/ //Step 4: input the password void Step_4() {input_password(1); //Input the password and display itif(Input_suc_flag==1){Step=6;} //Enter the password and go to the next stepInput_suc_flag=0; //Clear the password input complete flag } /*****************************************************************************/ //Step 5: change the password void Step_5() {switch(Step5){case 0: {Step5_0();} break;case 1: {Step5_1();} break;case 2: {Step5_2();} break;case 3: {Step5_3();} break;case 4: {Step5_4();} break;case 5: {Step5_5();} break;} } /*****************************************************************************//*****************************************************************************/ //Step 6: the password comparison result is correct or wrong, feedback the corresponding result void Step_6() {CipherComparison(); //Password comparisonif(result_flag==1) //If the password is correct{LcdInit();ShowString(0x00," SUCCESS!"); //Display "success!" and trigger the water lamplight_run(); }else //If the password is error{LcdInit();ShowString(0x00,"Error!"); //Display "error!" and trigger buzzer beep_run();}Step=0; }/****************************************************************************/ //Modify password function: step 0: display prompt: input the original password void Step5_0() {LcdWriteCom(0x01); //Clear screenShowString (0x00,"Input PassWord:");//The first line of LCD1602 displays "Input Password"Step5=1; //Go to the next step pw_num=0; } //Password modification function: Step 1: input the password void Step5_1() {input_password(1); //Input the password and display itif(Input_suc_flag==1) //When the password is entered{Step5=2; //Go to the next step Input_suc_flag=0; //Clear password input complete flag} } //Password modification function: Step 2: password comparison void Step5_2() {CipherComparison(); //Password comparisonStep5=3; //Go to the next step } //Password modification function: Step 3: processing after password comparison void Step5_3() {if(result_flag==0) //Input password error{if(Error_Num<3) //The number of input errors is less than 3{Error_Num++;LcdInit();ShowString (0x00,"Error"); //If the password is wrong, diaplay "Error"delay(20000);Step5=0;}else //The number of input errors is more than 3{Error_Num=0;Step=0;} }else //Input password correctly{LcdInit();ShowString (0x00,"New PassWord:");//If the password is correct, diaplay "New PassWord:" pw_num=0;Step5=4; //Go to the next step} } //Password modification function: Step 4: input the new password void Step5_4() {input_password(1); //Input the password and display itif(Input_suc_flag==1) //When the password is entered{ Step5=5; //Go to the next step Input_suc_flag=0;LcdWriteCom(0x01); //clear screenShowString (0x00," OK!");} } //Password modification function: Step 5: read the new password and save it in EEPROM void Step5_5() {unsigned char j;Load_first_flag = 1;At24c02Write(0,Load_first_flag); delay(100); for(j=0;j<PassWord_Length;j++) {PASSWORD[j]=INPUT_PW_Tab[j]; //Read passwordAt24c02Write(j+1,INPUT_PW_Tab[j] - '0');//Save password to EEPROMdelay(100);}Step5=0;Step=0; } /****************************************************************************/ //Initialization password: read password from EEPROM void init_Password() {unsigned char j;Load_first_flag=At24c02Read(0);if(Load_first_flag!=0) {for(j=0;j<PassWord_Length;j++) //Read password{PASSWORD[j]= At24c02Read(j + 1)+'0';}} }//Function to enter password void input_password(bit m) {unsigned char j;if(key_num!=0x0b) //When the OK key is not pressed{if(key_num<0x0a) //Keys 1-9 are pressed{INPUT_PW_Tab[pw_num]=key_num + '0'; //Save to the input password array. Using +'0' because it needs to store ASCII code pw_num=pw_num+1; //Password length + 1LcdWriteCom(LCD_LINE2); //Display the password on line 2 of LCD display for(j=0;j<pw_num;j++){if(m==0) {LcdWriteData('*'); } //The input value m can be modified to change the password display. M = 0, the password is displayed as' * '. M = 1 password direct display else {LcdWriteData(INPUT_PW_Tab[j]);} //display the password}}if(key_num==0x0a) //When the return key is pressed{if(pw_num!=0) {pw_num=pw_num-1;}else {Step=0;}LcdWriteCom(LCD_LINE2); for(j=0;j<pw_num;j++){if(m==0) {LcdWriteData('*'); } //Using '*' to hide the passwordelse {LcdWriteData(INPUT_PW_Tab[j]);}//Display password directly }LcdWriteData(' '); }} else //When the OK key is pressed{if(pw_num==0) //When the password number is wrong, it will be judged as wrong password directly {Step=0;LcdWriteCom(0x01);ShowString (0x00,"Error!");delay(10000);}else{ Input_suc_flag=1; } } } /***************************************************************/ //Function of password comparison void CipherComparison() { u8 i;if(PassWord_Length==pw_num) //Password length comparison{for(i=0;i<PassWord_Length;i++) //Password comparison{if(PASSWORD[i]!=INPUT_PW_Tab[i]){result_flag=0;return; //Password error}}result_flag=1;}elseresult_flag=0; }//Water lamp: feedback function after inputting correct password void light_run() {u8 i;for(i=0;i<8;i++){P2=~(0x03<<i); //Move the i bit to the right, and then assign the result to the P2 port. The first light of ~0x03 is D1 and D2 delay(100000); //delay } }//Beep: feedback function after inputting wrong password void beep_run() {u8 i;for(i=0;i<100;i++){beep=~beep;delay(1000);} }五、經(jīng)驗(yàn)總結(jié)與體會(huì)
1、遇到問(wèn)題及解決方法
①實(shí)驗(yàn)調(diào)試過(guò)程中,由于密碼固定為6位數(shù)的數(shù)字密碼,矩陣鍵盤鍵入數(shù)值為int型,而密碼數(shù)組為字符型,錄入密碼再?gòu)腅EPROM讀取時(shí)會(huì)發(fā)現(xiàn)出現(xiàn)非預(yù)期字符或無(wú)法顯示的空白字符,解決方法是在錄入和讀取密碼時(shí)統(tǒng)一為字符型例如:
char PASSWORD[]={‘8’,‘8’,‘8’,‘8’,‘8’,‘8’}; //The initial password is 888888
char INPUT_PW_Tab[10] = {’-’, ‘-’, ‘-’, ‘-’, ‘-’, ‘-’}; //Defines an array to hold passwords
錄入EEPROM:
At24c02Write(j+1,INPUT_PW_Tab[j] - ‘0’); //Save password to EEPROM
從EEPROM讀取:
PASSWORD[j]= At24c02Read(j + 1)+‘0’;
②實(shí)驗(yàn)調(diào)試過(guò)程中,由于數(shù)據(jù)的錄入和顯示會(huì)出現(xiàn)各種狀況,在實(shí)現(xiàn)修改密碼功能的調(diào)試過(guò)程中,由于統(tǒng)一使用字符型,需要與int型進(jìn)行轉(zhuǎn)換,可能會(huì)出現(xiàn)密碼錄入EEPROM后出現(xiàn)矩陣鍵盤無(wú)法輸入的字符或其他非法字符,此時(shí)由于無(wú)法輸入正確密碼而需要反復(fù)調(diào)試將會(huì)增加很多麻煩,此時(shí)解決方法可以使用語(yǔ)句:
At24c02Write(0, 0);
清除內(nèi)存,初始化,將密碼重置到888888,用于調(diào)試時(shí)可對(duì)EEPROM存入密碼初始化。調(diào)試成功后可將此語(yǔ)句注釋,恢復(fù)單片機(jī)重啟后依然可以讀取上次錄入的新密碼的功能。
總結(jié)
以上是生活随笔為你收集整理的51单片机:设计电子密码锁的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 鸿鹄系统和鸿蒙系统区别,荣耀智慧屏正式发
- 下一篇: Google云游戏平台Stadia好不好