51 microcontroller controls the 1602LCD display to output two lines of text and one

51 microcontroller controls the 1602LCD display to output two lines of text and one

1 Overview

This article introduces the basic knowledge of the 1602 model display screen and the use of a microcontroller to control it to output two lines of content.

2.1602 Basic knowledge

1602 LCD module is a general industrial LCD module, a dot matrix LCD module specially used to display letters, numbers, symbols, etc. As can be seen from the name, the LCD display module has 2 lines, each line has 16 characters and can display a total of 32 characters. Each character is composed of 5x7 or 5x11 dot matrix bits. What we are explaining here is the 5x7 mode module.

1602 pin description

Insert image description here

1602 address mapping

There are 80 bytes in the 1602's RAM storage, where the first line starts at 0x00 and the second line starts at 0x40
Insert image description here

1602 internal initialization command

Before controlling 1602, initialize it through the built-in instructions. The built-in instructions are as follows.

// 定义指令集
/*设置显示模式*/
#define LCD_MODE_PIN8 0x38	// 8位数据线,两行显示
#define LCD_MODE_PIN4 0x28	// 4位数据线,两个显示
#define LCD_SCREEN_CLR 0x01	// 清屏
#define LCD_CURSOR_RET 0x02	// 光标复位
#define LCD_CURSOR_RIGHT 0x06	// 光标右移,显示不移动
#define LCD_CURSOR_LEFT 0x04	// 光标左移,显示不移动
#define LCD_DIS_MODE_LEFT 0x07 	// AC自增,画面左移
#define LCD_DIS_MODE_RIGHT 0X05	// AC自增,画面右移


/*光标开关控制*/
#define LCD_DIS_CUR_BLK_ON 0x0f	// 显示开,光标开,光标闪烁
#define LCD_DIS_CUR_ON 0x0e	// 显示开,光标开,光标不闪烁
#define LCD_DIS_ON 0x0c	// 显示开,光标关,光标不闪烁
#define LCD_DIS_OFF 0x08	// 显示关,光标关,光标不闪烁

/*光标、显示移动*/
#define LCD_CUR_MOVE_LEFT 0x10	// 光标左移
#define LCD_CUR_MOVE_RIGHT 0x14	// 光标右移
#define LCD_DIS_MOVE_LEFT 0x18	// 显示左移
#define LCD_DIS_MOVE_RIGHT 0x1c	// 显示右移
1602 timing

1602 control can be summarized into two categories: reading and writing. These two operations are inseparable from the timing diagram. It is the core of controlling 1602. If you understand the timing diagram, you can control the 1602 output defined characters through code.

Read timing diagram interpretation:
Pull R/W high to enter the read operation mode. At the same time, RS is either high level or low level. When it is high level, it is a read data operation, and when it is low level, it is a read state operation. After tSP1 time, it can The enable signal E is pulled high, and the high-level maintenance time of E is tPW. After the enable signal E is pulled high, no more than tD, the 1602 LCD displays data on the DB0~DB7 data lines. At this time, we read the data and pull the enable E low, and the reading of the entire data or status is completed.

Insert image description here

Interpretation of the write timing diagram The
write operation timing is basically the same as the read operation. The only difference is that the tSP2 microcontroller must send the data to be written to the data port before the enable signal E is pulled high. When RS=1, it means that data will be written to 1602; when RS=0, it means that instructions will be written to 1602.
Insert image description here
Execution time of each stage in the sequence diagram
Insert image description here

Microcontroller and 1602 circuit schematic diagram

Insert image description here

3. Microcontroller controls 1602 output content

The following uses the microcontroller to control 1602 to output two lines of character code. This code is the minimum program to control 1602 and can be divided into the following parts:

  • Define pin function
    • Define the function of the microcontroller pin connected to the 1602 pin
  • Define instruction set
    • Several necessary operating instructions are solidified inside the 1602 chip. Use these commands to initialize the 1602.
  • Create a read busy detection function
    • 1602 can receive new commands only after each operation is completed, so check whether 1602 is busy before sending the command.
  • Create write command function
    • Receive the 1602 initialization command and the address of the output data.
  • Create a write data function
    • Receive microcontroller characters and then output them to 1602 for display
  • Create 1602 initialization function
    • Set the default working mode of 1602 through initialization
  • Create an output character function
    • Call the write command function to receive the address of the data display, call the write data function to display the data at the location specified by 1602
  • Create main function
#include <STC12C2052AD.H>
typedef unsigned char uint8;
// 定义引脚
#define	LCD1602_DB0_DB7	P1			// 定义LCD1602的数据总线
sbit LCD1602_RS = P3 ^ 2;					// 定义LCD1602的RS控制线
sbit LCD1602_RW = P3 ^ 3;					// 定义LCD1602的RW控制线
sbit LCD1602_E  = P3 ^ 4;					// 定义LCD1602的E控制线
sbit LCD1602_Busy = P1 ^ 7;					// 定义LCD1602的测忙线(与LCD1602_DB0_DB7关联)


// 定义指令集
/*设置显示模式*/
#define LCD_MODE_PIN8 0x38	// 8位数据线,两行显示
#define LCD_MODE_PIN4 0x28	// 4位数据线,两个显示
#define LCD_SCREEN_CLR 0x01	// 清屏
#define LCD_CURSOR_RET 0x02	// 光标复位
#define LCD_CURSOR_RIGHT 0x06	// 光标右移,显示不移动
#define LCD_CURSOR_LEFT 0x04	// 光标左移,显示不移动
#define LCD_DIS_MODE_LEFT 0x07 	// AC自增,画面左移
#define LCD_DIS_MODE_RIGHT 0X05	// AC自增,画面右移


/*光标开关控制*/
#define LCD_DIS_CUR_BLK_ON 0x0f	// 显示开,光标开,光标闪烁
#define LCD_DIS_CUR_ON 0x0e	// 显示开,光标开,光标不闪烁
#define LCD_DIS_ON 0x0c	// 显示开,光标关,光标不闪烁
#define LCD_DIS_OFF 0x08	// 显示关,光标关,光标不闪烁

/*光标、显示移动*/
#define LCD_CUR_MOVE_LEFT 0x10	// 光标左移
#define LCD_CUR_MOVE_RIGHT 0x14	// 光标右移
#define LCD_DIS_MOVE_LEFT 0x18	// 显示左移
#define LCD_DIS_MOVE_RIGHT 0x1c	// 显示右移


/**
LCD1602忙碌状态不会接收新指令,因此在发送新指令前先检测是否忙碌。
判断LCD1602_Busy变量的值为低电平则为不忙。
*/
void LCD1602_TestBusy(void){
    
    
	LCD1602_DB0_DB7 = 0xff;	//将数据引脚置为高电平
	LCD1602_RS = 0; // 指令状态
	LCD1602_RW = 1;	// 读状态
	LCD1602_E = 1;	// 打开LCD显示器读指令
	while(LCD1602_Busy);	//读取LCD1602_Busy(P1.7)为低电平则结束循环
	LCD1602_E = 0;	// 关闭LCD显示器读指令
}


/********************************************************************************************
// 写指令程序 //
// 向LCD1602写命令 本函数需要1个指令集的入口参数 //
/********************************************************************************************/
void LCD1602_WriteCMD(uint8 LCD1602_command) {
    
     
	LCD1602_TestBusy();
	//输入的命令赋值给LCD1602_DB0_DB7
	LCD1602_DB0_DB7 = LCD1602_command;
	LCD1602_RS = 0;
	LCD1602_RW = 0;
	LCD1602_E = 1;
	LCD1602_E = 0;
}
/********************************************************************************************
// 写数据程序 //
// 向LCD1602写数据 //
/********************************************************************************************/
void LCD1602_WriteData(uint8 LCD1602_data){
    
     
	LCD1602_TestBusy();
	LCD1602_DB0_DB7 = LCD1602_data;
	LCD1602_RS = 1;
	LCD1602_RW = 0;
	LCD1602_E = 1;
	LCD1602_E = 0;
}


// LCD1602初始化
void LCD1602_Init(void){
    
    
	LCD1602_WriteCMD(LCD_MODE_PIN8);	// 显示模式设置:显示2行,每个字符为5*7个像素
	LCD1602_WriteCMD(LCD_DIS_ON); 	// 显示开及光标设置:显示开,光标关
	LCD1602_WriteCMD(LCD_CURSOR_RIGHT);		//显示光标移动设置:文字不动,光标右移
	LCD1602_WriteCMD(LCD_SCREEN_CLR);	// 显示清屏
}


/*
输出字符串
x:数据地址
y:输出的行位置,第一行和第二行
str:输入字符串
*/
void print(uint8 x, uint8 y, uint8 *str){
    
    
	if(0 == y){
    
    
		LCD1602_WriteCMD(0x80 | x);
	}
	else{
    
    
		// 第二行起始位置是0x40
		LCD1602_WriteCMD(0x80 | (0x40+x));
	}
	while(*str != '\0'){
    
    
		LCD1602_WriteData(*str++);
	}

}



void main(){
    
    
	unsigned char str[] = "Hello LCD 1602";
	unsigned char str1[] = "Beyound Self";
	LCD1602_Init();
	print(0x00,0,str);
	print(0x00,1,str1);
	while(1);
	
}

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/134976934
Recommended