10_矩阵键盘
最编程
2024-04-13 22:54:13
...
矩阵键盘
矩阵键盘介绍
扫描的概念
矩阵按键原理图
按按键显示对应数字
MatrixKey.c
#include "Delay.h"
#include <REGX52.H>
unsigned char MatrixKey()
{
unsigned char KeyNumber=0;
P1=0xFF;
P1_7=0;
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=1;}
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=2;}
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=3;}
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=4;}
P1=0xFF;
P1_6=0;
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=5;}
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=6;}
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=7;}
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=8;}
P1=0xFF;
P1_5=0;
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=9;}
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=10;}
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=11;}
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=12;}
P1=0xFF;
P1_4=0;
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=13;}
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=14;}
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=15;}
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=16;}
return KeyNumber;
}
MatrixKey.h
#ifndef __MATRIXKEY_H__
#define __MATRIXKEY_H__
unsigned char MatrixKey();
#endif
main.c
#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#inlcude "MatrixKey.h"
unsigned char KeyNum=0;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"MetrixKey:");
while(1)
{
KeyNum=MatrixKey();
if(KeyNum)
{
LCD_ShowNum(2,1,KeyNum,2);
}
}
}
运行效果
矩阵键盘密码锁
main.c
#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#inlcude "MatrixKey.h"
unsigned char KeyNum;
unsigned int Password,Count;
void main()
{
LCD_Init();
LCD_ShowString(1,1,"Password:");
LCD_ShowNum(2,1,0000,4);
while(1)
{
KeyNum=MatrixKey();
if(KeyNum)
{
if(KeyNum<=10 && Count<4) //如果S1~S10按键按下,输入密码
{
Password*=10;
Password+=KeyNum%10;
Count++;
LCD_ShowNum(2,1,Password,4);
}
if(KeyNum==11) //确认按钮
{
if(Password==1234)
{
LCD_ShowString(1,14,"ok ");
}
else
{
LCD_ShowString(1,14,"err");
}
Password=0;
Count=0;
LCD_ShowNum(2,1,Password,4);
}
if(KeyNum==12) //取消按钮
{
Password=0;
Count=0;
LCD_ShowNum(2,1,Password,4);
LCD_ShowString(1,14," ");
}
}
}
}
MatrixKey.c
#include "Delay.h"
#include <REGX52.H>
unsigned char MatrixKey()
{
unsigned char KeyNumber=0;
P1=0xFF;
P1_7=0;
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=1;}
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=2;}
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=3;}
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=4;}
P1=0xFF;
P1_6=0;
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=5;}
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=6;}
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=7;}
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=8;}
P1=0xFF;
P1_5=0;
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=9;}
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=10;}
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=11;}
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=12;}
P1=0xFF;
P1_4=0;
if(P1_3==0){Delay(20);while(P1_3==0);Delay(20);KeyNumber=13;}
if(P1_2==0){Delay(20);while(P1_2==0);Delay(20);KeyNumber=14;}
if(P1_1==0){Delay(20);while(P1_1==0);Delay(20);KeyNumber=15;}
if(P1_0==0){Delay(20);while(P1_0==0);Delay(20);KeyNumber=16;}
return KeyNumber;
}
MatrixKey.h
#ifndef __MATRIXKEY_H__
#define __MATRIXKEY_H__
unsigned char MatrixKey();
#endif
显示效果
![VID_20231108_193310 (2)](H:\通讯软件\QQSave\1607442148\FileRecv\MobileFile\VID_20231108_193310 (2).gif)