1.系統功能概述
本次分享的是一款基於51單片機的指紋識別電子密碼鎖系統,該系統以STC89C52單片機作爲模塊核心,通過串口通信控制指紋模塊AS608實現錄取指紋並存儲指紋數據,並通過LCD12864液晶顯示屏比對流程及效果,輔以直流繼電器以發光二極管模擬開鎖動作。
2.硬件設計
(1)整體硬件框圖
硬件電路主要包括51單片機最小系統、指紋模塊AS608電路、4*4矩陣鍵盤電路、LCD12864顯示屏電路及系統電源。
(2)AS608指紋模塊電路
AS608指紋模塊採用串口或者USB進行數據通訊。本例程採用串口通訊的方式實現對指紋模塊的控制。單片機可通過廠家提供的控制指令集對指紋模塊做相應的操作,詳細可以參考AS608指紋模塊用戶手冊。
(3)其他電路
由於其他硬件電路都是比較常用,就不一一介紹了。
3.軟件設計
軟件流程:首先對各個模塊進行初始化,檢測有無按鍵按下,如果按下,則判斷是哪一個按鍵,並作出判斷,並調用相應的按鍵處理函數。
主程序主要由液晶顯示、串口通訊程序和按鍵程序組成。
(1)主函數
void main()
{
//PSB=0;
UART_Init(); //串口初始化
x24c02_init(); //24C02初始化
LcmInit(); //LCD12864初始化
LcmClearTXT(); //LCD12864清屏
while(1)
{
fan();
delay(100);
(2)指紋模塊處理程序(部分代碼)
因爲源碼比較大,我就不想全都貼出來了,如果需要詳細瞭解源碼可下載源碼查看(下載鏈接在文章最後)
#define buffer1ID 0x01
#define buffer2ID 0x02
#define queren 0x88 //確認
#define tuichu 0x85 //退出
#define shanchu 0x82 //刪除
#define User 1 //用戶
void fan(); //主界面刷指紋管理員函數
sbit B0=B^0;
sbit B7=B^7;
sbit k2=P3^7; //指紋模塊
sbit jidianqi=P3^6; //繼電器
extern unsigned char times[];
uchar Member=0,sec;
uchar code buffer[User][6]=
{
{"000000"}
};
uchar dz[4];
uchar time[4];
uchar mima[6];
uchar mimag[6];
uchar mimaID[6]={1,2,3,4,5,6};
uchar Address1=20,tempx=20;
uchar Address2=20;
uchar data K;
uchar data Key;
uint PageID;
uchar data querenma=11;
uchar sum[2];
int summaf,summas;
uchar xdata UserPassword[7]={0};//密碼
uchar xdata DSY_BUFFER[16] ={0};//緩存
void Red_Init(void);
//檢測脈衝寬度
unsigned char check(void);
unsigned char redchange(unsigned char rednum);
void delayms(unsigned int z)
{
unsigned int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/********************************************************************
* 名稱 : Chack(uchar User_Number)
* 功能 : 單一用戶密碼檢測
* 輸入 : User_Number
* 輸出 : 密碼正確與錯誤,錯誤返回0,正確返回1
********************************************************************/
uchar Chack(uchar User_Number)
{
uchar flag, i,temp[6],Address;
Address=User_Number*10;
for(i=0;i<6;i++)
{
temp[i]=x24c02_read(Address);
delayms(10);
Address++;
}
for(i=0;i<6;i++)
{
if(temp[i]==UserPassword[i])
flag=1;
else
flag=0;
}
return flag;
}
/********************************************************************
* 名稱 : PassWord_Chack()
* 功能 : 所有用戶密碼檢測
* 輸入 : 無
* 輸出 : 密碼正確與錯誤,錯誤返回0,正確返回1
********************************************************************/
uchar PassWord_Chack()
{
uchar i=0;
while(i<User)
{
if(Chack(i)==2)
{
Member=i+2;
return 1;
}
i++;
}
return 0;
}
void delay(uint tt)
{ uchar i;
while(tt--)
{
for(i=0;i<125;i++);
}
}
/********************************************************************
* 名稱 : UART_Init()
* 功能 : 單片機串口初始化
* 單片機:晶振11.0592MHz 波特率爲9600bps
* 指紋模塊:波特率爲9600bps,
********************************************************************/
void UART_Init()
{
SCON= 0x50; //串口方式1 //REN=1; 允許接收
PCON=0x01; //SMOD=0
TMOD= 0x01; //定時器1定時方式2
TH1= 0xFD; //11.0592MHz 模塊默認波特率爲9600bps
TL1= 0xFD;
TR0= 1; //啓動定時器
EA=1;
}
unsigned char Keycan(void) //按鍵掃描程序 P1.0--P1.3爲行線 P1.4--P1.7爲列線
{
unsigned char rcode, ccode;
P1 = 0xF0; // 發全0行掃描碼,列線輸入
if((P1&0xF0) != 0xF0) // 若有鍵按下
{
delay(1);// 延時去抖動
if((P1&0xF0) != 0xF0)
{
rcode = 0xef; // 逐行掃描初值
while((rcode&0x10) != 0)
{
P1 = rcode; // 輸出行掃描碼
if((P1&0xF0) != 0xF0) // 本行有鍵按下
{
ccode = (P1&0xF0)|0x0F;
//do{;}
while((P1&0xF0) != 0xF0); //等待鍵釋放
return ((~rcode) + (~ccode)); // 返回鍵編碼
}
else
rcode = (rcode<<1)|0x01; // 行掃描碼左移一位
}
}
}
return 0; // 無鍵按下,返回值爲0
}
void KeyDeal(unsigned char Key)
{ //unsigned char n;
if(Key!=0)
{
switch(Key)
{
case 0x12: K=1; break;
case 0x22: K=2; break;
case 0x42: K=3; break;
case 0x82: break;
case 0x13: K=4; break;
case 0x23: K=5; break;
case 0x43: K=6; break;
case 0x83: K=34;break;
case 0x14: K=7; break;
case 0x24: K=8; break;
case 0x44: K=9; break;
case 0x84: break;
case 0x18: break;
case 0x28: K=0; break;
case 0x48: K=11; break;
case 0x88: break;
default: break;
}
}
}
void SFG_identify() //自動驗證指紋 錄圖像+生成特徵+搜索
{
uchar i,ID1,ID2;
SBUF=0xef;
while(TI==0);
TI=0;
SBUF=0X01;
while(TI==0);
TI=0;
SBUF=0XFF;
while(TI==0);
TI=0;
SBUF=0XFF;
while(TI==0);
TI=0;
SBUF=0XFF;
while(TI==0);
TI=0;
SBUF=0XFF;
while(TI==0);
TI=1;
SBUF=0X01;
while(TI==0);
TI=0;
SBUF=0X00;
while(TI==0);
TI=0;
SBUF=0X03;
while(TI==0);
TI=0;
SBUF=0X11;
while(TI==0);
TI=0;
SBUF=0X00;
while(TI==0);
TI=0;
summaf=0x15;
SBUF=summaf; //校驗和
while(TI==0);
TI=0;
for(i=0;i<9;i++)
{
while(RI==0);
RI=0;}
while(RI==0);
RI=0;
querenma=SBUF;
while(RI==0);
RI=0;
ID1=SBUF;
while(RI==0);
RI=0;
ID2=SBUF;
while(RI==0);
RI=0;
while(RI==0);
RI=0; //得分
while(RI==0);
RI=0;
sum[1]=SBUF;
while(RI==0);
RI=0;
sum[0]=SBUF;
summas=(sum[1]>>8)+sum[0];
//PageID=ID1;
PageID=(ID1>>8)+ID2;
}
void shuazhiwen()
{
uchar IDs1,IDs2,IDs3;
LcmClearTXT();
PutStr(1,1,"請按手指開鎖");
SFG_identify();
while(querenma==2)
SFG_identify();
if(querenma==0)
{
LcmClearTXT();
PutStr(1,2,"門已打開");
IDs1=PageID/100;
IDs2=PageID/10%10;
IDs3=PageID%10;
PutStr(2,1,"編號爲:");
WriteCommand(0x8D); //指定第三行顯示位置
WriteData(0x30+IDs1);
WriteData(0x30+IDs2);
WriteData(0x30+IDs3);
jidianqi=0;
delay(2500);
jidianqi=1;
}
else if(querenma==9)
{
LcmClearTXT();
PutStr(1,1,"沒搜索到指紋");
PutStr(2,1,"請重新按手指");
}
else
{
LcmClearTXT();
PutStr(1,1,"接收包出錯");
}
delay(2000);
// while(Keycan()==0);
}
void addfinger() //錄入用戶指紋
{
uchar IDa1,IDa2,IDa3;
LcmClearTXT();
PutStr(1,2,"請按手指");
SFG_getimage();
while(querenma!=0)
SFG_getimage();
SFG_genchar(buffer1ID);
UART_Init();
SFG_fastsearch(buffer1ID);
while(querenma==1)
SFG_fastsearch(buffer1ID);
if(querenma==0)
{
LcmClearTXT();
PutStr(1,1,"該指紋已存儲");
PutStr(3,0," 按任意鍵繼續");
while(Keycan()==0);
}
else if(querenma==9)
{
LcmClearTXT();
PutStr(1,1,"請再次按手指");
SFG_enroll();
while(querenma==2)
SFG_enroll();
LcmClearTXT();
if(querenma==0)
{
IDa1=PageID/100;IDa2=PageID/10%10;IDa3=PageID%10;
PutStr(1,1,"指紋採集成功");
PutStr(2,1,"編號爲:");
WriteCommand(0x8D);
WriteData(0x30+IDa1);WriteData(0x30+IDa2);WriteData(0x30+IDa3);
}
else if(querenma!=0)
{
PutStr(1,1,"指紋採集失敗");
PutStr(2,1,"請重新操作");
}
PutStr(3,0," 按任意鍵繼續");
while(Keycan()==0);
}
LcmClearTXT();
}
void deletfinger() //刪除用戶指紋
{
uchar i,j=0;
LcmClearTXT();
PutStr(1,0,"輸入刪去的指紋號");
for(i=0;i<5;i++)dz[i]=0;
Key=Keycan();
while(Key!=queren)
{
Key=Keycan();
KeyDeal(Key);
delay(30); //按鍵有抖動@@@
if(Key==0)K=10;
if((K>=0)&&(K<=9))
{
dz[j]=K;
if(j<3)
{
WriteCommand(0x88+j); //指定第三行顯示位置
WriteData(0x30+dz[j]);
}
++j;
if(j==4)
j=3; //@@#yinhuang
} //顯示LCD12864並行顯示
if(K==34) //按了刪除鍵
{
if(j==0)
{
WriteCommand(0x88); //指定第三行顯示位置
WriteData(0x20);
}
else
{
--j;
WriteCommand(0x88+j); //指定第三行顯示位置
WriteData(0x20);
}
}
}
if(j>=2)
PageID=dz[2]+dz[1]*10+dz[0]*100;
if(j==1)
PageID=dz[1]+dz[0]*10;
if(j==0)
PageID=dz[0];
SFG_deletchar(PageID);
if(querenma==1)
{
LcmClearTXT();
PutStr(1,1,"刪去指紋號成功!");
}
else
{
LcmClearTXT();
PutStr(1,2,"刪去指紋號失敗!");
}
PutStr(2,0," 按任意鍵繼續");
while(Keycan()==0);
LcmClearTXT();
}
void gaimima() //修改用戶密碼
{
uchar i,j=0,mima1[4],mima2[4];
uchar k,temp;
LcmClearTXT();
PutStr(1,1,"請輸入新密碼");
for(i=0;i<6;i++)mima1[i]=0;
Key=Keycan();
while(Key!=queren)
{
Key=Keycan();
KeyDeal(Key);
delay(30);
if(Key==0)K=10;
if((K>=0)&&(K<=9))
{
mima1[j]=K;
if(j<6)
{
WriteCommand(0x89+j); //指定第三行顯示位置
WriteData(0x0f);
}
++j;
if(j==7)
j=6; //@@#yinhuang
} //顯示LCD12864並行顯示
if(K==34) //按了刪除鍵
{
if(j==0)
{
WriteCommand(0x89); //指定第三行顯示位置
WriteData(0x20);
}
else
{
--j;
WriteCommand(0x89+j); //指定第三行顯示位置
WriteData(0x40);
}
}
}
LcmClearTXT();
LcmClearTXT();
j=0;
PutStr(1,0,"請再次輸入新密碼");
for(i=0;i<6;i++)mima2[i]=0;
Key=Keycan();
while(Key!=queren)
{
Key=Keycan();
KeyDeal(Key);
delay(30);
if(Key==0)K=10;
if((K>=0)&&(K<=9))
{
mima2[j]=K;
if(j<6)
{
WriteCommand(0x89+j); //指定第三行顯示位置
WriteData(0x0f);
}
++j;
if(j==7)
j=6; //@@#yinhuang
} //顯示LCD12864並行顯示
if(K==34) //按了刪除鍵
{
if(j==0)
{
WriteCommand(0x89); //指定第三行顯示位置
WriteData(0x20);
}
else
{
--j;
WriteCommand(0x89+j); //指定第三行顯示位置
WriteData(0x20);
}
}
}
LcmClearTXT();
if((mima1[0]==mima2[0])&&(mima1[1]==mima2[1])&&(mima1[2]==mima2[2])&&(mima1[3]==mima2[3])&&(mima1[4]==mima2[4])&&(mima1[5]==mima2[5]))
{
for(i=0;i<6;i++)
mimaID[i]=mima1[i];
/////////////////////////////////////////////////////
for(i=0;i<6;i++) //密碼限制在6位以內
{
UserPassword[i]=mima1[i]+0x30;
}
temp=(Member-1)*100;
delayms(5);
for(k=1;k<6;k++)
{
x24c02_write(temp,UserPassword[k]);
delayms(10);
temp++;
}
//////////////////////////////////////////////////////
PutStr(0,1,"密碼修改成功");
PutStr(3,0," 按任意鍵繼續");
while(Keycan()==0);
}
else
{
PutStr(0,0," 密碼修改失敗 ");
PutStr(1,0,"兩次輸入的密碼不");
PutStr(2,0,"一致,請重新操作");
PutStr(3,0," 按任意鍵繼續");
while(Keycan()==0);
}
LcmClearTXT();
}
void zhu()
{
LcmClearTXT();
PutStr(1,2,"門已打開");
jidianqi=0;
delay(2500);
jidianqi=1;
PutStr(3,0," 按任意鍵繼續");
while(Keycan()==0);
}
void guanliyuan() //管理員
{ uchar i,j=0,x=1;
uchar Right_flag;
LcmClearTXT();
PutStr(1,1,"請輸入密碼:");
for(i=0;i<6;i++)mima[i]=0;
Key=Keycan();
while(Key!=queren)
{
Key=Keycan();
KeyDeal(Key);
delay(30);
if(Key==0)K=10;
if((K>=0)&&(K<=9))
{
mima[j]=K;
if(j<6)
{
WriteCommand(0x89+j); //指定第三行顯示位置
WriteData(0x0f);
}
++j;
if(j==7)
j=6;
} //顯示LCD12864並行顯示
if(K==34) //按了刪除鍵
{
if(j==0)
{
WriteCommand(0x89); //指定第三行顯示位置
WriteData(0x20);
}
else
{
--j;
WriteCommand(0x89+j); //指定第三行顯示位置
WriteData(0x20);
}
}
}
LcmClearTXT();
for(i=0;i<6;i++)
{
UserPassword[i]=mima[i]+0x30;
}
if(j==6){Right_flag=PassWord_Chack(); }
if (Right_flag==1)
{
Right_flag=0;
while(Keycan()!=tuichu)
{
PutStr(0,0,"按鍵1 : 增加指紋");
PutStr(1,0,"按鍵2 : 刪去指紋");
PutStr(2,0,"按鍵3 : 手動開鎖");
PutStr(3,0,"按鍵4 : 修改密碼");
KeyDeal(Keycan());
switch(K)
{
case 1: addfinger();K=8; break;
case 2: deletfinger(); break;
case 3: zhu(); K=6; break;
case 4: gaimima(); break;
default: break;
}
}
}
else
{
PutStr(1,2,"密碼錯誤");
PutStr(2,0," 請重新操作!");
PutStr(3,0," 按任意鍵繼續");
while(Keycan()==0);
}
Key=0;
LcmClearTXT();
}
void fan()
{
// PutStr(1,2,"請先按鍵");
// PutStr(2,2,"再刷指紋");
PutStr(0,2,"歡迎使用");
PutStr(1,1,"指紋門禁系統");
PutStr(3,1,"請按手指開鎖");
Key=Keycan();
if(k2==1) //指紋刷機
{
LcmClearTXT();
shuazhiwen();
LcmClearTXT();
}
if(Key==0x81) //管理員操作
{
LcmClearTXT();
guanliyuan();
LcmClearTXT();
}
}
源碼+電路圖 下載:關注公衆號,首頁回覆“指紋密碼鎖”獲取資料