51单片机利用e1的4*4矩阵键盘和a3区的lcd1602构成简单的按键输入系统,实现对矩阵键盘的扫描,并利用lcd1602显示按下的按键的号。16个按键的键号分别对应16个数值:0~15。的程序
时间: 2024-02-28 08:57:39 浏览: 52
好的,以下是51单片机利用e1的4*4矩阵键盘和a3区的lcd1602构成简单的按键输入系统,实现对矩阵键盘的扫描,并利用lcd1602显示按下的按键的号。16个按键的键号分别对应16个数值:0~15。的程序:
```
#include <reg51.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_E = P2^7;
sbit KBD_ROW0 = P1^0;
sbit KBD_ROW1 = P1^1;
sbit KBD_ROW2 = P1^2;
sbit KBD_ROW3 = P1^3;
sbit KBD_COL0 = P1^4;
sbit KBD_COL1 = P1^5;
sbit KBD_COL2 = P1^6;
sbit KBD_COL3 = P1^7;
void delay(unsigned int i)
{
while(i--);
}
void write_lcd1602_cmd(unsigned char cmd)
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_E = 1;
LCD1602_DB = cmd;
delay(100);
LCD1602_E = 0;
}
void write_lcd1602_dat(unsigned char dat)
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_E = 1;
LCD1602_DB = dat;
delay(100);
LCD1602_E = 0;
}
void init_lcd1602()
{
write_lcd1602_cmd(0x38);
write_lcd1602_cmd(0x0c);
write_lcd1602_cmd(0x06);
write_lcd1602_cmd(0x01);
}
void kbd_scan()
{
unsigned char key = 0xff;
KBD_ROW0 = 0;
KBD_ROW1 = 1;
KBD_ROW2 = 1;
KBD_ROW3 = 1;
if(KBD_COL0 == 0) key = 0;
if(KBD_COL1 == 0) key = 1;
if(KBD_COL2 == 0) key = 2;
if(KBD_COL3 == 0) key = 3;
KBD_ROW0 = 1;
KBD_ROW1 = 0;
KBD_ROW2 = 1;
KBD_ROW3 = 1;
if(KBD_COL0 == 0) key = 4;
if(KBD_COL1 == 0) key = 5;
if(KBD_COL2 == 0) key = 6;
if(KBD_COL3 == 0) key = 7;
KBD_ROW0 = 1;
KBD_ROW1 = 1;
KBD_ROW2 = 0;
KBD_ROW3 = 1;
if(KBD_COL0 == 0) key = 8;
if(KBD_COL1 == 0) key = 9;
if(KBD_COL2 == 0) key = 10;
if(KBD_COL3 == 0) key = 11;
KBD_ROW0 = 1;
KBD_ROW1 = 1;
KBD_ROW2 = 1;
KBD_ROW3 = 0;
if(KBD_COL0 == 0) key = 12;
if(KBD_COL1 == 0) key = 13;
if(KBD_COL2 == 0) key = 14;
if(KBD_COL3 == 0) key = 15;
if(key != 0xff)
{
write_lcd1602_cmd(0x80);
write_lcd1602_dat(key/10+'0');
write_lcd1602_dat(key%10+'0');
}
}
void main()
{
init_lcd1602();
while(1)
{
kbd_scan();
}
}
```
这个程序的作用是对矩阵键盘进行扫描,并利用lcd1602显示按下的按键的号。程序首先对lcd1602进行初始化,然后进入一个无限循环,在循环里调用kbd_scan函数对矩阵键盘进行扫描,并将按下的按键的号码显示在lcd1602上。具体的矩阵键盘扫描过程在kbd_scan函数中实现。
阅读全文