51单片机lcd矩阵键盘密码锁程序初始6位密码

时间: 2023-11-10 13:05:06 浏览: 68
以下是一个基于51单片机和LCD矩阵键盘的简单密码锁程序,初始密码为"123456": ``` #include <reg51.h> #define uchar unsigned char #define uint unsigned int sbit RS = P1^0; //LCD1602的接口定义 sbit RW = P1^1; sbit EN = P1^2; sbit k1 = P3^0; //矩阵键盘的接口定义 sbit k2 = P3^1; sbit k3 = P3^2; sbit k4 = P3^3; sbit k5 = P3^4; sbit k6 = P3^5; sbit k7 = P3^6; sbit k8 = P3^7; uchar password[6] = {'1', '2', '3', '4', '5', '6'}; //初始密码 uchar input[6]; //输入的密码 uchar i = 0; //计数器 void delayms(uint x) //延时函数 { uint i, j; for(i = x; i > 0; i--) for(j = 110; j > 0; j--); } void init() //LCD1602初始化函数 { delayms(15); RS = 0; RW = 0; EN = 1; P0 = 0x38; EN = 0; delayms(5); EN = 1; P0 = 0x38; EN = 0; delayms(5); EN = 1; P0 = 0x38; EN = 0; delayms(5); EN = 1; P0 = 0x0C; EN = 0; delayms(5); EN = 1; P0 = 0x06; EN = 0; delayms(5); EN = 1; P0 = 0x01; EN = 0; delayms(5); } void write_com(uchar com) //写命令函数 { RS = 0; RW = 0; EN = 1; P0 = com; EN = 0; delayms(5); } void write_data(uchar dat) //写数据函数 { RS = 1; RW = 0; EN = 1; P0 = dat; EN = 0; delayms(5); } void display(uchar *s) //显示函数 { while(*s != '\0') write_data(*s++); } void main() { init(); write_com(0x80); display("Enter Password:"); while(1) { if(k1 == 0) //按下1键 { input[i] = '1'; i++; delayms(10); while(k1 == 0); } if(k2 == 0) //按下2键 { input[i] = '2'; i++; delayms(10); while(k2 == 0); } if(k3 == 0) //按下3键 { input[i] = '3'; i++; delayms(10); while(k3 == 0); } if(k4 == 0) //按下4键 { input[i] = '4'; i++; delayms(10); while(k4 == 0); } if(k5 == 0) //按下5键 { input[i] = '5'; i++; delayms(10); while(k5 == 0); } if(k6 == 0) //按下6键 { input[i] = '6'; i++; delayms(10); while(k6 == 0); } if(k7 == 0) //按下7键(确认键) { if(i == 6) //输入了6位密码 { uchar j; for(j = 0; j < 6; j++) { if(input[j] != password[j]) //密码错误 { write_com(0xC0); display("Wrong Password!"); delayms(1000); write_com(0xC0); display("Enter Password:"); i = 0; break; } } if(j == 6) //密码正确 { write_com(0xC0); display("Password Correct"); delayms(1000); write_com(0xC0); display("Enter Password:"); i = 0; } } else //输入的密码不足6位 { write_com(0xC0); display("Input Incomplete"); delayms(1000); write_com(0xC0); display("Enter Password:"); i = 0; } while(k7 == 0); } if(k8 == 0) //按下8键(清除键) { write_com(0xC0); display("Enter Password:"); i = 0; while(k8 == 0); } if(i >= 6) //输入的密码超过6位 { write_com(0xC0); display("Password Too Long"); delayms(1000); write_com(0xC0); display("Enter Password:"); i = 0; } } } ``` 注意:此代码仅供参考,具体实现要根据实际情况进行调整和优化。

相关推荐

最新推荐

recommend-type

密码锁设计(4×4键盘及8位数码管显示)

用4×4组成0-9数字键及确认键构成的密码锁。 用8位数码管组成显示电路提示信息,当输入密码时,只显示“8.”,当密码位数输入完毕按下确认键时,对输入的密码与设定的密码进行比较,若密码正确,则门开,此处用LED...
recommend-type

单片机键盘程序(4×4矩阵式)

单片机键盘程序(4×4矩阵式)设计:用AT89S51的并行口P1接4×4矩阵键盘,以P1.0-P1.3作输入线,以P1.4-P1.7作输出线;在数码管上显示每个按键的“0-F”序号。
recommend-type

利用矩阵键盘控制51单片机LCD12864显示的数字时钟

利用51单片机内部定时器定时,产生时钟信号,矩阵键盘控制功能切换,LCD12864显示的数字时钟
recommend-type

基于FPGA的数字密码锁

自古以来人们对物品安全就十分重视,数字化的今天,电子锁正在逐步取代以往的机械锁被广泛运用在门禁、银行和保险柜。随着物联网技术的发展,人们对电子锁安全性和可靠性又提出了新的要求。本文所述的FPGA,即现场可...
recommend-type

51单片机驱动16×16LED点阵显示动画汉字汇编程序

这里提供一个完整的AT89S51单片机驱动驱动led点阵显示具有动画效果的汉字的汇编程序列子。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

深入了解MATLAB开根号的最新研究和应用:获取开根号领域的最新动态

![matlab开根号](https://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。