目录
一、LCD1602介绍
1、关于LCD
LCD ( Liquid Crystal Display 的简称)液晶显示器。能够同时显示16x2,32个字符,是一种专门用来显示字母、数字、符号等的点阵型液晶模块。LCD1602液晶显示器是广泛使用的一种字符型液晶显示模块。它是由字符型液晶显示屏(LCD)、控制驱动主电路HD44780及其扩展驱动电路HD44100,以及少量电阻、电容元件和结构件等装配在PCB板上而组成。该显示屏的优点是耗电量低、体积小、辐射低。相比于数码管,LCD屏幕除了能显示数字外,还能能显示字符,包括26个大小写字母和一些特殊符号。这大大提高了它的使用场合。
2、LCD1602的引脚
既然要和单片机通信,那么屏幕肯定和单片机进行连接,屏幕有哪些引脚呢?如下图所示。
3.LCD1602的使用步骤
(1).初始化屏幕
(2). 选择显存指令
(3). 写入数据
4.LCD1602写时序
二、代码
(1) main.c文件
#include "regx52.h"
#include "LCD1602.h"
#include "stdio.h"
#include "delay.h"
#include "uart.h"
sbit KEY3 = P3^2;
unsigned char rec_data = 0;
unsigned char buf[20]={0};
unsigned char rec_buf[6]={0}; //保存接受收到的数据
char rec_index = 0; //表示收到的数据放到数组的哪个位置
void main()
{
lcd1602_init();
if(KEY3 == 0)
{
Delay_ms(20);
uart_init(0xFA); //波特率9600
while(1)
{
if(KEY3 == 0)
{
//发送自己的名字和学号
printf("zhouyihui %d",123);
printf("20220320042 %d",123);
}
}
}
}
void uart_interrupt() interrupt 4
{
rec_data = SBUF; //取出数据
rec_buf[rec_index] = rec_data;
rec_index++;
//将接收到的数据通过lcd1602显示
if(rec_index > 5) //如果接受到6个字节
{
if(RI == 1)
{
//rec_buf = 0x12 0x34 0x32 0x11 0x67
char i = 0;
for(i=0;i<6;i++) //遍历数组,显示每个字节
{
sprintf(buf,"%02x",(unsigned int)rec_buf[i]);
lcd1602_show_string(i*2,0,buf);
}
rec_index = 0;
}
RI = 0; //清楚中断标记
}
}
(2) LCD1602.c文件
#include "lcd1602.h"
#include "regx52.h"
#include "delay.h"
void lcd1602_write_cmd(unsigned char cmd)
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_EN = 0;
LCD1602_DATAPORT = cmd;
LCD1602_EN = 1;
Delay_ms(1);
LCD1602_EN = 0;
Delay_ms(1);
}
void lcd1602_write_date(unsigned char dat)
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_EN = 0;
LCD1602_DATAPORT = dat;
LCD1602_EN = 1;
Delay_ms(1);
LCD1602_EN = 0;
Delay_ms(1);
}
void lcd1602_init()
{
lcd1602_write_cmd(0x38); //数据长度为8.2行显示,5x7点阵
//显示功能打开、无光标、光标不闪烁
lcd1602_write_cmd(0x0c);
//写入新数据后光标右移、文字保持不动
lcd1602_write_cmd(0x06);
lcd1602_write_cmd(0x01);
}
void lcd1602_show_string(unsigned char x,unsigned char y,unsigned char *str)
{
if(y == 0)
lcd1602_write_cmd(0x80 +0x00 + x);
if(y == 1)
lcd1602_write_cmd(0x80 + 0x40 + x);
while(*str != '\0')
{
lcd1602_write_date(*str);
str++;
}
}
(3) LCD1602.h文件
#ifndef _lcd1602_H
#define _lcd1602_H
#include "regx52.h"
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_EN = P2^7;
void lcd1602_show_string(unsigned char x,unsigned char y,unsigned char *str);
void lcd1602_init();
#define LCD1602_DATAPORT P0
#endif