基于51单片机利用矩阵按键实现密码锁 c语言
时间: 2023-10-03 14:08:13 浏览: 145
以下是基于51单片机利用矩阵按键实现密码锁的C语言代码,你可以参考一下:
```
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P2^6; // LCD1602液晶显示屏接口定义
sbit RW = P2^5;
sbit EN = P2^7;
sbit KEY_IN_1 = P1^0; // 矩阵按键接口定义
sbit KEY_IN_2 = P1^1;
sbit KEY_IN_3 = P1^2;
sbit KEY_IN_4 = P1^3;
sbit KEY_OUT_1 = P1^4;
sbit KEY_OUT_2 = P1^5;
sbit KEY_OUT_3 = P1^6;
sbit KEY_OUT_4 = P1^7;
uchar password[4] = {1, 2, 3, 4}; // 设置初始密码
uchar keyvalue = 0;
uchar keyflag = 0;
uchar input_pwd[4] = {0, 0, 0, 0};
uchar input_count = 0;
uchar error_count = 0;
void init_lcd1602() // LCD1602液晶显示屏初始化
{
delay(20);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x08);
delay(5);
write_com(0x01);
delay(5);
write_com(0x06);
delay(5);
write_com(0x0c);
delay(5);
}
void delay(uint x) // 延时函数
{
uint i, j;
for(i = x; i > 0; i--)
for(j = 110; j > 0; j--);
}
void write_com(uchar com) // 写指令函数
{
RS = 0;
RW = 0;
P0 = com;
EN = 1;
_nop_();
EN = 0;
}
void write_data(uchar dat) // 写数据函数
{
RS = 1;
RW = 0;
P0 = dat;
EN = 1;
_nop_();
EN = 0;
}
void keyscan() // 矩阵按键扫描函数
{
KEY_OUT_1 = 0;
if(KEY_IN_1 == 0)
{
keyvalue = 1;
keyflag = 1;
}
if(KEY_IN_2 == 0)
{
keyvalue = 2;
keyflag = 1;
}
if(KEY_IN_3 == 0)
{
keyvalue = 3;
keyflag = 1;
}
if(KEY_IN_4 == 0)
{
keyvalue = 4;
keyflag = 1;
}
KEY_OUT_1 = 1;
KEY_OUT_2 = 0;
if(KEY_IN_1 == 0)
{
keyvalue = 5;
keyflag = 1;
}
if(KEY_IN_2 == 0)
{
keyvalue = 6;
keyflag = 1;
}
if(KEY_IN_3 == 0)
{
keyvalue = 7;
keyflag = 1;
}
if(KEY_IN_4 == 0)
{
keyvalue = 8;
keyflag = 1;
}
KEY_OUT_2 = 1;
KEY_OUT_3 = 0;
if(KEY_IN_1 == 0)
{
keyvalue = 9;
keyflag = 1;
}
if(KEY_IN_2 == 0)
{
keyvalue = 10;
keyflag = 1;
}
if(KEY_IN_3 == 0)
{
keyvalue = 11;
keyflag = 1;
}
if(KEY_IN_4 == 0)
{
keyvalue = 12;
keyflag = 1;
}
KEY_OUT_3 = 1;
KEY_OUT_4 = 0;
if(KEY_IN_1 == 0)
{
keyvalue = 13;
keyflag = 1;
}
if(KEY_IN_2 == 0)
{
keyvalue = 14;
keyflag = 1;
}
if(KEY_IN_3 == 0)
{
keyvalue = 15;
keyflag = 1;
}
if(KEY_IN_4 == 0)
{
keyvalue = 16;
keyflag = 1;
}
KEY_OUT_4 = 1;
}
void display_pwd() // 显示密码函数
{
uchar i;
write_com(0x80);
write_data('I');
write_data('n');
write_data('p');
write_data('u');
write_data('t');
write_data(':');
for(i = 0; i < 4; i++)
{
write_data(input_pwd[i] + '0');
}
}
void main()
{
init_lcd1602(); // 初始化LCD1602液晶显示屏
while(1)
{
keyscan(); // 扫描矩阵按键
if(keyflag == 1 && input_count < 4) // 如果有按键按下且密码未输入完整
{
input_pwd[input_count++] = keyvalue; // 记录输入的密码
display_pwd(); // 显示输入的密码
keyflag = 0;
}
if(input_count == 4) // 如果密码输入完整
{
if(input_pwd[0] == password[0] && input_pwd[1] == password[1] && input_pwd[2] == password[2] && input_pwd[3] == password[3]) // 如果密码正确,开锁
{
write_com(0xc0);
write_data('W');
write_data('e');
write_data('l');
write_data('c');
write_data('o');
write_data('m');
write_data('e');
delay(1000);
input_count = 0;
error_count = 0;
write_com(0x01);
}
else // 如果密码错误
{
error_count++; // 记录错误次数
if(error_count >= 3) // 如果错误次数超过3次,锁定系统
{
write_com(0x01);
write_com(0x80);
write_data('S');
write_data('y');
write_data('s');
write_data('t');
write_data('e');
write_data('m');
write_data(' ');
write_data('L');
write_data('o');
write_data('c');
write_data('k');
delay(5000);
error_count = 0;
write_com(0x01);
}
else // 如果错误次数未超过3次,显示错误信息
{
write_com(0xc0);
write_data('E');
write_data('r');
write_data('r');
write_data('o');
write_data('r');
write_data('!');
delay(1000);
input_count = 0;
write_com(0x01);
}
}
}
}
}
```
以上代码仅供参考,实际应用中可能还需要根据具体情况进行修改和完善。
阅读全文