Display control of character liquid crystal display LCD 1602 (Keil+Proteus)

 Preface

I took the opportunity to complete the experiment on LCD 1602. There were a few parts of the circuit diagram that I didn’t understand, but no error was reported after removing them, so I left it alone.

LCD1602_Baidu Encyclopedia (baidu.com)icon-default.png?t=N7T8https://baike.baidu.com/item/LCD1602/6014393?fr=ge_ala

The LCD1602 liquid crystal display uses voltage to change the arrangement of internal molecules of the liquid crystal material filled between two parallel plates, so as to achieve the purpose of shading and transmitting light to display images of different shades and staggered patterns. And as long as a three-color filter layer is added between the two flat plates, the display can be realized.

Liquid crystal is a substance with flow characteristics, so only a small amount of external force is needed to make the liquid crystal molecules move. Taking the most common nematic liquid crystal as an example, liquid crystal molecules can easily be turned by the action of an electric field. Since the optical axis of the liquid crystal is quite consistent with its molecular axis, optical effects can be produced. When the electric field applied to the liquid crystal is removed and disappears, the liquid crystal will rely on its own elasticity and viscosity, and the liquid crystal molecules will very quickly return to the state before the electric field was applied.

"16" means that the LCD module can display 16 characters per line, and "02" means that it can display 2 lines.

For basic operations, refer to the article below:

Static display and dynamic display of LED digital tube (Keil+Proteus)-CSDN Blogicon-default.png?t=N7T8https://blog.csdn.net/weixin_64066303/article/details/134101256?spm =1001.2014.3001.5501

Keil

It should be noted that Sbit RS=P2^2;

The symbol "^" is preceded by the name of the special function register, and the number after "^" defines the position of the addressable bit in the special function register.

Command words of LCD 1602

Order RS R/~W D7 D6 D5 D4 D3 D2 D1 D0
clear screen 0 0 0 0 0 0 0 0 0 0
Cursor returns 0 0 0 0 0 0 0 0 0 X
Display mode settings 0 0 0 0 0 0 0 1 I/D S
Display on/off and cursor settings 0 0 0 0 0 0 1 D C B
Cursor or character shift 0 0 0 0 0 1 S/C R/L X X
Function settings 0 0 0 0 1 DL N F X X
CGROM address setting 0 0 0 1 Character library ROM address
DDRAM address setting 0 0 1 Display data RAM address
Read busy flag or address 0 1 BF Counter address
write data 1 0 data to write
Read data 1 1 read data

The read and write operations of LCD 1602 are specified as follows:

The control signal sent by the microcontroller to the LCD 1602 Output of LCD 1602
read status RS=0,R/~W=1,E=1 D0~D7=status word
write command RS=0, R/~W=0, D0~D7=command, E=positive pulse none
Read data RS=1,R/~W=1,E=1 D0~D7=data
write data RS=1, R/~W=0, D0~D7=data, E=positive pulse none

 I have canceled the cursor right movement here and printed it directly.

The address of the first row of characters is 80H~8FH.

The addresses of the second row of characters are C0H~CFH.

#include<reg51.h>
#include<intrins.h>	//包含_nop_()空函数指令的头文件
#define uchar unsigned char 
#define uint unsigned int
#define out P0
sbit RS=P2^0;//位变量
sbit RW=P2^1;//位变量
sbit E=P2^2;//位变量
//函数声明部分
void lcd_initial(void);//LCD初始化函数
void check_busy(void);//检查忙标志位函数
void write_command(uchar com);//写命令函数
void write_data(uchar dat);//写数据函数
void string(uchar ad,uchar *s);//显示字符串
void delay(uint);//延时

void main(void){
	lcd_initial();//对LCD初始化
	while(1){
		string(0x83,"I LOVE YOU");//显示第一行的字符
		string(0xC4,"FOREVER");//显示第二行的字符
		delay(100);//延时
		write_command(0x01);//清屏
		delay(100);//延时
	}
}

//延时
void delay(uint j){
	uchar i=250;
	for(;j>0;j--){
		while(--i);
		i=249;
		while(--i);
		i=250;
	}
}

//检查忙标志
void check_busy(void){
	uchar dt;
	do{
		dt=0xff;//dt为变量单元,初值为0xff
		//RS=0,E=1时才可以读忙标志位
		E=0;
		RS=0;
		RW=1;
		E=1;
		dt=out;//out为P0口,P0口的状态送入dt中
	}while(dt&0x80);//如果忙标志位BF=1,继续循环检测,等待BF=0
	E=0;//BF=0,LCD 1602不忙,结束检测
}

//写命令
void write_command(uchar com){
	check_busy();
	//按规定RS和E同时为0时,才可以写命令
	E=0;
	RS=0;
	RW=0;
	out=com;//将命令com写入P0口
	E=1;//写命令时,E应为正脉冲,即正跳变,所以前面先置E=0
	_nop_();//空操作1个机器周期,等待硬件反应
	E=0;//E由高电平变为低电平,LCD 1602开始执行命令
	delay(1);//延时,等待硬件反应
}

//写数据
void write_data(uchar dat){
	check_busy();//检测忙标志位BF=1则等待,若BF=0,则可对LCD 1602写入命令
	E=0;//按规定写数据时,E应为正脉冲,所以先置E=0
	//按规定RS=1和RW=0时,才可以写入数据
	RS=1;
	RW=0;
	out=dat;//将数据”dat“从P0口输出,即写入LCD 1602
	E=1;//E产生正跳变
	_nop_();//空操作1个机器周期,等待硬件反应
	E=0;//E由高电平变为低电平,写数据操作结束
	delay(1);
}

//液晶显示器初始化函数
void lcd_initial(void){
	write_command(0x38);//8位两行显示,5*7点阵字符
	_nop_();//空操作1个机器周期,等待硬件反应
	write_command(0x0C);//开整体显示,光标关,无闪烁
	_nop_();//空操作1个机器周期,等待硬件反应
	//write_command(0x05);//光标右移
	_nop_();//空操作1个机器周期,等待硬件反应
	write_command(0x01);//清屏
	delay(1);
}
//输出显示字符串
void string(uchar ad,uchar *s){
	write_command(ad);
	while(*s>0){
		write_data(*s++);//输出字符串,且指针增1
		delay(100);
	}
}

Proteus

Required components

Component name Proteus keyword
51 microcontroller AT89C51
reset button BUTTON
capacitance CAP
electrolytic capacitor CAP-ELEC
crystal oscillator CRYSTAL
Character LCD 1602 display LM016L
sliding rheostat POT-HG
resistance RES
exclusion RESPACK-8

 operation result

Reference link

Proteus-51 single-chip microcomputer-LCD1602 liquid crystal display DS1302 real-time clock_51 single-chip microcomputer lcd1602 display time_Falling in love with circuit design blog-CSDN blogicon-default.png?t=N7T8https://blog.csdn.net /liht_1634/article/details/1315240161. 51 MCU uses Proteus to master the use of LCD1602 display screen (simulation and code)_51 MCU 1602 displays Chinese characters - CSDN Blogicon-default.png?t=N7T8https://blog.csdn.net/m0_47235364/article/details/128294254STC89C51 Basics and Project Day 10: LCD display characters (non-standard protocol peripherals) - CSDN Blogicon-default.png?t=N7T8https://blog.csdn.net/as480133937/article/details/113148712[Selected][Commonly used sensors] Detailed explanation of the working principle and routine code of LCD1602 liquid crystal_lcd1602 working principle- CSDN Bloghttps://blog.csdn.net/zitech/article/details/9200203[89C51 MCU]LCD1602 static display-CSDN Blogicon-default.png?t=N7T8https://blog.csdn.net/Jaci133/article/details/133429036icon-default.png?t=N7T8

Summarize

Basically, I just followed the book, and there are no skills. I simply recorded it.

Guess you like

Origin blog.csdn.net/weixin_64066303/article/details/134224776