|
0.png (53.68 KB, 下載次數(shù): 62)
下載附件
2016-10-21 17:50 上傳
所有資料下載:
單片機密碼鎖.zip
(4.71 MB, 下載次數(shù): 150)
2016-10-21 17:52 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
1. 電子密碼鎖采用51單片機作為主控芯片,4x4矩陣鍵盤作為輸入設(shè)備,通過LCD1602顯示,使用24C02芯片用于存儲用戶密碼。
2.系統(tǒng)密碼長度設(shè)置為7位,具有輸入密碼,更改密碼,恢復(fù)初始密碼功能,密碼具有掉電存儲功能。密碼輸入錯誤達到三次,則鎖住鍵盤,并報警。
3.電子密碼鎖使用繼電器進行模擬開鎖過程。開鎖聲光提示。
0.png (672.26 KB, 下載次數(shù): 68)
下載附件
2016-10-21 17:47 上傳
0.png (699.45 KB, 下載次數(shù): 61)
下載附件
2016-10-21 17:48 上傳
密碼鎖的ppt教程:
0.png (735.54 KB, 下載次數(shù): 94)
下載附件
2016-10-21 17:48 上傳
原理圖:
0.png (148.34 KB, 下載次數(shù): 63)
下載附件
2016-10-21 17:53 上傳
密碼鎖單片機程序源碼:
- #include<reg52.h>
- #include<intrinS.h>
- #include<absacc.h>
- #include "keyc1.h" //見3.12
- #include "LCD1602.h"
- #include "24C02.h" //見2.2
- //見4.1
- #define unchar unsigned char
- #define uint unsigned int
-
- #define C02_write 0xa0
- #define C02_read 0xa1
- //----------------------------------------------------------------------------------------------
- #define enter 0x0c //確定
- #define backspace 0x0f //退格
- #define A 0x0a //輸入密碼
- #define B 0x0b //修改密碼
- //-----------------------------------------------------------------------------------------------
- unchar idata temp5_password[6]=
- {0,0,0,0,0,0}; //設(shè)置初始密碼為"123456"
- unchar num;//錯誤次數(shù)
- uint M1,M2=0,M3,M4,M5,M6;
- //-----------------------------------------------------------------------------------------------
- sbit SET=P3^2; //恢復(fù)出廠密碼設(shè)置鍵
- sbit Lock_BIT=P2^0; //鎖控制信號,輸出低電平
- sbit SPK=P2^2; //報警信號輸出端
- //-----------------------------------------------------------------------------------------------
- unchar int_count_1;
- unsigned int int_count_2;
- void SPKdelay(uint z) //蜂鳴器延時
- { uint i;
- for(i=z;i>0;i--);
- }
-
- //===============================================================================================
- void Longdelay(unsigned int i) //長時間延時函數(shù)
- {
- unsigned int j;
- for (;i>0;i--)
- for(j=10000;j>0;j--);
- }
-
- //================================================================================================
- bit compare_string(unchar *string1,unchar *string2)//密碼對比函數(shù)
- {
- unchar count;//設(shè)置循環(huán)對比計數(shù)器
- for(count=0;count<6;count++)//設(shè)置要對比的密碼為六位
- {
- if(string1[count]!=string2[count])
- return(0);//輸入密碼與原密碼不相同時返回出錯函數(shù)0
- }
- return(1);//輸入密碼正確返回正確參數(shù)1
- }
- //---------------------------------------------------------------------------------------------------
- unchar step_choose(void)//選擇輸入密碼或修改密碼處理函數(shù)
- {
- unsigned int i;
- unchar key;
- do{
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);//設(shè)置顯示“input password”的地址
- Show_Char("Input Password");//顯示"輸入密碼"
- GotoXY(0,1);//設(shè)置顯示plaese press A的首地址
- Show_Char("Please Press A");//顯示請輸入A
- for(i=0;i<30000;i++)//循環(huán)讀鍵盤
- {
- key=key1();//讀鍵盤
- if((key==A)||(key==B)) break;//如果是A或B就退出
- }
- if((key!=A)&&(key!=B))//如果不是A也不是B,就執(zhí)行下列程序
- {
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);// AC遞增,畫面不動
- GotoXY(0,0);//設(shè)置顯示“input password”的地址
- Show_Char("Change Password");//顯示修改密碼
- GotoXY(0,1);//設(shè)置顯示“please press B”的首地址
- Show_Char("Please Press B");//顯示請輸入B
- for(i=0;i<30000;i++)//循環(huán)讀鍵盤
- {
- key=key1();//讀鍵盤
- if((key==A)||(key==B)) break;//如果是A或B就退出
- }
- }
- }
- while((key!=A)&&(key!=B));//如果不是A也不是B,就循環(huán)讀鍵盤
- return (key);//是A或B就帶參數(shù)返回
- }
- //=================================================================================================
- bit input_password(unchar *password)//輸入密碼函數(shù)
- {
- unchar count,count1;//設(shè)置輸入密碼位數(shù)計數(shù)器
- unchar key;//設(shè)置鍵值變量
- GotoXY(0,1);
- for(count=0;count<7;count++)
- {
- Longdelay(3);//延時
- if(count<6);//輸入密碼小于六位嗎?
- {
- do{key=key1();}//是的,讀鍵盤
- while(key==0xff);//如果鍵盤值為0xff,繼續(xù)讀鍵盤
- if(key==0x0d) break; //退出鍵,回到初始界面
- if((key!=backspace)&&(key!=enter)&&(key>=0)&&(key<=9))//不是退格或確認(rèn)鍵嗎?
- {
- Show_1_Char('*');//不是,是數(shù)字鍵就顯示'*'號
- password[count]=key;//鍵值送入輸入密碼數(shù)組對應(yīng)位
- }
- if(key==backspace)//是退格鍵嗎?
- {
- if(count>0)//輸入位數(shù)大于0嗎?
- {
- GotoXY(--count,1);//是的,送刪除位的顯示地址
- Show_1_Char(' '); //刪除位顯示空白
- password[count]=' '; //輸入密碼數(shù)組對應(yīng)位置空白
- GotoXY(count,1); //送閃爍位
- count--;//密碼位計數(shù)器減1
-
- }
- }
- if(key==enter)//是確認(rèn)鍵嗎
- {
- GotoXY(0,0);//是的,改變顯示首地址
- return(0);//返回出錯參數(shù)0
- }
- }
- if(count==6)//密碼位計數(shù)器等于6嗎
- {
- do{key=key1();}//等于6,讀鍵盤
- while((key!=backspace)&&(key!=enter));//不是退格鍵或確認(rèn)鍵,繼續(xù)讀鍵盤
- if(key==backspace)//是退格鍵嗎
- {
- GotoXY(--count,1);//是的,送刪除位的顯示地址
- Show_1_Char(' ');//刪除位顯示空白
- GotoXY(count,1);//送閃爍位
- count--;//密碼位計數(shù)器減1
- }
- if(key==enter)//是確認(rèn)鍵嗎
- {return(1);}//是的,輸入密碼完成,返回正確參數(shù)1
- }
- }
- }
- //===================================================================================================
- void password()//密碼處理函數(shù)
- {
- uchar key,key2;
- uint kscount=0;
- unchar idata temp1_password[6]={' ',' ',' ',' ',' ',' '};//設(shè)置4組6位密碼變量
- unchar idata temp3_password[6]={' ',' ',' ',' ',' ',' '}; //初始化為空白
- unchar idata temp2_password[6]={' ',' ',' ',' ',' ',' '};
- unchar idata temp4_password[6]={' ',' ',' ',' ',' ',' '};
- key=step_choose( );//讀選擇狀態(tài)
- if(key==A)//是選擇輸入密碼嗎?
- {
- Read_Flash(temp1_password,0x00,6);//是的從24c02中讀取密碼送入第一組密碼變量中
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);//設(shè)置顯示“input password”的首地址
- Show_Char("Input Password");//顯示“輸入密碼”
- if(input_password(temp2_password))//完成輸入密碼了嗎?
- {
- if(compare_string(temp1_password,temp2_password))//完成,比較密碼正確嗎
- { num=0;
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);//正確,設(shè)置顯示“password correct”的首地址
- Show_Char("Password Correct");//顯示"密碼正確"
- GotoXY(0,1);//正確,設(shè)置顯示“password correct”的首地址
- Show_Char("Open Lock");//顯示"密碼正確" Lock_BIT=0;//密碼正確,開鎖
- Lock_BIT=0;//關(guān)開鎖信號
- while(1)
- {
- kscount++;
- if(kscount==1000)
- break;
- SPK=0;
- SPKdelay(10);//發(fā)開鎖提示聲
- SPK=1;
- SPKdelay(40);
- }
-
- while(1)
- { key2=key1();
- if(key2==0x0e)
- {
- Lock_BIT=1;//關(guān)開鎖信號
- break;
- }
- }
- SPK=0;
- //關(guān)開鎖提示音
- TR0=0;//關(guān)定時器0
- return;//返回
- }
- else
- {
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);//錯誤,設(shè)置顯示“wrong password”的首地址
- Show_Char("Wrong Password");//顯示“密碼錯誤”
- Longdelay(25);
- num++;
- if(num==3)
- {
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);
- Show_Char("Please Wait For");
- GotoXY(0,1);
- Show_Char(" 3 Minute");
- while(1)
- {
- SPK=0;
- SPKdelay(100);
- SPK=1;
- SPKdelay(100);
- M1++;
- if(M1==24000)
- {
- break;
- }
- }
- Longdelay(1400);
- }
- Longdelay(10);//延時
- return;//返回
- }
- }
- else
- {
- GotoXY(0,1);//沒有完成密碼輸入,設(shè)置顯示“error!”的首地址
- Show_Char("Error!");//顯示“出錯”
- Longdelay(15);//延時
- }
- }
- else//是,修改密碼
- {
- Read_Flash(temp1_password,0x00,6);//從24c02中讀取密碼送入第一組密碼變量中
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);//設(shè)置顯示“input old password”的首地址
- Show_Char("Input Old Password");//顯示輸入舊密碼
- if(input_password(temp2_password))//完成輸入密碼了嗎(輸入的舊密碼放在數(shù)組2中)
- {
- if(compare_string(temp1_password,temp2_password))//完成,比較密碼正確嗎?
- {
- GotoXY(0,1);//正確,設(shè)置顯示“password correct!”的首地址
- Show_Char("Password Correct!");//顯示“密碼正確”
- Longdelay(6);//延時
- WR_CMD(0x01);
- WR_CMD(0x06);
- GotoXY(0,0);
- Show_Char("Input New Password!");
- if(input_password(temp3_password))//完成第一次新密碼輸入嗎?(輸入密碼放在數(shù)組三中)
- {
- WR_CMD(0x01);
- WR_CMD(0x06);
- GotoXY(0,0);
- Show_Char("Input Again");//顯示再次輸入密碼
- if(input_password(temp4_password))//完成第二次新密碼輸入嗎?(輸入的密碼放在密碼組4中)
- {
- if(compare_string(temp3_password,temp4_password))//比較兩次輸入密碼屬否相同
- {
- Write_Flash(temp3_password,0x00,6);//相同,將新密碼存入24c02中
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);
- Show_Char("Password has");
- GotoXY(0,1);
- Show_Char("been changed");
- Longdelay(10);
- return;
- }
- else
- {
- WR_CMD(0x01);//兩次輸入不同清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);
- Show_Char("Twice input"); //顯示”兩次輸入“
- GotoXY(0,1);
- Show_Char("is different"); //顯示是“不同的”
- Longdelay(10);
- return;
- }
- }
- else
- {
- GotoXY(0,1);//沒有正確完成第二次新密碼的輸入,設(shè)置顯示“error”的首地址
- Show_Char("Error!");//顯示錯誤
- Longdelay(15);
- return;
- }
- }
- else
- {
- GotoXY(0,1);//沒有正確完成第一次新密碼輸入,設(shè)置顯示“error”的首地址
- Show_Char("Error!");
- Longdelay(15);
- return;
- }
- }
- else
- {
- WR_CMD(0x01);//兩次輸入不同清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(3,0);//舊密碼輸入錯誤,設(shè)置顯示“wrong password”的首地址
- Show_Char("Sorry");
- GotoXY(0,1);
- Show_Char("Wrong password");
- num++;
- if(num==3)
- {
- WR_CMD(0x01);//清屏
- WR_CMD(0x06);//AC遞增,畫面不動
- GotoXY(0,0);
- Show_Char("Please Wait For");
- GotoXY(0,1);
- Show_Char(" 3 Minute");
- while(1)
- {
- SPK=0;
- SPKdelay(100);
- SPK=1;
- SPKdelay(100);
- M1++;
- if(M1==24000)
- {
- break;
- }
- }
- Longdelay(1400);
- }
- Longdelay(25);
- return;
- }
- }
- else
- {
- GotoXY(0,1);//沒有正確完成舊密碼的輸入,設(shè)置顯示“error!”的首地址
- Show_Char("Error!");
- Longdelay(15);
- return;
- }
- }
- }
- //============================================================================================================
- void int_service(void) interrupt 0 //外部中斷0處理函數(shù)
- {
- EA=1;//關(guān)CPU中斷
- EX0=1;//關(guān)外部中斷0
- for(int_count_1=4;int_count_1>0;int_count_1--)//設(shè)置延時時間,10秒
- {
- for(int_count_2=0xffff;int_count_2>0;int_count_2--)
- {
- if(SET==1)//恢復(fù)出廠密碼設(shè)置鍵松開否?
- {
- return;
- }
- }
- }
- WR_CMD(0x01);
- WR_CMD(0x06);
- Show_Char("Reset Cuccess!");//顯示“恢復(fù)出廠密碼”
- Longdelay(50);
- Write_Flash(temp5_password,0x00,6); //初始密碼0000000密碼送入24c02中保存
-
- /* EA=1;//關(guān)CPU中斷
- EX0=1; */
- }
- //==========================================================================================
- void t0(void) interrupt 1 using 0//定時、計數(shù)器0中斷處理函數(shù)
- {
- TH0=(65536-1000)/256;//重新將定時1000微秒初值的高八位送入TH0
- TL0=(65536-1000)%256;// 重新將定時1000微秒初值的低八位送入TL0
- SPK=~SPK;//信號輸出位取反,即1000微秒反轉(zhuǎn)一次,產(chǎn)生周期為2ms的方波
- }
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- main()
- { unchar key;
- Initialize(); //1602初始化
- IT0=1;//設(shè)INT0為跳變沿沿觸發(fā)方式
- EX0=1;//開外部中斷0
- TMOD=0x01;
- TH0=(65536-1000)/256;//重新將定時1000微秒初值的高八位送入TH0
- TL0=(65536-1000)%256;// 重新將定時1000微秒初值的低八位送入TL0
- TR0=0;
- ET0=1;
- EA=1;
- while(1)
- {
- password();//運行密碼處理函數(shù)
- }
- }
- //=================================終于完成了==================================================
復(fù)制代碼
|
|