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 #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.
Previous article:51 MCU - pointer to array
Next article:51 single chip microcomputer-application of various function codes
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Experience in Application Test of DSP External SRAM
- ADS amplifierMRF8P9040N verification simulation
- The most detailed summary of embedded system knowledge and interface technology ever!
- 8N60MOS field effect tube product details, original supply, high practical value
- Award-winning live broadcast | Rochester Electronics explains in detail: Challenges and solutions after semiconductor production shutdown
- C language structure pointer definition problem help
- Newbie help, how to modify the contents of a file with the suffix bin
- From fail to pass, what does DDR debugging go through?
- Understand 20 types of analog circuits in one article
- Human life status monitoring controller based on GD32E231