51 single chip microcomputer series - learning LCD1602 liquid crystal display - 8 bus - display a string of characters

Publisher:平凡梦想Latest update time:2021-12-24 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Today I reviewed the content of LCD1602 liquid crystal display and did an experiment to display a string of characters "First Try" in 8-bus mode.

The simulation diagram is as follows:


code show as below:

LCD1602.h


#ifndef _LCD1602_H_

#define _LCD1602_H_


#include


//Redefine keywords

#ifndef uchar

#define uchar unsigned char

#endif


#ifndef uint 

#define uint unsigned int

#endif


/**********************************

PIN port definition

**********************************/

#define Lcd1602DataPin P0

sbit LCD1602_E=P2^7;

sbit LCD1602_RW=P2^5;

sbit LCD1602_RS=P2^6;


/**********************************

Function declaration

**********************************/

/*Delay function under 12MHZ clock of 51 MCU*/

void Lcd1602_Delay1ms(uint c); //Error 0us

/*LCD1602 write 8-bit command subfunction*/

void LcdWriteCom(uchar com);

/*LCD1602 write 8-bit data sub-function*/ 

void LcdWriteData(uchar dat);

/*LCD1602 initialization subroutine*/  

void Lcd1602Init();


#endif


LCD1602.c


#include "lcd1602.h"

/***********************************************************************************

* Function name: Lcd1602_Delay1ms

* Function: Delay function

* Input: c

* Output: None

***********************************************************************************/

void Lcd1602_Delay1ms(uint c){//delay function

    uchar a,b;

 for (; c>0; c--){

   for (b=199;b>0;b--){

     for(a=1;a>0;a--);

   }      

 }    

}


/***********************************************************************************

* Function name: LcdWriteCom

* Function: Write a byte command to LCD

* Input: com

* Output: None

***********************************************************************************/

void LcdWriteCom(uchar com) //Write command

{

 LCD1602_E = 0; // Enable

 LCD1602_RS = 0; /*Select to send command*/

 LCD1602_RW = 0; //Select write


Lcd1602DataPin = com; //Insert command

Lcd1602_Delay1ms(1); //Wait for data to stabilize

LCD1602_E = 1; //According to the write data timing diagram, pull up the write timing (data)

Lcd1602_Delay1ms(5); //hold time

LCD1602_E = 0; //Pull low again to finish writing data, release

}


/***********************************************************************************

* Function name: LcdWriteData

* Function: Write one byte of data to the LCD

* Input: dat

* Output: None

***********************************************************************************/     

void LcdWriteData(uchar dat) //Write data

{

 LCD1602_E = 0; //Enable clear

 LCD1602_RS = 1; /*Select input data*/

 LCD1602_RW = 0; //Select write


Lcd1602DataPin = dat; //Write data

Lcd1602_Delay1ms(1);

LCD1602_E = 1; //Write timing

Lcd1602_Delay1ms(5); //hold time

LCD1602_E = 0;

}


/***********************************************************************************

* Function name: Lcd1602Init()

* Function: Initialize LCD screen

* Input: None

* Output: None

***********************************************************************************/     

void Lcd1602Init() //LCD initialization subroutine

{

 LcdWriteCom(0x38); //Turn on the display, set to 8 buses, 2 lines of display, 5*7 dot matrix, this is the "function setting command"

 LcdWriteCom(0x0c); //Turn on the display without displaying the cursor, "display switch control command"

 LcdWriteCom(0x06); //Write a pointer plus 1, "enter mode setting instruction"

 LcdWriteCom(0x01); //Clear screen, "clear screen command"

 LcdWriteCom(0x80); //Set the starting point of the data pointer. The first character is at 0x00, but the address format of the data to be written must be 0x**+0x80, so add the address plus 0x80 in advance; it is okay not to write it here, but the address to be written must be + 0x80 before calling the main function

}


main.c


#include

#include "lcd1602.h"


#define uint unsigned int

#define uchar unsigned char


uchar DisplayData[]=" First Try "; //Define the string to be displayed, and the space is also counted as a character

void main(void){

 uchar i;

 Lcd1602Init(); //Call the initialization function of LCD1602, which uses the call of the write command function

 for(i=0;i<11;i++){ //Loop calling LCD1602 write data function to write characters respectively

  LcdWriteData(DisplayData[i]);

 }

 while(1); //The infinite loop is here so that the data can be displayed statically all the time: because after the data is input into LCD1602, it will be stored in DDRAM until it changes.

}


Experimental results:

Summarize the method of using LCD1602:

1. Initialize LCD1602 first, that is, initialize the function Lcd1602Init();

The content that needs to be initialized is

(1):

(2):

(3):

(4):

These must be set during initialization, and these function command bytes are written into LCD1602 through the write command function written by yourself


In our program, we also set the cursor position, that is, at which dot matrix the data starts to be displayed. Of course, you can not write the command to determine the display address in the initialization, and just determine the display address before displaying in the main.c file.

(5): Confirm the display address command:

Figure 7

2. After initialization, you can write the characters to be displayed to the LCD's DDRAM through the data write function you wrote.

question:

1. They all write data, and they all write one byte. Why do we need a write command function "" and a "write data function"?

Answer: The reason is that the high and low level requirements for the LCD1602_RS pin are different when writing commands and writing data.

Write command: LCD1602_RS=0;

Write data: LCD1602_RS=1;

Therefore, we need to write two functions separately to implement the functionality.


2. Determine the displayed address, why is it at address +0x80

Answer: The fixed format of the command to write the display address in Figure (5) above is: 0x80+display address


3. Write timing:

4. How to display a character alone


uchar dat=1;

dat=dat+0x30;

LcdWriteData(dat); //This is it, in the form of ASCII code


5. How to display in the second line


// Display in the second line:

 LcdWriteCom(0x40+0x80); //Re-confirm the cursor position through the write command function

 for(i=0;i<11;i++){ //Recycle and call the LCD1602 write data function to write characters respectively

  LcdWriteData(DisplayData[i]);

}


5. RAM address mapping of LCD1602

Conclusion: Only by studying with a calm mind can you gain something. There is no such thing as overnight success.

Reference address:51 single chip microcomputer series - learning LCD1602 liquid crystal display - 8 bus - display a string of characters

Previous article:51 single chip microcomputer series - LCD1602 displays the characters on the calculator keys
Next article:51 MCU Series - How to use timers T0 and T1

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号