[51 microcontroller] 1602 custom character principles and examples (examples of "Chinese", love and degrees Celsius)

Publisher:真诚相伴Latest update time:2022-11-12 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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   //strlen header file

 

//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

Reference address:[51 microcontroller] 1602 custom character principles and examples (examples of "Chinese", love and degrees Celsius)

Previous article:[51 MCU] The functions of 1602 CGRAM, CGROM and DDRAM
Next article:STC89C52 microcontroller learning (1)----Lighting up the LED

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号