c51中代码要求可以用1602液晶屏的显示驱动,矩阵键盘输入液晶屏显示输出 ,串口下位机发送数据 ,串口下位机接收数据代码
时间: 2023-12-03 19:42:49 浏览: 75
C51操作1602液晶显示的代码
以下是一个简单的示例代码,用于在C51中使用1602液晶屏进行显示,同时通过矩阵键盘进行输入,并通过串口与下位机进行数据通信:
```
#include <reg52.h>
#include <stdio.h>
#define LCD_DATABUS P0
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
sbit KEYPAD_ROW1 = P1^0;
sbit KEYPAD_ROW2 = P1^1;
sbit KEYPAD_ROW3 = P1^2;
sbit KEYPAD_ROW4 = P1^3;
sbit KEYPAD_COL1 = P1^4;
sbit KEYPAD_COL2 = P1^5;
sbit KEYPAD_COL3 = P1^6;
sbit KEYPAD_COL4 = P1^7;
sbit UART_TX = P3^1;
sbit UART_RX = P3^0;
void InitLCD(void);
void SendCommand(unsigned char cmd);
void SendData(unsigned char dat);
void DisplayString(unsigned char x, unsigned char y, unsigned char *str);
void DelayUs2x(unsigned char t);
void DelayMs(unsigned char t);
unsigned char KeypadScan(void);
void InitUART(void);
void SendByte(unsigned char dat);
unsigned char ReceiveByte(void);
void main(void)
{
unsigned char keypad_val = 0;
unsigned char uart_rx_data = 0;
unsigned char str[16] = {0};
InitLCD();
InitUART();
DisplayString(0, 0, "Keypad:");
DisplayString(0, 1, "UART:");
while (1)
{
keypad_val = KeypadScan();
if (keypad_val != 0)
{
sprintf(str, "%d", keypad_val);
DisplayString(8, 0, str);
}
if (RI)
{
uart_rx_data = ReceiveByte();
sprintf(str, "%d", uart_rx_data);
DisplayString(5, 1, str);
}
}
}
void InitLCD(void)
{
SendCommand(0x38); // function set: 8-bit, 2-line, 5x8 dots
SendCommand(0x0c); // display on, cursor off, blink off
SendCommand(0x06); // entry mode set: increment, no shift
SendCommand(0x01); // clear display
DelayMs(2); // wait for clear display command to finish
}
void SendCommand(unsigned char cmd)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 1;
LCD_DATABUS = cmd;
DelayUs2x(10);
LCD_EN = 0;
}
void SendData(unsigned char dat)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 1;
LCD_DATABUS = dat;
DelayUs2x(10);
LCD_EN = 0;
}
void DisplayString(unsigned char x, unsigned char y, unsigned char *str)
{
unsigned char addr = 0;
if (y == 0)
{
addr = 0x80 + x;
}
else if (y == 1)
{
addr = 0xc0 + x;
}
SendCommand(addr);
while (*str != '\0')
{
SendData(*str++);
}
}
void DelayUs2x(unsigned char t)
{
while (t--)
{
_nop_();
_nop_();
}
}
void DelayMs(unsigned char t)
{
while (t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
unsigned char KeypadScan(void)
{
unsigned char row = 0;
unsigned char col = 0;
unsigned char keypad_val = 0;
KEYPAD_ROW1 = 0;
KEYPAD_ROW2 = 1;
KEYPAD_ROW3 = 1;
KEYPAD_ROW4 = 1;
DelayUs2x(5);
col = KEYPAD_COL1 & KEYPAD_COL2 & KEYPAD_COL3 & KEYPAD_COL4;
if (!col)
{
row = 1;
}
KEYPAD_ROW1 = 1;
KEYPAD_ROW2 = 0;
KEYPAD_ROW3 = 1;
KEYPAD_ROW4 = 1;
DelayUs2x(5);
col = KEYPAD_COL1 & KEYPAD_COL2 & KEYPAD_COL3 & KEYPAD_COL4;
if (!col)
{
row = 2;
}
KEYPAD_ROW1 = 1;
KEYPAD_ROW2 = 1;
KEYPAD_ROW3 = 0;
KEYPAD_ROW4 = 1;
DelayUs2x(5);
col = KEYPAD_COL1 & KEYPAD_COL2 & KEYPAD_COL3 & KEYPAD_COL4;
if (!col)
{
row = 3;
}
KEYPAD_ROW1 = 1;
KEYPAD_ROW2 = 1;
KEYPAD_ROW3 = 1;
KEYPAD_ROW4 = 0;
DelayUs2x(5);
col = KEYPAD_COL1 & KEYPAD_COL2 & KEYPAD_COL3 & KEYPAD_COL4;
if (!col)
{
row = 4;
}
if (col == 0x08)
{
keypad_val = row * 3;
}
else if (col == 0x04)
{
keypad_val = row * 3 + 1;
}
else if (col == 0x02)
{
keypad_val = row * 3 + 2;
}
else
{
keypad_val = 0;
}
return keypad_val;
}
void InitUART(void)
{
TMOD = 0x20; // timer1 mode 2: 8-bit auto-reload
TH1 = 0xfd; // 9600 baud rate
TL1 = 0xfd;
TR1 = 1; // start timer1
TI = 1; // enable transmit interrupt
ES = 1; // enable serial interrupt
}
void SendByte(unsigned char dat)
{
SBUF = dat;
while (!TI);
TI = 0;
}
unsigned char ReceiveByte(void)
{
unsigned char uart_rx_data = 0;
while (!RI);
uart_rx_data = SBUF;
RI = 0;
return uart_rx_data;
}
void UARTInterrupt(void) interrupt 4
{
if (RI)
{
RI = 0;
}
if (TI)
{
TI = 0;
}
}
```
在这个代码中,使用了1602液晶屏进行显示,使用矩阵键盘进行输入。同时,使用了串口与下位机进行通信。具体来说:
- `InitLCD`函数用于初始化液晶屏。
- `SendCommand`函数用于发送命令到液晶屏。
- `SendData`函数用于发送数据到液晶屏。
- `DisplayString`函数用于在液晶屏上显示字符串。
- `DelayUs2x`和`DelayMs`函数用于延时,以等待液晶屏响应。
- `KeypadScan`函数用于扫描矩阵键盘,并返回按键值。
- `InitUART`函数用于初始化串口。
- `SendByte`函数用于向下位机发送一个字节的数据。
- `ReceiveByte`函数用于从下位机接收一个字节的数据。
- `UARTInterrupt`是串口中断服务函数,在接收到数据时触发。
在主函数中,首先调用`InitLCD`和`InitUART`函数进行初始化。然后在循环中不断扫描矩阵键盘和串口,如果有按键按下或者接收到数据,就在液晶屏上显示相应的内容。
阅读全文