当前位置:网站首页>矩阵键盘
矩阵键盘
2022-07-30 05:46:00 【缘聚654】
这次我们来了解矩阵键盘

可以看到矩阵键盘和独立键盘是大同小异的,矩阵键盘就是由四个独立键盘组成的4*4的矩阵,才用共阴极共阳极的工作原理。所以控制其工作的代码也与独立键盘类似
#include <REGX52.H>
#include "Delay.h"
unsigned char MatrixKey()
{
unsigned char KeyNumber=0;
P1=0xFF;
P1_3=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}
P1=0xFF;
P1_2=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}
P1=0xFF;
P1_1=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}
P1=0xFF;
P1_0=0;
if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}
if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}
if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}
if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}
return KeyNumber;
}再结合前面所介绍的lcd1602模块可以实现按键控制显示对应的数字以及键盘密码锁的功能
按键显示数字的主程序
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
#include "MatrixKey.h"
unsigned char KeyNum;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"hello word");
while(1)
{
KeyNum=MatrixKey();
if(KeyNum)
{
LCD_ShowNum(2,1,KeyNum,2);
}
}
}使用了模块化编程,在引入了头文件后可以直接使用其相应的功能函数
再按下第6号按键时在lcd1602上显示06

实现矩阵键盘密码锁功能的代码
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
#include "MatrixKey.h"
unsigned char KeyNum;
unsigned int Password,Count;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"Password");
while(1)
{
KeyNum=MatrixKey();
if(KeyNum)
{
if(KeyNum<=10)
{
if(Count<4)
{
Password=Password*10;
Password=Password+KeyNum%10;
Count++;
}
LCD_ShowNum(2,1,Password,4);
}
if(KeyNum==11)
{
if(Password==2345)
{
LCD_ShowString(1,14," OK");
Password=Count=0;
LCD_ShowNum(2,1,Password,4);
}
else
{
LCD_ShowString(1,14,"ERR");
Password=Count=0;
LCD_ShowNum(2,1,Password,4);
}
}
if(KeyNum==12)
{
Password=Password/10;
Count--;
LCD_ShowNum(2,1,Password,4);
}
}
}
}设置从1号键到10号键代表数字1-0,11号键表示确认,12号为清除。
密码初始为2345,正确显示0k,错误显示ERR。
WeChat_20220727173208
边栏推荐
- About map custom sorting of keys
- 使用Dva项目作Antd的Demo
- Anaconda 安装低版本tensorflow
- Insertion Sort in Classic Sort
- 【速成MSP430f149】电赛期间学习MSP430f149笔记
- 2020-09-03 Solve the very slow installation of pip install [Errno 101] Network unreachable problem
- TCP为什么要三次握手,握手过程中丢包会怎么样?
- QT weekly skills (3)~~~~~~~~~ serial port addition
- clinit方法
- Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
猜你喜欢

Antd简单启动一个企业级项目

led闪烁

SSH-RSA密钥

TCP为什么要三次握手,握手过程中丢包会怎么样?

"R Language + Remote Sensing" Comprehensive Evaluation Method of Water Environment

QT serial 4: LORA test platform based on QT and STM32H750 (3)

迷宫问题----经典回溯法解决

FPGA parsing B code----serial 1

华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长

QT serial port dynamically displays a large number of data waveform curves in real time (5) ======== "Final perfect solution version"
随机推荐
sizeof和strlen最全区别,以及指针和数组运算解析
超详细的PCB高可靠辨别方法
FPGA parsing B code----serial 2
(*(void (*)())0)()的解读
2020-09-03解决pip install安装非常慢[Errno 101] 网络不可达问题
Vim查找字符
Duplicate keys detected:‘/da…‘
闭包(你不知道的JS)
The IEEE under the specified journal search related papers
xftp的简单使用
"R Language + Remote Sensing" Comprehensive Evaluation Method of Water Environment
如何判断 PCB 板是否变形?
Antd 树拖拽一些细节,官网没有,摸坑篇
Antd简单启动一个企业级项目
快速排序学习记录
动态规划进阶 JS
TCP建立连接的过程
jvm之逃逸分析
Explore the efficiency of make_shared
vs编译boost库脚本