C51 MCU LCD1602 Driver

Publisher:天涯拾遗Latest update time:2022-05-26 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

LCD1602 Introduction

LCD1602 character liquid crystal (16 characters per line, two lines in total) - a dot matrix liquid crystal module specially used to display letters, numbers and symbols. It is composed of several 5x7 or 5x10 dot matrix characters. Each dot matrix character position can be used to display a character. There is a dot spacing between each bit, and there is also a spacing between each line, which plays the role of character spacing and line spacing. Because of this, it cannot display pictures very well.


1602 Pinout

We only need to pay attention to the following pins:

Pin 3: VL, LCD display bias signal, used to adjust the display contrast of LCD1602. Generally, an external potentiometer is used to adjust the bias signal. Note that the strongest contrast can be obtained when the voltage of this pin is 0.

Pin 4: RS, data/command selection terminal. When this pin is at a high level, the data byte can be transmitted to the 1602. When it is at a low level, the command byte is transmitted. The command byte is used to set some working modes of the LCD1602; the data byte is the byte used to display on the 1602. It is worth mentioning that the data of the LCD1602 is 8 bits.

Pin 5: R/W, read/write selection terminal. When this pin is high, the LCD1602 can be read, otherwise it can be written.

Pin 6: E, enable signal, is actually the data control clock signal of LCD1602. The rising edge of this signal is used to realize data transmission to LCD1602.

Pins 7~14: 8-bit parallel data port, which makes it much easier to read and write data to LCD1602.

1602 Technical Parameters

Operation Timing

Since we don't need to read 1602, we only look at the write operation below:

① When we want to write instruction words, when setting the working mode of LCD1602: we need to set RS to low level, RW to low level, and then send the data to data port D0~D7, and finally a high pulse on the E pin will write the data.

② When we want to write data words and realize display on 1602: we need to set RS to high level, RW to low level, and then send the data to data port D0~D7, and finally a high pulse on the E pin will write the data.

Did you find that the difference between writing instructions and writing data is only the different RS levels. The following is the timing diagram of LCD1602:


Write Operation Timing

Timing parameters

When writing a command byte, from left to right, RS becomes low level, R/W becomes low level, and it is noted that the RS state changes first. Then, the data on DB0~DB7 enters the valid stage, and then the E pin has a full pulse jump, and then the E pulse width with a minimum time of tpw=400ns must be maintained. Then the E pin jumps negatively, the RS level changes, and the R/W level changes. This is a complete LCD1602 write command timing.


LCD1602 key command operation

1. Clear screen command

Clear the LCD display, that is, fill all the contents of DDRAM with the "blank" ASCII code 20H;

Return the cursor to the upper left corner of the LCD screen;

Set the value of the address counter (AC) to 0;


2. Enter mode setting command

Function: Set the direction of cursor shift after entering 1 bit of data each time, and set whether to move each character written. The parameter settings are as follows:

Bit Name --- Settings;

I/D —— 0=The cursor moves left after writing new data; 1=The cursor moves right after writing new data;

S —— 0=The display screen does not move after writing new data; 1=The display screen moves right by 1 character after writing new data;


3. Display switch control instructions

Function: Control the display on/off, cursor on/off, and whether the cursor flashes. The parameter settings are as follows:

Bit Name————Setting

D —— 0=display function off; 1=display function on;

C —— 0=no cursor; 1=with cursor;

B —— 0=cursor flashes; 1=cursor does not flash;


4. Function Setting Instructions

Function: Set the number of data bus bits, the number of displayed lines and fonts. The parameter settings are as follows:

Bit Name————Setting

DL —— 0 = data bus is 4 bits; 1 = data bus is 8 bits;

N —— 0=display 1 line; 1=display 2 lines;

F —— 0=5×7 dots/each character; 1=5×10 dots/each character;


5. RAM address map

After the setting is completed, we need to understand where and at what position the display is. When initializing the LCD module, we must first set its display mode. When the LCD module displays characters, the cursor automatically moves to the right without manual intervention. Before entering each instruction, it is necessary to determine whether the LCD module is still busy. DDRAM is the display data RAM, which is used to store the character code to be displayed. There are 80 bytes in total, and the corresponding relationship between its address and the screen is as follows:

To display characters, you must first enter the display character address, that is, tell the module where to display the characters; for example, the address of the first character in the second line is 40H, so can you directly write 40H to determine the cursor position? What about the position of the first character in the second line? This is not possible, because when writing the display address, the highest bit D7 is required to be constant at high level 1, so the actual data written should be:

0100000OB (40H)+10000000B(80H)=11000000B(C0H)。

In 1602, we just use the first 16. The second line also uses the first 16 addresses. The corresponding ones are as follows:

LCD1602 driver code

Write Command

// Write a one-byte command to the LCD1602, cmd-the command value to be written 

// Write command timing: RS=0, RW=0, D7-D0 = data, E=positive pulse, LCD samples data on the falling edge of the pulse.

void lcd1602WriteCmd(unsigned char cmd)

{

delayNms(5); //delay 5ms

RS=0;// Order

RW=0;// Write

E=1;// Enable LCD1602

DPORT=cmd;// Output the command code on the data port

delay10us(); // short delay 10us

E=0;// Turn off LCD1602 enable and release the bus

}


Writing Data

// Write one byte of data to the LCD1602, dat-data value to be written 

// Write data timing: RS=1, RW=0, D7-D0 = data, E=positive pulse, LCD samples data on the falling edge of the pulse.

void lcd1602WriteByte(unsigned char dat)

{

delayNms(5); //delay 5ms

RS=1;// data

RW=0;// Write

E=1;// Enable LCD1602

DPORT=that;// Send the data to be written to the data port

delay10us(); // short delay 10us

E=0;    // Turn off LCD1602 enable and release the bus   

}


Clear screen command

// Clear screen command: clear the display content and fill all the internal RAM of 1602 with blank ASCII code 20H

//Return the cursor to the coordinate origin in the upper left corner of the screen

// Set the 1602 internal display address to 0  

void lcd1602Clear()      

{

lcd1602WriteCmd(0x01);

delayNms(50);//50ms


Setting the Cursor

void lcd1602SetCursor(unsigned char Para)    // Set the cursor

{

switch(To)

{

case 0:

{

lcd1602WriteCmd(0x08);break;     // Turn off the display

}

case 1:

{

lcd1602WriteCmd(0x0c);break;     // Turn on the display but without cursor

}

case 2:

{

lcd1602WriteCmd(0x0e);break;     // Turn on the display with cursor but without blinking

}

case 3:

{

lcd1602WriteCmd(0x0f);break;     // Turn on the display with the cursor flashing

}

default:

break;

}

}


initialization

// LCD 1602 initialization

void lcd1602Init()    

{

lcd1602WriteCmd(0x38); /*Display mode setting*/ 

delayNms(5); //5ms

lcd1602WriteCmd(0x38); 

//delayNms(5); 

//lcd1602WriteCmd(0x38); 

//delayNms(5); 

//lcd1602WriteCmd(0x38);

lcd1602WriteCmd(0x08);     //Display off, cursor not displayed, cursor not blinking

lcd1602WriteCmd(0x01);     // Clear the screen

lcd1602WriteCmd(0x06);     // Display the cursor position

delayNms(5);//5ms

lcd1602WriteCmd(0x0c);     //Display on and cursor settings

lcd1602Clear();

}


Function Encapsulation

Steps:

1. Initialization

2. Write command (RS=L) to set the display coordinates

3. Write data (RS=H)


lcd1602.h file

Statement: Please write your own delay function based on the crystal oscillator


#include "delay.h" //Please write your own delay function according to the crystal oscillator


spitRS=P1^0;// Modify according to actual hardware connection

spitRW=P1^1;// Modify according to actual hardware connection

spitAND=P1^2;// Modify according to actual hardware connection

#define DPORTP0// Modify according to actual hardware connection


void lcd1602Init();// Initialization

void lcd1602Clear();           // Clear screen command

void lcd1602SetCursor(unsigned char Para);//Set the cursor

void lcd1602WriteChar(unsigned char xPos,unsigned char yPos,unsigned char Dat); // write 1 character

void lcd1602WriteString(unsigned char xPos,unsigned char yPos,unsigned char *s); // write string


lcd1602.c file

#include "lcd1602.h"


void lcd1602WriteByte(unsigned char dat){}//Write data

void lcd1602WriteCmd(unsigned char cmd){}//Write command

void lcd1602Clear(){}//Qingping

void lcd1602SetCursor(unsigned char Para){}    // Set the cursor

void lcd1602Init(){}//initialization


//Internal function used to set the starting coordinates of displayed characters

void lcd1602WritePos(unsigned char xPos,unsigned char yPos) 

{

unsigned char tmp;

xPos&=0x0f;      // The x position range is 0~15

yPos&=0x01;      // The y position range is 0~1

if(yPos==0)         // Display the first line

tmp=xPos;  // The first line of characters starts at address 0x00

else

tmp=xPos+0x40;  // The second line of characters starts at address 0x40

tmp|=0x80;  // Set RAM address

lcd1602WriteCmd(tmp);

}


//Display the specified character in the specified row and column, xpos: row, ypos: column, c: character to be displayed

void lcd1602WriteChar(unsigned char xPos,unsigned char yPos,unsigned char Dat)

{

lcd1602WritePos(xPos,yPos);

lcd1602WriteByte(That);

}


// Display the string on the LCD, xpos: row coordinate, ypos: column coordinate, str-string pointer

void lcd1602WriteString(unsigned char xPos,unsigned char yPos,unsigned char *s)

{

unsigned char i=0;

lcd1602WritePos(xPos,yPos);  // Starting coordinates

while(s[i])

{

lcd1602WriteByte(s[i]);

i++;

if (i>=16)  break;  // Data exceeding 16 characters is discarded

}

}

Keywords:C51 Reference address:C51 MCU LCD1602 Driver

Previous article:C51 entry-level small project - two-way intersection traffic light
Next article:Getting Started with C51 MCU - Buttons

Recommended ReadingLatest update time:2024-11-23 18:11

Keil C51 Code Banking
1 Introduction MCS-51 has 8 data lines and 16 address lines, so MCS-51 can only access a maximum of 64KB (216) addresses. Many complex C51 codes may generate a Bin file of 64KB in size. In view of this situation, Keil C51 proposed the Code Banking mechanism to solve this problem. 2. Basic principles The code executi
[Microcontroller]
Keil C51 Code Banking
Embedded Learning Notes 19——Transplantation of AVR MCU C51 to ICCAVR
1. Header file, replace reg51.h, reg52.h with the corresponding AVR header files, such as iom16v.h, etc. 2. Process the data types of bit and sbit in C51. Since ICCAVR does not support bit and sbit data types, you can use bit operations and BIT to process them. Define macros for setting and clearing respectively. To u
[Microcontroller]
LCD driver transplantation boot logo
Migration environment BootLoader:u-boot-1.1.6 kernel:linux-2.6.30.4 CPU:s3c2440 Development board: TQ2440 LCD:TFT480*272 Reference: "Tianqian Linux Porting Manual" Transplantation steps 1. Modify the LCD clock calculation method The kernel source code "drivers/video/s3c2410fb.c" file is the LCD driver source code. M
[Microcontroller]
LCD driver transplantation boot logo
Current output characteristics of brushed motor driver chips. How to increase the peak drive current?
In recent years, with the enhancement of people's health awareness, the expansion of the sub-healthy, elderly Chinese, business travel and office groups, and the continuous updating and upgrading of intelligent massage chair products, massage chairs with good massage and health care effects are gradually gaining rec
[Embedded]
Current output characteristics of brushed motor driver chips. How to increase the peak drive current?
C51 DS1302 chip + lcd12864 display time
1. Schematic 1302 chip part LCD12864 part 2. Code part config.h #ifndef __CONFIG_H #define __CONFIG_H #include reg51.h #include "lcd12864.h" #include "intrins.h"  #define uchar unsigned char #define uint unsigned int #endif  lcd12864.c #include "lcd12864.h" void lcdinit(void) //Initialize LCD  {     write(0,0
[Microcontroller]
C51 DS1302 chip + lcd12864 display time
ARM4412 bare board drives LED lights, buttons, and buzzers
First find the circuit diagram of the board, taking LED as an example Next is the circuit's network label: Find the register description in the corresponding chip manual The LED light is operated by operating the corresponding register: First, register the corresponding register address into a file: reg.h   1
[Microcontroller]
ARM4412 bare board drives LED lights, buttons, and buzzers
Variable speed DC fan motor driver THMC40/41 and its application
    Abstract: THMC40/41 is a special driver that can drive and control two-phase adjustable speed 12V brushless DC motors. The article introduces the functional characteristics of THMC40/41, describes in detail the structure, pin functions and limit parameters of the driver, and finally gives their application circui
[Industrial Control]
A complete guide to high-efficiency LED driver design
As the cost of producing LEDs decreases, they are being used in more and more applications, including handheld devices, automotive electronics , and architectural lighting. LEDs are ideal lighting sources because of their high reliability, good efficiency, and ultra-fast response time. Although incand
[Power Management]
A complete guide to high-efficiency LED driver design
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号