C51(矩阵键盘电子密码锁)
作者:互联网
代码段
#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,"Passwordid");
while(1)
{
KeyNum=MatrixKey();
if(KeyNum)
{
if(KeyNum<=10)//如果s1-s10按键按键按下,输入密码
{
if(count<4)//如果小于四
{
Password*=10;//相等于密码左移一位
Password=KeyNum%10;
count++;//几次加一
}
LCD_ShowNum(2,1,Password,4);
}
if(KeyNum==11)//如果s11按下,确认
{
if(Password==2345)
{
LCD_ShowString(1,14,"OK ");//显示OK
Password=0;//密码清零
count=0;//计次清零
LCD_ShowNum(2,1,Password,4);
}
else
{
LCD_ShowString(1,14,"eorr");
Password=0;
count=0;
LCD_ShowNum(2,1,Password,4);
}
}
if(KeyNum==12)//如果s12按下,取消
{
Password=0;//密码清零
count=0;//计次清零
LCD_ShowNum(2,1,Password,4);;//更新显示
}
}
}
}
矩阵键盘扫描
#include <REGX52.H>
#include"DELAY.H"
/*
矩阵键盘扫描
KeyNumber 按下按键读取键码1值
*/
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;
}
MatrixKey.h
#ifndef __MATRIXKEY_H__
#define __MATRIXKEY_H__
unsigned char MatrixKey();
#endif
标签:Delay,P1,20,C51,键盘,include,while,KeyNumber,密码锁 来源: https://blog.csdn.net/qq_56992543/article/details/119484809