当前位置: 首页 > news >正文

基于51单片机密码锁—有3个密码lcd1602显示

基于51单片机密码锁设计

(仿真+程序)

功能介绍

具体功能:

1.存有三个密码可开锁,通过按键输入,LCD1602显示信息;

2.密码输入正确绿LED亮,继电器吸合;密码输入错误红LED亮,继电器闭合;密码连续输入错误3次就会被锁定;

3.密码可修改,密码只能为0000-0255;

4.接串口传输显示操作信息;

​演示视频:

基于51单片机密码锁—有3个密码lcd1602显示

添加图片注释,不超过 140 字(可选)

#include <reg52.h>
#include <intrins.h>
/***微信公众号:木子单片机****
************************************/
#include "lxl_24C02C.h"
#include "uart.h"
#include "LCD1602.h"
#include <keypad44.h>sbit greenled=P2^3;						//Enter the right passwords,green led will lighting.
sbit lock=P2^4;								//Enter the right passwords,electronic lock will unlock.
sbit redled=P2^5;							//Enter the wrong passwords,red led will lighting.
void show_base_infromation()	//show something personal information on the screen of LCD1206.just delay for a moment than cleanning all screen.
{uchar i;LCMDisplayString(0,0,"Design:muzi");LCMDisplayString(1,0,"My ID:123456789");	for(i=0;i<4;i++)keypad_delay(65535);		//delay for a momentLCMClear();															//clean all screen.
}void set_init_password()			//set users' initialize passwords. 
{Master_writeByte(1,1);			//The user 1 ,password:0001,password saved in register 1 of 24C02C.Master_writeByte(2,2);			//The user 2 ,password:0002,password saved in register 2 of 24C02C.Master_writeByte(3,3);			//The user 3 ,password:0003,password saved in register 3 of 24C02C.
}
int get_user_password(uchar i)//Get users' password ,i is the register number of the specific user.
{int read=Master_readByte(i);//read the appointed register.return _cror_(read,8);			//translated the value to int,and return the value.
}
void main()
{uchar presstimes=0;					//the register keep user's press times,maximum are 4,because the munber of password bits are defined as 4.  uchar enter_times=0;				//if user enter wrong password ,the register keep user's press times of "Enter",maximum are 3,beyond 3 account will locked(forbid to enter any number.).bit changepswd_state=0;			//flage bit of user pressed "Reset password" key,1:pressed,0:not pressed.bit change_use1=0;					//flage bit of user1 entered the old password correctly,and input the new passwords done.bit change_use2=0;					//*****************************similar to change_use1***********************************bit change_use3=0;					//*****************************similar to change_use1***********************************bit beyond3times=1;					//flage bit of users' entered the wrong password beyond 3 times.1: not beyond,0:beyond.int readkeypad;														//the register keep the keypad return value.int keypadvalue=0;												//the register keep the value when 4 times pressed.int press1,press2,press3,press4;					//press1:the value of 1st keypad return value multiple oppointed scale.press2-3 are similar to this.UartInit();																//initialize the USART for printf() to debug.LCMInit();																//initialize the LCD1206.printf("\nshow some \npersonal \ninfromation!\n");show_base_infromation();printf("\nplease enter your\npassword\n");keypad_delay(65535);	set_init_password();LCMDisplayString(0,0,"Input pswd:");LCMDisplayString(1,0,"You error 0 Time");while(1){readkeypad=key_scan();if(((readkeypad<10)||(readkeypad==14)||(readkeypad==13))&&beyond3times)//allowed press 0-9,"clean" or "enter" when account not lock(wrong password not beyond 3 times).{presstimes=presstimes+1;switch(presstimes){case 1:															//the 1st bit of 4bits password.{press1=readkeypad*1000;	LCMDisplayString(0,11,"*");		printf("pressed %d\n",readkeypad);}break;case 2:															//the 2nd bit of 4bits password.{press2=readkeypad*100;LCMDisplayString(0,11,"**");printf("pressed %d\n",readkeypad);}break;case 3:															//the 3rd bit of 4bits password.{press3=readkeypad*10;LCMDisplayString(0,11,"***");printf("pressed %d\n",readkeypad);}break;case 4:															//the 4th bit of 4bits password.{press4=readkeypad;LCMDisplayString(0,11,"****");printf("pressed %d\n\n",readkeypad);}break;default:break;				}			}if((presstimes<=5)&&(readkeypad>11))		//Entered 4bits password,checking whether "Enter" or "Clean" pressed.{switch(readkeypad){case 14:													//pressed 'Enter' key{keypadvalue=press1+press2+press3+press4;presstimes=0;if(enter_times<3)LCMDisplayString(0,11,"    ");if(change_use1)									{Master_writeByte(1,keypadvalue);change_use1=0;LCMClear();LCMDisplayString(0,0,"Input pswd:");LCMDisplayString(1,0,"You error 0 Time");}if(change_use2){Master_writeByte(2,keypadvalue);change_use2=0;LCMClear();LCMDisplayString(0,0,"Input pswd:");LCMDisplayString(1,0,"You error 0 Time");}if(change_use3){Master_writeByte(3,keypadvalue);change_use3=0;LCMClear();LCMDisplayString(0,0,"Input pswd:");LCMDisplayString(1,0,"You error 0 Time");}//printf("pswd changed!\n");if((keypadvalue==get_user_password(1))||(keypadvalue==get_user_password(2))||(keypadvalue==get_user_password(3)))//matching password saved in 24C02C,only password are correct can match successful.{greenled=0;lock=0;redled=1;printf("\nWelcome You to\nGo Home!\n");enter_times=0;LCMDisplayChar(1,10,'0');}else														//match password failed.{greenled=1;lock=1;redled=0;printf("\nPasswords error\nplease try\nagain!\n");enter_times++;switch(enter_times)						//show times of enter wrong password.{case 0:LCMDisplayChar(1,10,'0');break;case 1:LCMDisplayChar(1,10,'1');break;case 2:LCMDisplayChar(1,10,'2');break;default:										//enter wrong password 3 times,lock the account!{beyond3times=0;LCMClear();LCMDisplayString(0,0,"Account Locked!");LCMDisplayString(1,0,"Call 1234567890");printf("\nInput error\nbeyond 3 times\nthe account\nlocked!\nplease Contact \nadministrator\n");}break;}}}break;case 13:													//press 'Clean' key,clean all entered numbers.{presstimes=0;LCMDisplayString(0,11,"    ");greenled=1;lock=1;printf("\nPressed Clean!\n");}break;default:break;}}if((presstimes==5)&&(readkeypad<10))//if 5th entered is a number of 0-9.clean all entered passwords,because passwords were designed 4bits.{				presstimes=0;LCMDisplayString(0,11,"    ");printf("\nPlease Reenter Your\nPassword\n");}if(readkeypad==12)									//Press "reset password" key,enter to reset passwords process while loop.{changepswd_state=1;LCMClear();LCMDisplayString(0,0,"Enter Old pswd:");printf("\nEnter your old\npswd now:\n");}else changepswd_state=0;while(changepswd_state)							//Reset passwords while loop{readkeypad=key_scan();if((readkeypad<10)||(readkeypad==14)||(readkeypad==13)){presstimes=presstimes+1;switch(presstimes){case 1:	{press1=readkeypad*1000;	LCMDisplayString(1,0,"*");		printf("pressed %d\n",readkeypad);}break;case 2:{press2=readkeypad*100;		LCMDisplayString(1,0,"**");		printf("pressed %d\n",readkeypad);}break;case 3:{press3=readkeypad*10;		LCMDisplayString(1,0,"***");	printf("pressed %d\n",readkeypad);}break;case 4:	{press4=readkeypad;				LCMDisplayString(1,0,"****");	printf("pressed %d\n\n",readkeypad);}break;default:break;				}			}if((presstimes<=5)&&(readkeypad>11)){switch(readkeypad){case 14:										//press 'enter' key{keypadvalue=press1+press2+press3+press4;presstimes=0;LCMDisplayString(1,0,"    ");if(keypadvalue==get_user_password(1))				//matched user1's passwords.{LCMClear();LCMDisplayString(1,0,"Enter New pswd:");presstimes=0;change_use1=1;changepswd_state=0;}else if(keypadvalue==get_user_password(2))	//matched user2's passwords.{LCMClear();LCMDisplayString(1,0,"Enter New pswd:");presstimes=0;change_use2=1;changepswd_state=0;}else if(keypadvalue==get_user_password(3))	//matched user3's passwords.{LCMClear();LCMDisplayString(1,0,"Enter New pswd:");presstimes=0;change_use3=1;changepswd_state=0;}else																				//Didn't match user1's passwords.{presstimes=0;changepswd_state=0;change_use1=0;change_use2=0;change_use3=0;printf("\nOld Pswd Error!\n");LCMClear();LCMDisplayString(0,0,"Input pswd:");LCMDisplayString(1,0,"You error   Time");}										}break;					default:break;}				}LCMDelay(1000);																					//keypad scan delay time.}			LCMDelay(1000);																						//keypad scan delay time.}}

硬件设计

使用元器件:

单片机:AT89C52;

(注意:单片机是通用的,无论51还是52、无论stc还是at都一样,引脚功能都一样。程序也是一样的。)

 

设计资料

01仿真图

本设计使用proteus8.9版本设计!具体如图!

 

02程序

本设计使用软件keil5版本编程设计!具体如图!

 

03设计资料

        资料获取请关注同名公众号,全部资料包括程序(含注释)、仿真源文件等。具体内容如下,全网最全!!

 

可以关注下方公众号!

点赞分享一起学习成长。


http://www.mrgr.cn/news/70727.html

相关文章:

  • Linux系统Docker部署开源在线协作笔记Trilium Notes与远程访问详细教程
  • 归并排序与逆序对问题(C语言版)
  • 网安基础知识|IDS入侵检测系统|IPS入侵防御系统|堡垒机|VPN|EDR|CC防御|云安全-VDC/VPC|安全服务
  • OpenCV相机标定与3D重建(3)校正鱼眼镜头畸变的函数calibrate()的使用
  • socket连接封装
  • element dialog 2层弹窗数据同步问题
  • 【项目开发】RESTful架构及RESTful API设计指南
  • dapp获取钱包地址,及签名
  • js.零钱兑换
  • python:用 sklearn 转换器处理数据
  • 【C++ 篇】类之华章:超越固有模式,品味面向对象的璀璨光芒
  • OSG开发笔记(三十一):OSG中LOD层次细节模型介绍和使用
  • MySQL数据库的备份与还原
  • 大模型论文精华—20241111
  • 贪心算法day05(k次取反后最大数组和 田径赛马)
  • 3.keeplived配置文件
  • VideoChat:开源的数字人实时对话系统,支持自定义数字人的形象和音色
  • 二维差分矩阵 模板题
  • 李佳琦回到巅峰背后,双11成直播电商分水岭
  • 链式结构二叉树
  • 【QT常用技术讲解】任务栏图标+socket网络服务+开机自启动
  • 项目管理平台盘点:2024推荐的9款优质工具
  • jmeter基础05_第1个http请求
  • 【论文速看】DL最新进展202411011-图像超分、Transformer
  • 分布式----Ceph部署(上)
  • 软件测试中的PIE模型