目录
资料下载地址:基于STM32蓝牙密码锁
一、项目功能概述
主控用的STM32F407ZET6,通过蓝牙模块HC05使手机与STM32单片机进行蓝牙通信,通过手机输入密码控制密码锁开关。也可通过矩阵按键输入密码,当密码输入错误5次后,蜂鸣器报警,并且通过按键修改输入密码。
二、原理图
三、实物图
上电显示如下图
按任意按键进入主界面
手机app解锁界面
四、代码
主程序
int main(void)
{
u16 set=0;
int key_num;
int time1;
int a=0,b=0;
int time2; //锁屏时间
char arrow=0; //箭头位子
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
uart_init(115200);
delay_init(84);
LED_Init();
uart3_init(9600);// 蓝牙初始化
Button4_4_Init();//矩阵按键初始化
OLED_Init(); //oled初始化
W25QXX_Init(); //初始化W25Q128
exfuns_init(); //为fatfs相关变量申请内存
f_mount(fs[0],"0:",1); //挂载SD卡
f_mount(fs[1],"1:",1); //挂载FLASH.
delay_ms(3000);
OLED_Clear();
FENG=0;
STMFLASH_Read(0X08020004,(u32*)Pwd,2); //读取密码1
STMFLASH_Read(0X08090004,(u32*)Pwd2,2); //读取密码2
STMFLASH_Read(0X080f0004,(u32*)cardid,1); //读取卡号1
strcpy(Pwd,"123456");
while(1)
{
MAIN:
OLED_Show_Font(56,48,0);//显示锁图标
while(1)
{
time1++;
if((time1%5000)==0)//每200ms检测一次卡片,蓝牙数据
{
time1=0;
//手机蓝牙解锁密码1
Error=usart3_cherk((char*)Pwd);
if(Error==0){
OLED_Clear();
Show_Str(20,10,128,24,"ok!",24,0);
delay_ms(1000);
goto MENU;
}
else
OLED_Show_Font(56,48,0);//锁
//手机蓝牙解锁密码2
Error=usart3_cherk((char*)Pwd2);
if(Error==0){
OLED_Clear();
Show_Str(20,10,128,24,"ok!",24,0);
delay_ms(1000);
goto MENU;
}
else
OLED_Show_Font(56,48,0);//锁
}
//密码锁
key_num=Button4_4_Scan(); //按键扫描
if(key_num!=-1)
{
Error=password();
if(Error==0)goto MENU;
else
OLED_Show_Font(56,48,0);//锁
}
}
//主界面
MENU:
//主页菜单显示
FENG=0;
OLED_Clear();
if(arrow<3){
Show_Str(5,arrow*16,128,16,"->",16,0);
set=0;}
else {
Show_Str(5,(arrow-3)*16,128,16,"->",16,0);
set=3;}
Show_Str(25,0,128,16,setup[set],16,0);
Show_Str(25,16,128,16,setup[set+1],16,0);
Show_Str(25,32,128,16,setup[set+2],16,0);
Show_Str(0,52,128,12,"down up NO sure",12,0);
time2=0;
while(1)
{
//超时锁屏
time2++;
if(time2>50000 | key_num==15){
time2 =0;
OLED_Clear();
Show_Str(30,20,128,16,"Timeout lock screen",16,0);
delay_ms(500);
OLED_Show_Font(56,48,0);//锁
delay_ms(1000);
OLED_Clear();
goto MAIN;
}
//功能选项选择
key_num=Button4_4_Scan();
if(key_num)
{
if(key_num==13){
if(arrow>0)arrow--;
goto MENU;
}
if(key_num==14){
if(arrow<5)arrow++;
goto MENU;
}
if(key_num==16){
switch(arrow)
{
case 2:SetPassworld();break;//修改密码
}
goto MENU;
}
}delay_ms(1);
}
}
}
密码检测
int password(void)
{
int key_num=0,i=0,satus=0;
u16 num=0,num2=0,time3=0;
u8 pwd[11]=" ";
u8 hidepwd[11]=" ";
OLED_Clear();//清屏
Show_Str(5,0,128,16,"MIMA: ",16,0);
Show_Str(10,16,128,12," 1 2 3 4 ",12,0);
Show_Str(10,28,128,12," 5 6 7 8 ",12,0);
Show_Str(10,40,128,12," 9 0 Dis ",12,0);
Show_Str(10,52,128,12,"Del Clr Bck Ok ",12,0);
while(1)
{
key_num=Button4_4_Scan();
if(key_num)
{
time3=0;
if(key_num>=1 && key_num<10 && i>=0 && i<10){ //‘1-9’键
pwd[i]=key_num+0x30;
hidepwd[i]='*';
i++;
}
if(key_num==10 && i>=0 && i<10){ //‘0’键
pwd[i]=0x30;
hidepwd[i]='*';
i++;
}
if(key_num==12){//显示
satus=!satus;
}
if(key_num==13 && i>0){
pwd[--i]=' '; //‘del’键
hidepwd[i]=' ';
}
if(key_num==14){
while(i--){
pwd[i]=' '; //‘清空’键
hidepwd[i]=' '; //‘清空’键
}
i=0;
}
if(key_num==15){//返回
OLED_Clear();//清屏
delay_ms(500);
return -1;
}
if(key_num==16)break; //‘Enter’键
}
if(satus==0)OLED_ShowString(53,0,hidepwd,12);
else OLED_ShowString(53,0,pwd,12);
time3++;
if(time3%1000==0){
OLED_Clear();//清屏
return -1;
}
}
for(i=0; i<10; i++){ //验证虚伪密码
if(pwd[i]==Pwd[num])num++;
else num=0;
if(num==6)
break;
}
for(i=0; i<10; i++){ //验证密码
if(pwd[i]==Pwd2[num2])num2++;
else num2=0;
if(num2==6)
break;
}
if(num==6 | num2==6){
OLED_Clear();//清屏
Show_Str(20,10,128,24,"OK!",24,0);
delay_ms(1000);
OLED_Show_Font(56,48,1);//开锁
delay_ms(1500);
OLED_Clear();//清屏
return 0;
}
else
{
a++;
if(a==6)
{
FENG=1;
}
OLED_Clear();//清屏
Show_Str(20,20,128,16,"Error",16,0);
delay_ms(1500);
OLED_Clear();//清屏
return -1;
}
}
五、资料清单