51 MCU-LCD screen code separate file

Publisher:三青Latest update time:2021-08-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Similarly, we create "lcd.c" and "lcd.h", and then add them to the project file. The code of "lcd.c" brings together all the commonly used functions that Teacher Song has written for us.


1. The code of lcd.c


#include

#include

 

/* Wait for LCD to be ready */

void LcdWaitReady()

{

    unsigned char sta;

   

    LCD1602_DB = 0xFF;

    LCD1602_RS = 0;

    LCD1602_RW = 1;

    do {

          LCD1602_E = 1;

          sta = LCD1602_DB; //Read status word

          LCD1602_E = 0;

    } while (sta & 0x80); //bit7 is equal to 1, indicating that the LCD is busy, repeat the test until it is equal to 0

}

 

/* Write a one-byte command to the LCD1602, cmd-the command value to be written*/

void LcdWriteCmd(unsigned char cmd)

{

    LcdWaitReady();

    LCD1602_RS = 0;

    LCD1602_RW = 0;

    LCD1602_DB = cmd;

    LCD1602_E = 1;

    LCD1602_E = 0;

}

 

/* Write one byte of data to LCD1602, dat-data value to be written*/

void LcdWriteDat(unsigned char dat)

{

    LcdWaitReady();

    LCD1602_RS = 1;

    LCD1602_RW = 0;

    LCD1602_DB = dat;

    LCD1602_E = 1;

    LCD1602_E = 0;

}

 

/* Set the display RAM start address, that is, the cursor position, (x, y) - corresponding to the character coordinates on the screen*/

void LcdSetCursor(unsigned char x, unsigned char y)

{

    unsigned char addr;

     

    if (y == 0) //Calculate the display RAM address from the input screen coordinates

        addr = 0x00 + x; //The first line of characters starts at 0x00

    else

        addr = 0x40 + x; //The second line of characters starts at 0x40

    LcdWriteCmd(addr | 0x80); //Set RAM address

}

 

/* Display the string on the LCD, (x,y)-corresponding to the starting coordinates on the screen, str-string pointer*/

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)

{

    LcdSetCursor(x, y); //Set the starting address

    while (*str != '') //Continuously write string data until the end character is detected

    {

        LcdWriteDat(*str++); //First get the data pointed to by str, then str adds 1

    }

}

 

/* Display the string on the LCD, (x, y)-corresponding to the starting coordinates on the screen, str-string pointer, len-length of characters to be displayed*/

void LcdShowStr_len(unsigned char x, unsigned char y, unsigned char *str, unsigned char len)

{

    LcdSetCursor(x, y); //Set the starting address

    while (len--) //Continuously write len character data

    {

        LcdWriteDat(*str++); //First get the data pointed to by str, then str adds 1

    }

}

 

/* Clear the area, clear the len characters starting from the (x,y) coordinates*/

void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len)

{

    LcdSetCursor(x, y); //Set the starting address

    while (len--) //Continuously write spaces

    {

        LcdWriteDat(' ');

    }

}

 

/* Clear the entire screen */

void LcdFullClear()

{

    LcdWriteCmd(0x01);

}    

 

/* Initialize 1602 LCD */

void InitLcd1602()

{

    LcdWriteCmd(0x38); //16*2 display, 5*7 dot matrix, 8-bit data interface

    LcdWriteCmd(0x0C); //Display on, cursor off

    LcdWriteCmd(0x06); //The text remains unchanged, the address is automatically increased by 1

    LcdWriteCmd(0x01); //Clear screen

}


2. The code of lcd.h


#ifndef __LCD_H__

#define __LCD_H__

  

#define LCD1602_DB P0

sbit LCD1602_RS = P1^0;

sbit LCD1602_RW = P1^1;

sbit LCD1602_E = P1^5;

void LcdWaitReady(); //Wait for LCD to be ready

void LcdWriteCmd(unsigned char cmd); //Write a one-byte command to LCD1602, cmd-the command value to be written

void LcdWriteDat(unsigned char dat); //Write one byte of data to LCD1602, dat-data value to be written

void LcdSetCursor(unsigned char x, unsigned char y); //Set the display RAM start address, that is, the cursor position, (x, y) - corresponds to the character coordinates on the screen

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str); //Display the string on the LCD, (x,y)-corresponding to the starting coordinates on the screen, str-string pointer

void LcdShowStr_len(unsigned char x, unsigned char y, unsigned char *str, unsigned char len); //Display the string on the LCD, (x, y)-corresponding to the starting coordinates on the screen, str-string pointer, len-length of characters to be displayed

void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len); //Area clear, clear len characters starting from (x, y) coordinates

void LcdFullClear(); //Clear the entire screen

void InitLcd1602(); //Initialize 1602 LCD

  

#endif


3.main.c code


#include  

#include //See Chapter 6, Lecture 8 for details

#include

 

void main()

{  

    unsigned char str[] = "Kingst Studio";

   

    InitLcd1602(); //Initialize LCD screen

    LcdShowStr(2, 0, str); //Start displaying the string in the array in the third cell of the first row

    LcdShowStr(0, 1, "Welcome to KST51"); //Display the string "Welcome to KST51" in the first cell of the second row

    while (1);

}


I remind you again to add it to the project file. If you don't add it, there will be no compilation error, but you will not be able to execute the corresponding code after downloading it, and the content will not be displayed normally.

11.3.png

Reference address:51 MCU-LCD screen code separate file

Previous article:51 MCU - pointer to array
Next article:51 single chip microcomputer-application of various function codes

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号