写一个51单片机4*4矩阵控制lcd显示程序,要求每一个按键对应一个数字
时间: 2024-03-19 20:43:52 浏览: 45
C51单片机密码锁(4×4矩阵键盘+LCD1602液晶显示屏)
5星 · 资源好评率100%
好的,以下是51单片机4*4矩阵控制LCD显示程序,请参考:
```c
#include <reg51.h>
//定义端口
#define LCD_DB P0
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
//定义矩阵键盘端口
sbit Row1 = P1^0;
sbit Row2 = P1^1;
sbit Row3 = P1^2;
sbit Row4 = P1^3;
sbit Col1 = P1^4;
sbit Col2 = P1^5;
sbit Col3 = P1^6;
sbit Col4 = P1^7;
//函数声明
void delay(unsigned int i);
void LCD_Init(void);
void LCD_Write_Cmd(unsigned char cmd);
void LCD_Write_Data(unsigned char dat);
void LCD_Write_String(unsigned char x, unsigned char y, unsigned char *s);
unsigned char Key_Scan(void);
unsigned char Key_Get(void);
void main()
{
unsigned char key, num;
unsigned char str[4] = {0};
LCD_Init();
while(1)
{
key = Key_Get();
if(key != 0)
{
num = key - '0';
str[3] = num % 10 + '0';
num /= 10;
str[2] = num % 10 + '0';
num /= 10;
str[1] = num % 10 + '0';
num /= 10;
str[0] = num + '0';
LCD_Write_String(0, 0, str);
}
}
}
//延时函数
void delay(unsigned int i)
{
unsigned int j, k;
for(j = i; j > 0; j--)
for(k = 125; k > 0; k--);
}
//LCD初始化函数
void LCD_Init(void)
{
LCD_Write_Cmd(0x38);
LCD_Write_Cmd(0x0c);
LCD_Write_Cmd(0x06);
LCD_Write_Cmd(0x01);
delay(10);
}
//LCD写命令函数
void LCD_Write_Cmd(unsigned char cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
LCD_DB = cmd;
delay(10);
LCD_EN = 1;
delay(10);
LCD_EN = 0;
}
//LCD写数据函数
void LCD_Write_Data(unsigned char dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
LCD_DB = dat;
delay(10);
LCD_EN = 1;
delay(10);
LCD_EN = 0;
}
//LCD写字符串函数
void LCD_Write_String(unsigned char x, unsigned char y, unsigned char *s)
{
unsigned char i;
if(x < 16)
{
if(y == 0)
LCD_Write_Cmd(0x80 + x);
else
LCD_Write_Cmd(0xc0 + x);
}
for(i = 0; s[i] != '\0'; i++)
LCD_Write_Data(s[i]);
}
//矩阵键盘扫描函数
unsigned char Key_Scan(void)
{
unsigned char key_value = 0;
Row1 = 0, Row2 = 1, Row3 = 1, Row4 = 1;
if(Col1 == 0)
{
delay(10);
if(Col1 == 0)
{
key_value = '1';
while(!Col1);
}
}
if(Col2 == 0)
{
delay(10);
if(Col2 == 0)
{
key_value = '2';
while(!Col2);
}
}
if(Col3 == 0)
{
delay(10);
if(Col3 == 0)
{
key_value = '3';
while(!Col3);
}
}
if(Col4 == 0)
{
delay(10);
if(Col4 == 0)
{
key_value = 'A';
while(!Col4);
}
}
Row1 = 1, Row2 = 0, Row3 = 1, Row4 = 1;
if(Col1 == 0)
{
delay(10);
if(Col1 == 0)
{
key_value = '4';
while(!Col1);
}
}
if(Col2 == 0)
{
delay(10);
if(Col2 == 0)
{
key_value = '5';
while(!Col2);
}
}
if(Col3 == 0)
{
delay(10);
if(Col3 == 0)
{
key_value = '6';
while(!Col3);
}
}
if(Col4 == 0)
{
delay(10);
if(Col4 == 0)
{
key_value = 'B';
while(!Col4);
}
}
Row1 = 1, Row2 = 1, Row3 = 0, Row4 = 1;
if(Col1 == 0)
{
delay(10);
if(Col1 == 0)
{
key_value = '7';
while(!Col1);
}
}
if(Col2 == 0)
{
delay(10);
if(Col2 == 0)
{
key_value = '8';
while(!Col2);
}
}
if(Col3 == 0)
{
delay(10);
if(Col3 == 0)
{
key_value = '9';
while(!Col3);
}
}
if(Col4 == 0)
{
delay(10);
if(Col4 == 0)
{
key_value = 'C';
while(!Col4);
}
}
Row1 = 1, Row2 = 1, Row3 = 1, Row4 = 0;
if(Col1 == 0)
{
delay(10);
if(Col1 == 0)
{
key_value = '*';
while(!Col1);
}
}
if(Col2 == 0)
{
delay(10);
if(Col2 == 0)
{
key_value = '0';
while(!Col2);
}
}
if(Col3 == 0)
{
delay(10);
if(Col3 == 0)
{
key_value = '#';
while(!Col3);
}
}
if(Col4 == 0)
{
delay(10);
if(Col4 == 0)
{
key_value = 'D';
while(!Col4);
}
}
return key_value;
}
//获取矩阵键盘按键值
unsigned char Key_Get(void)
{
unsigned char key = 0;
while(1)
{
key = Key_Scan();
if(key != 0)
return key;
}
}
```
这个程序使用了4*4矩阵键盘,每个按键对应一个数字。当按下一个按键时,LCD屏幕会显示对应的数字。程序中的Key_Scan函数用来扫描矩阵键盘,Key_Get函数用来获取按下的键值。LCD_Write_Cmd用来写LCD命令,LCD_Write_Data用来写LCD数据,LCD_Write_String用来写字符串。请注意,程序中的端口定义可能需要根据具体硬件进行修改。
阅读全文