51单片机lcd1602利用e1的4*4矩阵键盘显示数字
时间: 2023-12-03 19:02:00 浏览: 138
首先,你需要连接51单片机和LCD1602显示屏。然后,连接4*4矩阵键盘到51单片机的E1引脚。
接下来,你需要编写51单片机的程序,使其能够读取4*4矩阵键盘的输入,并将数字显示在LCD1602屏幕上。以下是一个简单的示例程序:
```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屏幕上。你可以根据自己的需要修改程序来实现更复杂的功能。
阅读全文