功能介绍:
0.本系统采用STC12C5A60S2-35I作为单片机,因本系统需要使用WiFi模块和指纹模块,两者均是串口串口通信,89C52只有一路串口,因此无法满足需求,STC12C5A60S2-35I加强型51具备两路串口。
1.采用LCD1602液晶显示相关信息
2.密码可通过矩阵键盘设置,解锁支持矩阵键盘和指纹
3.支持WiFi通信解锁功能
4.采用DC005作为电源接口可直接输入12V给整个系统供电,系统具备12V转5V电源模块
原理图:
PCB:
主程序:
#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 tempPasswd[4] = {'x','x','x','x'}; //临时密码
unsigned char tempEnable = 0; //临时密码使能
unsigned char isNewFlag = 1;void main()
{LOCK = OPEN; //锁打开DelayMs(200);LOCK = CLOSE; //锁关闭LCD_Init(); //初始化液晶LCD_DispStr(0, 0, " Welcome! "); //液晶开机显示界面DelayMs(200);DelayMs(200);DelayMs(200);LCD_DispStr(0, 0, " Waiting... "); //液晶开机显示界面Uart2_Init();Uart2_Send_Str("AT+CIPMUX=1\r\n", 13); //打开多连接DelayMs(50);Uart2_Send_Str("AT+CIPSERVER=1,8080\r\n", 21); //建立服务 端口号为8080DelayMs(200);IE2 &= 0xFE; //串口2中断关闭DelayMs(200);Uart1_Init(); //初始化串口DelayMs(200);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_Clear();LCD_DispStr(0, 0, "Fingerprint Lock"); //液晶开机显示界面
// 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: "); //显示Timer0_Init(); //初始化定时器while (1){if (refreshFlag == 1){refreshFlag = 0;if (modeFlag == NORMAL){if (stepCnt == 0){LCD_DispStr(0, 0, "Enter pw or fp: "); //显示LCD_DispStr(0, 1, " ");}TR0 = 0;IE2 &= 0xFE; //串口2中断关闭CTRL_BY_FPM10A_Find_Fingerprint(); //搜索指纹IE2 |= 0x01; //串口2中断打开TR0 = 1;}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){IE2 &= 0xFE; //串口2中断关闭Into_FP();IE2 |= 0x01; //串口2中断打开}else if (modeFlag == FIND_FP){IE2 &= 0xFE; //串口2中断关闭FPM10A_Find_Fingerprint();IE2 |= 0x01; //串口2中断打开}else if (modeFlag == ADD_FP){IE2 &= 0xFE; //串口2中断关闭FPM10A_Add_Fingerprint();IE2 |= 0x01; //串口2中断打开}else if (modeFlag == DELETE_FP){LCD_DispStr(0, 0, " Empty All ");LCD_DispStr(0, 1, " A.Yes B.No ");}}IE2 &= 0xFE; //串口2中断关闭Key_Process(Calkey_scan());IE2 |= 0x01; //串口2中断打开}
}void Timer0_Init(void)
{AUXR &= 0x7F; //定时器时钟12T模式TMOD &= 0xF0; //设置定时器模式TL0 = 0x00; //设置定时初值TH0 = 0x4C; //设置定时初值 50msTF0 = 0; //清除TF0标志TR0 = 1; //定时器0开始计时ET0 = 1; //开启定时器1中断PT0 = 1;EA = 1; //总中断打开
}void Timer0_Routine(void) interrupt 1
{static unsigned int time_50ms = 0;TL0 = 0x00; //设置定时初值TH0 = 0x4C; //设置定时初值 50mstime_50ms++;if (time_50ms > 5) //定时显示{refreshFlag = 1;time_50ms = 0;//BUZZER = ~BUZZER;}}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;unsigned char i = 0;unsigned char j = 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 (tempEnable == 1) //可以输入临时密码{for (j = 0; j < 4; j++) //判断密码是否正确{if (inpPasswd[j] != tempPasswd[j])break;}}if (i == 4 || j == 4) //密码输入正确{if (j == 4){tempEnable = 0; //清空临时密码使能for (j = 0; j < 4; j++) //判断密码是否正确{tempPasswd[j] = 'x'; //清空临时密码//LCD_DispOneChar(0+j, 1, tempPasswd[j]); //显示状态}}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;// FINGERPRINT_Cmd_Delete_All_Model();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/BV19r4y147zJ/