51单片机:设计电子密码锁
文章目錄
- 一、課程設計內容
- 功能闡述
- 二、開發板原理圖與設計流程圖
- 三、設計思路和方法
- 1、EEPROM初始化
- 2、LCD1602初始化
- 3、矩陣按鍵掃描
- 4、輸入密碼
- 5、密碼比對
- 四、源代碼附錄
- 五、經驗總結與體會
- 1、遇到問題及解決方法
一、課程設計內容
選用單片機開發板STC89C52作為本設計的核心元件,利用編程設計、I/O端口及其板上的各種硬件電路,設計電子密碼鎖功能
功能闡述
選用單片機開發板STC89C52作為本設計的核心元件,利用編程設計、I/O端口及其板上的各種硬件電路,設計密碼鎖功能,以自創的代碼將功能實現,并形成項目報告。利用單片機的矩陣鍵盤用于密碼的輸入和一些功能的控制,利用EEPROM用于密碼的存儲,外接LCD1602液晶顯示器用于顯示作用。
二、開發板原理圖與設計流程圖
三、設計思路和方法
設計思路:初始化從EEPROM中讀取密碼,單片機通電即顯示首頁,按任意鍵進入功能選擇界面,可通過按鍵移動光標選擇直接輸入密碼登錄,或者進行修改密碼操作。①直接輸入密碼登錄:通過矩陣按鍵輸入6位數字密碼,并存入數組,然后通過該數組與EEPROM讀取的密碼進行比對,從而判斷密碼是否正確,如果密碼正確則顯示登錄成功并觸發流水燈反饋結果,如果密碼錯誤則顯示輸入錯誤并觸發蜂鳴器警告,當輸入密碼次數超過3次則直接返回首頁。②進行修改密碼操作:提示輸入原密碼,鍵入6位密碼并判斷,當輸入密碼正確時提示輸入新密碼,鍵入6位新密碼后存入EEPROM,重啟或重新登錄時從EEPROM讀取密碼,此時使用為新密碼。
1、EEPROM初始化
將51單片機的頭文件和i2c.h的頭文件包含進來,對24C02芯片進行讀寫操作,調用At24c02Write函數將變量、數據寫入到對應的地址內,調用 At24c02Read 函數進行讀取操作,將從對應地址內讀取的值存儲在變量中。
2、LCD1602初始化
LCD1602驅動的底層協議中幾個常用的函數:
① LcdWriteCom():寫命令函數,通過此函數向LCD1602寫命令。比如:清屏LcdWriteCom(0x01); 設置數據指針起LcdWriteCom(0x80)。
② LcdWriteData():顯示函數,在寫數據之前需要通過LcdWriteCom()函數告訴要寫數據的地址,LCD1602的第一行的16個顯示位地址是0x80到0x8f;第二行的地址是0xc0到0xcf。比如在1602的第一行第一位顯示數字8:LcdWriteCom(0x80)或LcdWriteData(‘8’)。
③ showString (unsigned char Coordinate,char *ptr):ShowString (首地址,字符串)函數在需要顯示字符串時使用。比如在第二行第3位開始顯示hello:ShowString (0x13,”hello”)。其中首地址的高四位為0則表示在第一排顯示,為“1”則在第二排顯示。低四位為0則在第0位顯示。
3、矩陣按鍵掃描
矩陣按鍵P1口的低四位接的4*4矩陣鍵盤的行,高四位接的矩陣鍵盤的列。檢測矩陣鍵盤是否有按鍵按下時:先將P1端口的低四位置1,高四位清零,檢測P1端口的狀態,如果高四位不為零,則表示有按鍵按下,并且可以知道是x0-x3哪一列有按鍵按下,比如P1=0x1f;則第一列有按鍵按下。此時我們在將P1口第四位清零,高四位置1;檢測P1的狀態,就知道y0-y3哪一行有按鍵按下。結合xy就可以知道具體是哪個按鍵按下。
4、輸入密碼
檢測按鍵’0’~'9’的按下,將鍵入數字信息保存至輸入密碼數組,+‘0’因為需要存入的是ASCII碼,顯示密碼在Lcd顯示屏第2行,可修改傳入值m改變密碼顯示形式,m=0密碼以’*'顯示,m=1密碼直接顯示,按下return鍵時返回一步,按下OK鍵時結束輸入。
5、密碼比對
先判斷密碼長度,如果不為6位直接跳轉密碼錯誤反饋,滿足密碼位數,再逐位與EEPROM內存的密碼進行比對,逐一對應則跳轉密碼成功反饋,否則跳轉密碼錯誤反饋。
四、源代碼附錄
/**************************************************************************************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);} }五、經驗總結與體會
1、遇到問題及解決方法
①實驗調試過程中,由于密碼固定為6位數的數字密碼,矩陣鍵盤鍵入數值為int型,而密碼數組為字符型,錄入密碼再從EEPROM讀取時會發現出現非預期字符或無法顯示的空白字符,解決方法是在錄入和讀取密碼時統一為字符型例如:
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’;
②實驗調試過程中,由于數據的錄入和顯示會出現各種狀況,在實現修改密碼功能的調試過程中,由于統一使用字符型,需要與int型進行轉換,可能會出現密碼錄入EEPROM后出現矩陣鍵盤無法輸入的字符或其他非法字符,此時由于無法輸入正確密碼而需要反復調試將會增加很多麻煩,此時解決方法可以使用語句:
At24c02Write(0, 0);
清除內存,初始化,將密碼重置到888888,用于調試時可對EEPROM存入密碼初始化。調試成功后可將此語句注釋,恢復單片機重啟后依然可以讀取上次錄入的新密碼的功能。
總結
以上是生活随笔為你收集整理的51单片机:设计电子密码锁的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鸿鹄系统和鸿蒙系统区别,荣耀智慧屏正式发
- 下一篇: [附源码]计算机毕业设计Python儿童