【C51】Source code 5 -- LCD 1602 display characters

Publisher:喜茶我要七分糖Latest update time:2016-09-28 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
LCD 1602, formally called LCM 1602 (including some peripheral circuits), is generally used to display simple characters, and can also be "created" in a customized way.

I have just learned the basic character display. Just character display requires a large number of various instruction formats. Let's write a program at this stage and summarize it:

Program function: Display characters in the center of the LCD: "Hello World", "By Fate"

Note: Use of LCD 1602: http://gaebolg.blog.163.com/blog/static/198269068201231665524137/

 

Attached is the source code: (I am a rookie, so there are inevitably some poorly written parts. It is only for backup purposes. Welcome to give me some advice, and please go away if you hate me...)

/*------------------------------------------------------------------------------------
LCD 1602 displays characters, "Hello World", "By Fate" are displayed in the center
-------------------------------------------------------------------------------------*/

#include
#include // Various functions, the empty function _nop_() is used here

sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;

#define DataPort P0

void Init_LCD(void); // Initialize LCD display

void LCD_Write_Com(unsigned char c); // write command to LCD
void LCD_Write_Data(unsigned char d); // write data to LCD

bit LCD_Busy(void); // LCD busy detection function, busy returns 1, idle returns 0
void LCD_Clear(void); // Clear screen

/*-----------------------------------------------------------------------------------------------------
LCD parameter 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 = 5x7 dots/each character 1 = 5x10 dots/each character
--------------------------------------------------------------------------------------*/
void LCD_Set_Argument(unsigned char DL, unsigned char N, unsigned charF);

/*---------------------------------------------------------------------------------------------------------------------------------------------------
LCD input mode setting: ID 0 = Cursor moves left after writing new data 1 = Cursor moves right after writing new data
                          0 = Display does not move after writing new data 1 = Display moves right by 1 word after writing new data
------------------------------------------------------------------------------------------------------------------------------------*/
void LCD_Set_InputMode(unsigned char ID, unsigned char S);

/*---------------------------------------------------------------------------------------
LCD display settings: D 0 = Display function off 1 = Display function on
                   C 0 = No cursor 1 = With cursor
                   B 0 = Cursor does not blink 1 = Cursor blinks
--------------------------------------------------------------------------*/
void LCD_Set_DisplayMode(unsigned char D, unsigned char C, unsigned char B);

/*-----------------------------------------------------------
Display character c at row, column col
------------------------------------------------------------*/
void LCD_Write_Char(unsigned char row, unsigned char col, unsigned charc);

/*-----------------------------------------------------------
Starting from row, column col, display string s
------------------------------------------------------------*/
void LCD_Write_String(unsigned char row, unsigned char col, unsigned char *s);

void Delay(unsigned int t);
void Delay_ms(unsigned int t);       // 延迟 t ms

void main (void)
{
    Init_LCD();
    LCD_Write_Char(1, 3, 'H');
    LCD_Write_Char(1, 4, 'e');
    LCD_Write_Char(1, 5, 'l');
    LCD_Write_Char(1, 6, 'l');
    LCD_Write_Char(1, 7, 'o');
    LCD_Write_String(1, 9, "World!");
    LCD_Write_String(2, 5, "By Fate!");
    while (1);
}

void Init_LCD(void)
{
    LCD_Set_Argument(1, 1, 0); // Set basic parameters: 8-bit data bus, 2-line display, 5x7 dot matrix/character
    LCD_Set_DisplayMode(0, 0, 0); // Set display mode: turn off display, turn off cursor display, turn off cursor blinking
    LCD_Set_InputMode(1, 0); // Set input mode: for each character input, the cursor moves right and the screen does not move
    LCD_Clear(); // Clear the screen
    LCD_Set_DisplayMode(1, 0, 0); // Turn on display
}

void LCD_Write_Com(unsigned char c)
{
    while (LCD_Busy());
    RS = 0;
    RW = 0;
    EN = 1;
    DataPort = c;
    _nop_(); // Small delay, much smaller than Delay(), purpose: to make the data on the bus stable
    EN = 0; // Write operation is driven by EN falling edge
}

void LCD_Write_Data(unsigned char d)
{
    while (LCD_Busy());
    RS = 1;
    RW = 0;
    EN = 1;
    DataPort = d;
    _nop_();
    EN = 0;
}

bit LCD_Busy(void)
{
    DataPort = 0xFF;
    RS = 0;
    RW = 1;
    EN = 0;
    _nop_();
    EN = 1; // Read operation is EN = 1 drive
    return (bit)(DataPort & 0x80); // When forced to convert to bit, unless the original data is equal to 0, bit = 0, otherwise bit = 1
}

void LCD_Clear(void)
{
    LCD_Write_Com(0x01);
    Delay_ms(5);
}

void LCD_Set_Argument(unsigned char DL, unsigned char N, unsigned charF)
{
    DL <<= 4; // The instruction format determines the shift, see the instruction format requirements
    N <<= 3;
    F <<= 2;
    LCD_Write_Com(0x20 | DL | N | F);
    Delay_ms(5);
}

void LCD_Set_InputMode(unsigned char ID, unsigned char S)
{
    ID <<= 1;
    LCD_Write_Com(0x04 | ID | S);
    Delay_ms(5);
}

void LCD_Set_DisplayMode(unsigned char D, unsigned char C, unsigned char B)
{
    D <<= 2;
    C <<= 1;
    LCD_Write_Com(0x08 | D | C | B);
    Delay_ms(5);
}

void LCD_Write_Char(unsigned char row, unsigned char col, unsigned charc)
{
    if (row == 1) LCD_Write_Com(0x80 + col - 0x01); // From the instruction format, we can see that DB7 = 1, so add 80H
    else if (row == 2) LCD_Write_Com(0xC0 + col - 0x01);
    LCD_Write_Data(c);
}

void LCD_Write_String(unsigned char row, unsigned char col, unsigned char *s)
{
    if (row == 1) LCD_Write_Com(0x80 + col - 0x01);
    else if (row == 2) LCD_Write_Com(0xC0 + col - 0x01);
    while (*s) {
        LCD_Write_Data(*s);
        s++;
    }
}

void Delay(unsigned int t)
{
    while (t--);
}

void Delay_ms(unsigned int t) // According to the test, t ms can be represented fairly closely
{
    while (t--) {
        Delay(245);
        Delay(245);
    }
}

Keywords:C51 Reference address:【C51】Source code 5 -- LCD 1602 display characters

Previous article:Tip 2 -- Summary of MCU C51 & A51 Programming Key Points
Next article:【C51】Source code 4 -- Speaker plays the theme song "You" of "Higurashi When They Cry"

Recommended ReadingLatest update time:2024-11-16 20:56

Example of using the command line to compile C51 source code and generate a HEX file
Refer to Keil\C51\HLP\C51.pdf and A51.pdf documents, examples and instructions are as follows: set PATH=%PATH%;G:\develop\keil\C51\BIN;G:\develop\keil\C51\INC;G:\develop\keil\C51\LIB set C51_LIB=G:\develop\keil\C51\LIB\ (Note: the G:\develop\keil directory should be changed to the location where Keil is installed on t
[Microcontroller]
[C51 self-study notes] Serial communication + RS-232C interface + RS-422A/RS-485 interface
Introduction: Computer communication refers to the information exchange between a computer and an external device or between two computers. There are two modes of communication: parallel communication and serial communication. In multi-microcomputer systems and modern measurement and control systems, serial communic
[Microcontroller]
[C51 self-study notes] Serial communication + RS-232C interface + RS-422A/RS-485 interface
C51 program for single chip microcomputer to drive standard PC keyboard
C51 program for single chip microcomputer to drive standard PC keyboard   //#i nclude "reg51.h" #i nclude "intrins.h" #i nclude "ku.h" //Key passcode and ascii comparison table sbit sda= p1^0; //Keyboard data line unsigned char dat=0,dat1=0,dat2=0; //Receive keyboard data variable? Store passcode variable to acc
[Microcontroller]
Expert summary! Three pieces of C51 programming experience
 Three Experiences of C51 Programming In the development and application of single-chip microcomputers, high-level languages ​​have gradually been introduced, and C language is one of them. People who are used to assembly language always feel that high-level languages ​​are not as controllable as assembly language.
[Microcontroller]
51 single chip microcomputer to realize the water lamp
  Before implementing the running light, we need to mention that before writing the C51 code, you need to refer to the circuit diagram. Take the running light as an example. Let's look at Figure 2. The positive poles of the eight LEDs are connected to the +5V voltage through a bus. Looking at Figure 1 again, we can
[Microcontroller]
51 single chip microcomputer to realize the water lamp
C51 Programming 11-Interrupts (Interrupt Principles 1)
In the previous IO chapter, the matrix keyboard and LED were used. The main function used their functions to detect whether the matrix keyboard was pressed and whether the LED needed to be lit. As shown in the following code, the keyboard scan and display are continuously executed in the loop. /*********************
[Microcontroller]
C51 Programming 11-Interrupts (Interrupt Principles 1)
Proteus C51 simulation learning board 7——LCD1602
LCD1602 is the most basic character LCD display, which can display 16x02=32 characters. So the operation of timing - reading and writing is the most important content. Today, I will take you to read the timing. Before looking at the timing diagram, you need to understand the pin functions of the LCD to better unders
[Microcontroller]
Proteus C51 simulation learning board 7——LCD1602
MEGA8 MCU drives LCD1602
I tried to find a header file for a long time but it didn't work! I was almost discouraged, but suddenly I had an idea: could it be that the Flash of mega8 is not working? This mega8 has been with me for many years and it should be retired. I replaced it with a new mega8 with the idea of ​​giving it a try, and everyth
[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号