51 MCU--LCD1602 Programming Introduction

Publisher:czm721002Latest update time:2018-11-19 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 This time I will share my LCD1602 programming experience:


Let me first explain that the chip driver of the LCD1602 I am going to talk about below is HD44780. If your LCD1602 driver chip is not HD44780, then the following content is not applicable.


My overall understanding of LCD1602


Here I will talk about my overall understanding of LCD1602. LCD1602 is an LCD display that can display two lines of characters. Each line can have 40 characters. However, only the first 16 characters of each line can be displayed. The last 24 characters of each line cannot be displayed immediately. It is not easy to understand just by saying it. Let's take a look at the schematic diagram below:


1.jpg


In addition, LCD1602 can only display characters, numbers and some simple symbols, but cannot display Chinese characters, so remember this.


LCD1602 Pinout:


Then let's talk about the pins. Whenever you learn a chip or module, the first thing you need to understand is the pins:


2.jpg


Since we are only learning how to program now, I will not talk about the circuit connection. Here are the pins related to programming:


1. RS pin: data/command selection terminal (H/L)


When you want to send a command to the LCD, give this pin a low level;


When you want to exchange data with the LCD, give this pin a high level.


2.R/W pin: read/write selection terminal (H/L)


When you want to write data to the LCD, give this pin a low level;


When you want to read the data in the LCD, give this pin a high level.


3. E pin: enable terminal (Here to take care of those who like to nitpick: Why is E called "enable"? What does "enable" mean? How come I have never heard of it?)


Answer 1: The full English name of the E pin is Enable pin. En is a prefix in English, which means "enable...", and the "able" behind it is translated as "can be", so together it is called the "enable" pin.


Answer 2: What does enable mean? I won't talk about professional terms here. You can think of the enable terminal as a switch. When this pin is high/low level, the device can be used. When it is the opposite level, the device is useless. No matter what you do, it will not work. Of course, is it a low-level device that is effective, or a high-level device that is effective? This depends on the data sheet of the specific device or some pins will be directly marked, such as E is high level effective, and E(——) is low level effective.


4.D0~D7 pins: There is nothing much to say about this. They are the pins for transmitting data or commands.


5. I won’t talk about other power pins, backlight power supply and so on, as they have been explained very clearly in the table, and we are only learning programming now, so I won’t talk about circuit connections.


Now that we have discussed the pins, let's talk about the programming part of LCD1602:


By convention, the dividing line:


3.jpg


From "Sword Art Online" Klein


I think there are only two parts that you need to remember about LCD1602, timing and instructions (you don’t have to remember them all. After learning, you can write a function library by yourself and call it directly when you need it in future development)


Let's talk about the timing and initialization process first:


LCD1602 read timing:


4.jpg


I didn't write the source code of the reading function, after all, it is almost never used.


As can be seen from the figure:


The RS pin depends on what you read. If you read data, it is high level, and if you read a command, it is low level.


The RW pin is naturally high level, because we want to read;


Then the E pin is next. When we read commands or data, we must enable the device first, so the E pin must be set to a high level;


Finally, D0 to D7 are the read data, and we just need to find a variable array to save them.


Although the reading timing has been explained, we almost do not need to perform a reading operation in actual development. I think someone will say at this time: Don’t we need to read the busy flag? Then don’t we need to perform a reading operation? That is indeed the case, but the speed of LCD1602 is in the nanosecond level, while the speed of 51 single-chip microcomputer is in the millisecond level, so there is basically no need to read the busy flag, just a slight delay will do.


As shown in the figure:


5.jpg

LCD1602 write timing:

6.jpg

I wrote this function, here is the code:

7.jpg


[Note] I used two methods in the function, paralleling 4 ports and paralleling 8 ports;


Let's talk about the most important write operation timing, as can be seen from the figure:


The RS pin depends on what you read. If you read data, it is high level, and if you read a command, it is low level.


The RW pin is naturally at a low level, because we are going to write this time;


Then the E pin is next. When we read commands or data, we must enable the device first, so the E pin must be set to a high level;


Finally, D0 to D7 are the data or commands to be written.


LCD1602 initialization process:


I also wrote this, here is the code:

8.jpg


The initialization of LCD1602, that is, the initialization process of HD44780 chip, there are too many versions of this initialization process, most of which can be successfully initialized, but I am writing an article here, so I can't talk about a messy initialization process, so I checked the original English data sheet of HD44780 chip, as shown below:


9.jpg


If you want to understand it, just read it step by step from top to bottom, and then you will understand why the initialization should be written as above.


[Note] Many initialization versions can be initialized successfully. The one I mentioned is just the most formal one. It is a personal preference. As long as the initialization can be successful, it will be fine.


Finally, write the header file of LCD1602, and then the function library of LCD1602 is ready. Of course, we did not write the read function, if you want to write these, just add them yourself. With this function library, you can directly call it when using LCD1602 in the future, which is very convenient.


Reference address:51 MCU--LCD1602 Programming Introduction

Previous article:MCU Project: 51 MCU Bluetooth Control Fan
Next article:51 Single Chip Microcomputer--Memorize the knowledge related to stepper motor

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号