功能:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶显示功能界面
2.支持添加/删除/搜索指纹,更改密码
3.支持指纹和密码开锁
4.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB :
主程序:文章来源:https://www.toymoban.com/news/detail-540712.html
#include "main.h"
enum ModeFlag_Df ModeFlag; //模式,正常开锁模式,设置密码模式,设置指纹模式
unsigned char stepCnt = 0;
char setIndex = 0;
xdata unsigned char dis0[16];
bit refreshFlag = 1;
unsigned char inpPasswd[4]; //输入密码
unsigned char Passwd[4] = {'0','0','0','0'}; //初始密码
unsigned char isNewFlag = 1;
void main()
{
LOCK = OPEN; //锁打开
DelayMs(200);
LOCK = CLOSE; //锁关闭
EEPROM_Init();
DelayMs(200);
// EEPROM_WriteByte(IS_NEW_ADDR, 1);
ReadPassword(); //初次使用时屏蔽此句
if (isNewFlag != 0)
{
unsigned char i = 0;
EEPROM_WriteByte(IS_NEW_ADDR, 0);
for (i = 0; i < 4; i++)
{
Passwd[i] = '0';
}
WritePassword();
}
LCD_Init(); //初始化液晶
LCD_DispStr(0, 0, "Fingerprint Lock"); //液晶开机显示界面
Timer0_Init(); //初始化定时器
Uart_Init(); //初始化串口
DelayMs(200);
DelayMs(200);
DelayMs(200); //延时500MS,等待指纹模块复位
Device_Check(); //校对指纹模块是否接入正确,液晶做出相应的提示
DelayMs(200);
DelayMs(200);
DelayMs(200); //对接成功界面停留一定时间
LCD_Clear();
LCD_DispStr(0, 0, "Enter pw or fp: "); //显示
while (1)
{
if (refreshFlag == 1)
{
TR0 = 0;
refreshFlag = 0;
if (ModeFlag == NORMAL)
{
if (stepCnt == 0)
{
LCD_DispStr(0, 0, "Enter pw or fp: "); //显示
LCD_DispStr(0, 1, " ");
}
CTRL_BY_FPM10A_Find_Fingerprint(); //搜索指纹
}
else if (ModeFlag == OLD_PW)
{
if (stepCnt == 0)
{
LCD_DispStr(0, 0, "Old Password: "); //显示
LCD_DispStr(0, 1, " ");
}
}
else if (ModeFlag == SELECT_PW_FP)
{
LCD_DispStr(0, 0, "A.Set Password "); //显示
LCD_DispStr(0, 1, "B.SetFingerprint");
}
else if (ModeFlag == SET_PW)
{
if (stepCnt == 0)
{
LCD_DispStr(0, 0, "New Password: "); //显示
LCD_DispStr(0, 1, " ");
}
}
else if (ModeFlag == SET_FP_SELECT)
{
Into_FP();
}
else if (ModeFlag == FIND_FP)
{
FPM10A_Find_Fingerprint();
}
else if (ModeFlag == ADD_FP)
{
FPM10A_Add_Fingerprint();
}
else if (ModeFlag == DELETE_FP)
{
LCD_DispStr(0, 0, " Empty All ");
LCD_DispStr(0, 1, " A.Yes B.No ");
}
TR0 = 1;
}
Key_Process(Calkey_scan());
}
}
void Timer0_Init(void)
{
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
EA = 1; //总中断打开
ET0 = 1; //定时器中断打开
TR0 = 1; //定时器开关打开
}
void Timer0_Interrupt(void) interrupt 1
{
static unsigned char time_20ms = 0;
TH0 = (65536 - 18432) / 256; //重新赋值 20ms
TL0 = (65536 - 18432) % 256;
time_20ms++;
if (time_20ms > 10) //定时显示
{
refreshFlag = 1;
time_20ms = 0;
}
}
void Open_Lock(void) //打开电磁锁
{
LOCK = OPEN;
DelayMs(250);
DelayMs(250);
LOCK = CLOSE;
}
void Into_FP(void)
{
/**************进入主功能界面****************/
if (setIndex == 0)
{
LCD_DispStr(0, 0, " *Search finger "); //第一排显示搜索指纹
LCD_DispStr(0, 1, " Add Delete"); //添加和删除指纹
}
else if (setIndex == 1)
{
LCD_DispStr(0, 0, " Search finger "); //第一排显示搜索指纹
LCD_DispStr(0, 1, " *Add Delete"); //添加和删除指纹
}
else if (setIndex == 2)
{
LCD_DispStr(0, 0, " Search finger "); //第一排显示搜索指纹
LCD_DispStr(0, 1, " Add *Delete"); //添加和删除指纹
}
}
void ReadPassword(void)
{
unsigned char i = 0;
isNewFlag = EEPROM_ReadByte(IS_NEW_ADDR);
for (i = 0; i < 4; i++)
{
Passwd[i] = EEPROM_ReadByte(PASSWORD_ADDR + i);
}
}
void WritePassword(void)
{
unsigned char i = 0;
for (i = 0; i < 4; i++)
{
EEPROM_WriteByte(PASSWORD_ADDR + i, Passwd[i]);
}
}
void Key_Process(unsigned char keyCode)
{
static unsigned char count_num = 0;
static unsigned char i = 0;
if (ModeFlag == NORMAL)
{
if (keyCode == 'A') //设置键
{
ModeFlag = OLD_PW;
}
else if ((keyCode >= '0') && (keyCode <= '9'))
{
stepCnt = 1;
if (count_num <= 3)
{
inpPasswd[count_num] = keyCode;
// LCD_DispOneChar((count_num+6), 1, inpPasswd[count_num]);
LCD_DispOneChar((count_num+6), 1, '*');
count_num++;
}
if (count_num == 4)
{
DelayMs(250);
DelayMs(250);
for (i = 0; i < 4; i++) //判断密码是否正确
{
if (inpPasswd[i] != Passwd[i])
break;
}
if (i == 4) //密码输入正确
{
LCD_DispStr(0, 1, " Open "); //显示状态
Open_Lock();
}
else
{
LCD_DispStr(0, 1, " Error "); //显示状态
Buzz_Times(2);
}
DelayMs(250);
DelayMs(250);
stepCnt = 0;
count_num = 0;
}
}
}
else if (ModeFlag == OLD_PW)
{
if ((keyCode >= '0') && (keyCode <= '9')) //输入旧密码
{
stepCnt = 1;
if (count_num <= 3)
{
inpPasswd[count_num] = keyCode;
// LCD_DispOneChar((count_num+6), 1, inpPasswd[count_num]);
LCD_DispOneChar((count_num+6), 1, '*');
count_num++;
}
if (count_num == 4)
{
DelayMs(250);
DelayMs(250);
for (i = 0; i < 4; i++) //判断密码是否正确
{
if (inpPasswd[i] != Passwd[i])
break;
}
if (i == 4) //密码输入正确
{
LCD_DispStr(0, 1, " Right "); //显示状态
DelayMs(250);
DelayMs(250);
ModeFlag = SELECT_PW_FP;
}
else
{
LCD_DispStr(0, 1, " Error "); //显示状态
Buzz_Times(2);
DelayMs(250);
DelayMs(250);
ModeFlag = NORMAL; //返回最初界面
}
stepCnt = 0;
count_num = 0;
}
}
}
else if (ModeFlag == SELECT_PW_FP)
{
if (keyCode == 'A')
{
ModeFlag = SET_PW;
}
else if (keyCode == 'B')
{
ModeFlag = SET_FP_SELECT;
setIndex = 0;
}
}
else if (ModeFlag == SET_PW)
{
if ((keyCode >= '0') && (keyCode <= '9')) //设置新密码
{
stepCnt = 1;
if (count_num <= 3)
{
Passwd[count_num] = keyCode;
LCD_DispOneChar((count_num+6), 1, Passwd[count_num]);
count_num++;
}
if (count_num == 4)
{
DelayMs(250);
DelayMs(250);
LCD_DispStr(0, 1, " OK "); //显示状态
WritePassword();
DelayMs(250);
DelayMs(250);
ModeFlag = NORMAL;
stepCnt = 0;
count_num = 0;
}
}
else if (keyCode == 'B')
{
ModeFlag = SELECT_PW_FP;
}
}
else if (ModeFlag == SET_FP_SELECT)
{
if (keyCode == 'A') //确认
{
switch (setIndex)
{
case 0: //搜索指纹
ModeFlag = FIND_FP;
// FPM10A_Find_Fingerprint();
break;
case 1: //添加指纹
ModeFlag = ADD_FP;
// FPM10A_Add_Fingerprint();
break;
case 2: //清空指纹
ModeFlag = DELETE_FP;
// FPM10A_Delete_All_Fingerprint();
break;
}
}
else if (keyCode == 'B')
{
ModeFlag = SELECT_PW_FP;
}
else if (keyCode == '*') //设置位置移动
{
setIndex--;
if (setIndex < 0)
{
setIndex = 2;
}
}
else if (keyCode == '#') //设置位置移动
{
setIndex++;
if (setIndex > 2)
{
setIndex = 0;
}
}
}
else if (ModeFlag == FIND_FP)
{
if (keyCode == 'B')
{
ModeFlag = SET_FP_SELECT;
}
}
else if (ModeFlag == DELETE_FP)
{
if (keyCode == 'A')
{
LCD_DispStr(0, 0, " Emptying ");
LCD_DispStr(0, 1, " ");
DelayMs(250);
FINGERPRINT_Cmd_Delete_All_Model();
FPM10A_Receive_Data(12);
LCD_DispStr(0, 0, " All empty ");
LCD_DispStr(0, 1, " ");
Buzz_Times(3);
DelayMs(250);
ModeFlag = NORMAL;
}
else if (keyCode == 'B')
{
ModeFlag = SET_FP_SELECT;
}
}
if (keyCode == 'D')
{
ModeFlag = NORMAL;
stepCnt = 0;
count_num = 0;
setIndex = 0;
}
}
实物演示视频:
https://www.bilibili.com/video/BV1b3411j7DF/文章来源地址https://www.toymoban.com/news/detail-540712.html
到了这里,关于基于51单片机的带矩阵键盘指纹密码电子锁原理图PCB的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!