51 single chip microcomputer-LCD1602 display module

Publisher:泥匠手Latest update time:2021-09-01 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

- What is LCD1602

LCD: Liquid Crystal Display, abbreviated as LCD, its main display principle is to use electric current to stimulate liquid crystal molecules to produce points, lines, and surfaces and cooperate with the back light tube to form a picture. Usually, various liquid crystal displays are directly called liquid crystals.


1602: LCDs are usually named according to the number of rows of characters or the number of rows and columns of the LCD dot matrix. 1602 means that 16 characters can be displayed per line, and a total of two lines can be displayed. There are also similar numbers such as 0801 and 1601.


Note: This type of LCD is a character LCD, which means it can only display ASCII code characters, such as numbers, uppercase and lowercase letters, various symbols, etc.


LCD1602 displays as shown below:

insert image description here

- How to operate LCD1602

Before operation, you need to understand several important knowledge points of LCD1602:

RS: Data/command selection terminal, which controls the command operation and data operation. RS=0 means LCD command operation, and RS=1 means LCD data operation.

RW: Read/write selection terminal. As the name implies, its function is to control reading and writing. RW=0 means writing to the LCD (either instructions or data can be written), and RW=1 means reading from the LCD.

E: Enable signal, a pulse of E represents the start of the operation.

RAM address mapping: The controller has an internal 80B RAM buffer, and the corresponding relationship is shown in the figure. When writing data to any of the addresses 00-0F and 40-4F in the figure (corresponding to 1602, that is, 16 characters can be displayed per line, and a total of two lines can be displayed), the LCD screen can display the data immediately, but when writing to other addresses, they must be moved into the displayable area through the screen shift instruction before they can be displayed.

insert image description here

Data pointer: There is a data address pointer inside the controller, through which the user can access all 80B of RAM (access method: 0X80+corresponding address code).


After saying so much, how to make LCD1602 display characters? Simply speaking, it can be divided into three parts: initialization, instruction operation, and data operation. Next, we will sort out these three parts (mainly based on practical applications, it is impossible to cover everything, please understand).


Initialization: Initialization can be understood as setting the display mode of LCD1602. A set of commonly used initialization instructions are listed below.


void LCD_1602_init()//initialization

{

LCD_1602_Cmd(0X38); //Display: 8 bits per line, 5x7 display dot matrix

LCD_1602_Cmd(0X06); //After writing each character, the pointer increases by one, i.e. the cursor moves one position to the right

LCD_1602_Cmd(0X0c); //Turn on the display but don't display the cursor

LCD_1602_Cmd(0X01); //Clear screen

//LCD_1602_Cmd(0X18): All displayed characters are shifted one position to the left.

//LCD_1602_Cmd(0X80): Set the starting point of the data pointer such as LCD_1602_Cmd(k2+0x80).

}


Command operation: Generally, commands are written to the LCD, so RW=0, RS=0


void LCD_1602_Cmd(uchar cmd) //write command

{

RS=0; //The timing is RS first, then RW and finally E

RW=0;

P0=cmd; //Specific instructions are given to P0, because the eight-bit data port of the general LCD is connected to P0

E=1; //One pulse, one enable signal

dy(2);

E=0;

}


Data operation: write data to LCD, that is, RW=0, RS=1


void LCD_1602_Data(uchar dat) //Write data, write data dat (dat is ASCII code characters, such as numbers, uppercase and lowercase letters, various symbols, etc.)

{

RS=1;

RW=0;

P0=dat; //Specific data for P0

E=1;

dy(2);

E=0;

}


Display location: Display is fine, but where should it be displayed on the LCD? This requires the use of the data pointer mentioned above. The data is displayed at the address where the data pointer points to. We use rows as x and columns as y to represent the specific location.


void LCD_1602_Display(uchar x,uchar y,uchar dat) //Display character dat in row x and column y

{

uchar k1=0x00,k2=0x40;//Comparing the RAM address, the 16 bits displayed in the first line are all 0x0?, and the second line are all 0x4?

if(x==1) //If it is in the first row

{

k1+=y; //At this time, k1 corresponds to the (x,y) address

LCD_1602_Cmd(k1+0x80);//Access location of data pointer

}

else // in the second line

{

k2+=y;

LCD_1602_Cmd(k2+0x80);

}

LCD_1602_Data(dat); //Display the corresponding data on (x,y)

}


Display numbers and single characters: When displaying numbers, because the ASCII code of the number 0 is 48, LCD_1602_Data(6+48); is required. When displaying a single character, single quotes are required LCD_1602_Data('k');, so LCD_1602_Data('6') can also be used to display numbers.

This is enough for the operation of LCD1602.


- Upload code

I used LCD1602 to realize a simple "I love you" display. The code is as follows (I have tested it and it works):


#include "reg51.h"

#define uchar unsigned char

#define uint unsigned int

uchar p[]="i love you";

sbit E=P2^7; // Enable E, send signal in pulse form

sbit RS=P2^6; //0: instruction 1: character

sbit RW=P2^5; //0: write 1: read


void dy(uint x)

{

uint i;

i=x*100;

while(i--);

}


void LCD_1602_Cmd(uchar cmd) //write command

{

RS=0; //The timing is RS, RW, E

RW=0;

P0=cmd;

E=1; //One pulse, one enable signal

dy(2);

E=0;

}


void LCD_1602_Data(uchar dat)//write character

{

RS=1;

RW=0;

P0=dat;

E=1;

dy(2);

E=0;

}

void LCD_1602_init()//initialization

{

LCD_1602_Cmd(0X38); //Display: 8 bits per line, 5x7 display dot matrix

LCD_1602_Cmd(0X06); //After writing each character, the pointer increases by one, i.e. the cursor moves one position to the right

LCD_1602_Cmd(0X0c); //Turn on the display but don't display the cursor

LCD_1602_Cmd(0X01); //Clear screen

//LCD_1602_Cmd(0X18): All displayed characters are shifted one position to the left.

//LCD_1602_Cmd(0X80): Set the starting point of the data pointer such as LCD_1602_Cmd(k2+0x80).

}


void LCD_1602_Display(uchar x,uchar y,uchar dat) //Display character dat in row x and column y

{

uchar k1=0x00,k2=0x40;

if(x==1)

{

k1+=y;

LCD_1602_Cmd(k1+0x80);

}

else

{

k2+=y;

LCD_1602_Cmd(k2+0x80);

}

LCD_1602_Data(dat);

}


void display(uint l,uchar *p) //display string

{

uint i;

for(i=0;i LCD_1602_Data(p[i]);

}


void main()

{

uchar i;

LCD_1602_init();

//LCD_1602_Display(2,0,6+48); //Add 48 when displaying numbers, because the ascii code of 0 is 48

for(i=0;i {

LCD_1602_Display(1,i,p[i]);

}

LCD_1602_Cmd(0X80+0X40);  

display(sizeof(p)-1,p);

}

Reference address:51 single chip microcomputer-LCD1602 display module

Previous article:C51 - Small Knowledge Points
Next article:51 single chip microcomputer - use PWM to control the speed of DC motor

Recommended ReadingLatest update time:2024-11-17 01:41

Intelligent calculator based on single chip microcomputer (AT89C51, LCD1602, matrix keyboard)
1. Introduction This circuit consists of AT89C51 minimum system, LCD1602 liquid crystal display module and matrix keyboard module. 2. Implementation Effect 3. Partial Code /* S16 S12 S8 S4 are 123+ S15 S11 S7 S3 are 456- S14 S10 S6 S2 are 789* S13 S9 S5 S1 are 0 CLR = / */ /*Want more projects private wo!!!*
[Microcontroller]
Intelligent calculator based on single chip microcomputer (AT89C51, LCD1602, matrix keyboard)
LCD1602-DS1302 clock program
//DS1302 clock program #include #include #define uchar unsigned char #define uint unsigned int /****************************************************************/ //Shaozhanyu production Hebei Zhengding welcomes you Changsha Aviation Vocational and Technical College //2010 QQ:411656434 //Copyright:
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号