基于51单片机的指纹密码锁

2020-12-12 18:09发布

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)指纹模块处理程序(部分代码)


因为源码比较大,我就不想全都贴出来了,如果需要详细了解源码可下载源码查看(下载链接在文章最后)



				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();
  }

}	

源码+电路图 下载:关注公众号,首页回复“指纹密码锁”获取资料


在这里插入图片描述