51 MCU-LCD screen code explanation

Publisher:心灵捕手Latest update time:2021-10-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

We will not go into details about the pointer and 1602 LCD screen. The document "Teaching You to Learn 51 Single-Chip Microcomputers" has very detailed explanations in Chapters 12 and 13. We do not need to create unique driver codes ourselves. There is no need to "reinvent the wheel". We can just transplant and use the code written by Mr. Song. Mr. Song also explained the principle of the LCD screen code in detail in the document. You can master it by reading it repeatedly. Here, I will only explain some of the codes. Open Mr. Song's code lesson12_4.


1. Display the execution operation of characters

If we need to display the following content on the LCD screen

11.1.png

Then you need to operate step by step on the code to complete it.


First, set the usage indicators of the LCD screen, such as "16*2 display, 5*7 dot matrix", "display on, cursor off", "text does not move, address automatically +1". These are called LCD screen initialization, which means that these tasks need to be configured in advance for the LCD screen to display content normally.


For example, if we want to set the LCD screen to "display on, cursor off" function, we need to write the command byte to the LCD screen as 0x0C. Before writing, we need to read whether the LCD screen is busy, that is, "sta = LCD1602_DB;" reads the 8-bit state of the entire P0 port, and "while (sta & 0x80);" means that as long as P0.7 is in a high level state, it proves that the LCD screen is busy, and the byte 0x0C cannot be written to the LCD screen. After the LCD screen outputs a low level and pulls the pin low, it proves that the LCD screen is not busy, and the command byte can be written. The analysis of the "void LcdWaitReady()" function is completed.


"void LcdWriteCmd (unsigned char cmd)" is the byte to be written into the command.

LCD1602_RS = 0;

LCD1602_RW = 0;


It prepares the LCD screen to receive commands, and then makes "P0= cmd;" output relevant high and low levels, and then makes LCD1602_E complete a high pulse. In this way, when "cmd=0x0C;", the LCD screen receives the command and makes the display function "display on, cursor off".


Then let's talk about how to implement the code to display the content from which grid. If you want to display a character in the first grid of the second line, the cursor should be set at this position, that is, the byte written to the LCD screen is "0x80|0x40", and the highest bit of the command byte for setting the coordinates should be 1. Write the address where you want to display.

11.2.png


Finally, let’s explain the “void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)” function.


The function for setting the address to be displayed has been mentioned. We see "LcdWriteDat(*str++);", which means to write the character data in an array or a string into the address of the display position in the above figure so that the characters can be displayed in the corresponding place.


In the initialization function, the setting of "LcdWriteCmd(0x06);" is to automatically increase the address by 1. If data is continuously written at the first position of the second row of the LCD screen (that is, the address is 0x40), then after writing one data, the next data will be written at the address 0x41, and the subsequent data will be written at the address 0x41.


We have briefly talked about the LCD screen code. If you still have doubts in understanding, we may lack theoretical knowledge, which needs to be digested slowly.


Of course, we don't need to go into every detail. We will teach you how to use these functions later.

Reference address:51 MCU-LCD screen code explanation

Previous article:51 MCU-Serial port printf series function
Next article:51 MCU - pointer to array

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号