principle
When setting the contrast of the 1602 screen, we can see a 5*8 dot matrix. In fact, the LCD displays the character fonts (picture below), and the CGROM has been defined and can only be read but not written, while the RAM It can be read and written, so we just need to write our custom character font into the corresponding CGRAM. CGRAM has a total of 8 corresponding addresses: (0x40, 0x48, 0x50, 0x58, 0x60, 0x68, 0x70, 0x78 ), because each font corresponds to 8 lines, so you need to add 8 after each font is written. When writing to CGRAM according to the instructions, you need to add 0x40, so the corresponding address is as above. However, in fact, the actual address of CGRAM is 0x00-0x07, so write it after When entering data, you need to write it according to the actual address. The same principle applies to the fonts in CGROM.
Font code: 1602 display mode is 5*7 dot matrix. The font code corresponds to the 8 lines of the font graphic. The first three digits of each line are 0 and the last line is 0x00. Therefore, the character code for degrees Celsius is: 0x10, 0x06, 0x09,0x08,0x08,0x09,0x06,0x00
The font code can be obtained using ZIMO221 font extraction software.
Sample code
#include #include //Define custom symbols for different digit variable types #define uint8 unsigned char #define uint16 unsigned short int #define uint32 unsigned long int #define int8 signed char #define int16 signed short int #define int32 signed long int #define uint64 unsigned long long int #define int64 signed long long int //Define some commonly used instructions in 1602 so that different readers can modify them according to their own needs #define SETMODE 0x38 //16*2 display, 5*7 dot matrix, 8-bit data interface #define DISOPEN 0x0C //Display on, do not display the cursor, the cursor does not flash #define DISMODE 0x06 //Add 1 to the address after reading and writing characters, and the screen display will not move #define SETADDR 0x80 //Set the initial value of the data address pointer #define CLEAR 0x01 //Clear the screen and clear the data pointer to zero #define RET 0x02 //Enter, clear data pointer #define PORT P0 //I/O port sbit RS = P3^5; sbit E = P3^4; sbit dula = P2^6; sbit wela = P2^7; void delay(uint16 time); void Write1602_Com(uint8 com); void Write1602_Dat(uint8 dat); void Init1602(void); void Write1602_Str(uint8 addr,uint8 length,uint8 *pbuf); uint8 code xin[8]={ //Xin character model 0x00,0x1B,0x1F,0x1F,0x1F,0x0E,0x04,0x00 }; uint8 code hot[8]={ //Celsius temperature font 0x10,0x06,0x09,0x08,0x08,0x09,0x06,0x00 }; uint8 code zuo[8]={ //Chinese left part font model 0x00,0x02,0x01,0x02,0x01,0x01,0x02,0x00 }; uint8 code you[8]={ //Chinese right part font model 0x00,0x1e,0x12,0x0c,0x0a,0x11,0x00,0x00 }; uint8 i; void main() { Init1602(); //Initialize 1602 //Customized CGRAM Write1602_Str(0x40,8,hot); //Celsius temperature scale->in memory 000 of CGRAM Write1602_Str(0x48,8,zuo); //The left part of Han -> CGRAM memory 001 Write1602_Str(0x50,8,you); //The right part of Han -> CGRAM memory 010 Write1602_Str(0x58,8,xin); //Heart->Memory 011 of CGRAM Write1602_Com(0x80); //Set the DDRAM address, which is the displayed position for(i=0;i<8;i++) Write1602_Dat(0); for(i=0;i<8;i++) Write1602_Dat(3); Write1602_Com(0xc0); //Same as above for(i=0;i<16;i++) Write1602_Dat(1+i%2); while(1); } void delay(uint16 time) { uint8 i,j; for(i=time;i>0;i--) for(j=0;j<110;j++); } void Init1602(void) { dula=0; //Turn off the display of the data tube, because my 51 corresponds to the input of the data tube wela=0; //So a latch lock is needed to turn off the impact Write1602_Com(SETMODE); //Mode setting delay(5); Write1602_Com(DISOPEN); //Display settings delay(5); Write1602_Com(DISMODE); //Display mode delay(5); Write1602_Com(CLEAR); //Clear the screen delay(5); } void Write1602_Dat(uint8 dat) { E=0; RS=1; //data delay(5); //Delay PORT=dat; //port assignment delay(5); E=1; //High pulse delay(5); E=0; } void Write1602_Com(uint8 com) { E=0; RS=0; //Command delay(5); //Delay PORT=com; //port assignment delay(5); E=1; //High pulse delay(5); E=0; } void Write1602_Str(uint8 addr,uint8 length,uint8 *pbuf) { uint8 i; Write1602_Com(addr); for(i=0;i { Write1602_Dat(pbuf[i]); } } Effect A bit ugly. . . . It’s a font issue, so you can design it yourself. Summarize: 1. Design font templates for custom characters and obtain character codes 2. Write the character code in the corresponding position of CGRAM 3. Write the address corresponding to CGRAM in DDRAM
Previous article:[51 MCU] The functions of 1602 CGRAM, CGROM and DDRAM
Next article:STC89C52 microcontroller learning (1)----Lighting up the LED
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Nonlinear Compensation Method for Sensor Bridge Circuit
- DIY_boostxl-k350qvg under the recent proofing storm
- Today is 520, another beautiful day for confession. What actions will you take?
- Design and Implementation of Digital Video Conversion Interface Based on FPGA
- CR Micro's 8-bit microcontroller CS98P154 with OTP ROM
- Creative Electronics: DIY Wireless Charger for Your Phone
- Specifications of several types of batteries
- [Lazy people take care of fish tank control system] Debug method in ON Semiconductor IDE environment
- W5500 UDP Issues
- Where should the fuse be connected?