利用E1区的4*4矩阵键盘和A3的lcd1602构成简单的按键输入显示系统,实现对矩阵的扫描,并利用lcd1602显示按下的按键的号
时间: 2024-02-24 12:56:49 浏览: 99
4*4矩阵键盘与1602液晶显示
4星 · 用户满意度95%
好的,以下是一个基于你提供的硬件的简单按键输入显示系统的程序示例:
```c
#include <reg51.h>
#define LCD_DB P0
sbit LCD_RS = P2^6;
sbit LCD_EN = P2^7;
unsigned char KeyTable[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
void Delay10ms() {
unsigned char i, j;
i = 25;
j = 214;
do {
while (--j);
} while (--i);
}
void LCD_WriteCmd(unsigned char cmd) {
LCD_RS = 0;
LCD_DB = cmd;
LCD_EN = 1;
Delay10ms();
LCD_EN = 0;
}
void LCD_WriteData(unsigned char dat) {
LCD_RS = 1;
LCD_DB = dat;
LCD_EN = 1;
Delay10ms();
LCD_EN = 0;
}
void LCD_Init() {
LCD_WriteCmd(0x38);
LCD_WriteCmd(0x0c);
LCD_WriteCmd(0x06);
LCD_WriteCmd(0x01);
}
unsigned char KeyScan() {
unsigned char i, j, key;
P1 = 0xf0;
if (P1 != 0xf0) {
Delay10ms();
if (P1 != 0xf0) {
key = P1;
P1 = 0x0f;
switch (key) {
case 0x70: i = 0; break;
case 0xb0: i = 1; break;
case 0xd0: i = 2; break;
case 0xe0: i = 3; break;
}
switch (P1) {
case 0x07: j = 0; break;
case 0x0b: j = 1; break;
case 0x0d: j = 2; break;
case 0x0e: j = 3; break;
}
return KeyTable[i][j];
}
}
return 0xff;
}
void main() {
unsigned char key;
LCD_Init();
while (1) {
key = KeyScan();
if (key != 0xff) {
LCD_WriteCmd(0x01);
LCD_WriteData(key);
}
}
}
```
这个程序会将4*4矩阵键盘的输入映射到KeyTable数组中,然后将相应的数字显示在LCD1602屏幕上。请注意,这个程序只能检测一个按键是否按下,如果同时按下多个按键,它只会检测到最后按下的那个按键。如果需要检测多个按键同时按下的情况,你需要编写更复杂的程序。
阅读全文