1. System Description
1. Hardware Description: Using serial transmission, the controller is ST7920. The data lines DB0-DB7 do not need to be connected; the serial chip select signal CS is connected to P3.5; the serial data port SID is connected to P3.6; the serial clock signal SCLK is connected to P3.4; the serial/parallel selection interface PSB is connected to P3^7, and is selected for serial transmission at low level.
2. Functionality: Displays a combination of Chinese characters and letters.
3. Design Plan: By calling the character display function co_disp( ) and the Chinese character display function cn_disp( ).
4. Notes: The serial working mode can omit the interface connections of LCD12864 in D0-D7, simplifying the PCB design and saving the microcontroller’s I/O resources, but real-time performance is reduced, generally used in situations where the processing and display speed requirements are not high, such as in temperature and pressure data acquisition systems.
5. The specific serial read/write operation timing diagram based on Keil5, modular compilation interface of the serial LCD12864, and display effect diagram are shown below.
Figure 1: Serial read/write operation timing diagram of LCD12864 driven by ST7920
Figure 2: Compilation interface of LCD12864 serial programming
Figure 3: Display effect diagram
2. Main function file main.c: The main function is the entry point of the program, mainly completing the call to the modular lcd12864cx.c source program, the specific main.c main program is as follows.
#include”lcd12864cx.h”
uchar code num[]={“0123456789.”};
uchar code str1[]={” Yulin University “};/* The Chinese characters here should appear at even positions of the address, because the called display character function cn_disp( ) reads the font library content in even order (one character occupies two bytes), if the space before “ Yulin University ” is changed to 3 spaces, then all displays on the first line will be garbled. If a space is added in the middle of the displayed characters, the content after the space will be garbled.*/
uchar code str2[]={” School of Energy Engineering “};
uchar code str3[]={“Pi π“};/* π is processed as a Chinese character here*/
uchar code str4[]={“0123456789ABCDEF”};
void main()
{
uchar i;
lcd_init();
clrram();
for(i=0;i<8;i++)
cn_disp(0,i,i,str1);
for(i=0;i<8;i++)
cn_disp(1,i,i,str2);/* Displaying 8 Chinese characters starting from the 0th column of the second line*/
for(i=0;i<4;i++) /* Displaying 4 Chinese characters starting from the 0th column of the third line*/
cn_disp(2,i,i,str3);
co_disp(2,4,3,10,num);/* Displaying characters num[3] and num[10] in the third line starting from the 4th column*/
co_disp(2,5,1,4,num);
co_disp(2,6,1,5,num);
co_disp(2,7,9,2,num);
for(i=0;i<8;i++)
cn_disp(3,i,i,str4);/* Display result: 0123456789ABCDEF*/
/* If the above statement is changed to co_disp(3,i,i,i,num);, the display result will be: 0011223344556677*/
while(1);
}
3. lcd12864cx.c source file: Mainly completes the definition of functions declared in the lcd12864cx.h header file, the specific lcd12864cx.c program is as follows.
#include”lcd12864cx.h”
void delay_yjp(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t; i++)
for(j=0;j<100; j++);
}
/********************************************************************
* Name : sendbyte()
* Function : Send data according to the LCD serial communication protocol
* Input : dat
* Output : None
***********************************************************************/
void sendbyte(unsigned char dat)
{
unsigned int i;
for(i=0;i<8; i++)
{
if((dat<< i) & 0x80)
{
SID= 1;
}
else
{
SID= 0;
}
SCLK = 0;
SCLK = 1;
}
}
/********************************************************************
* Name : lcd_cmd()
* Function : Write serial command
* Input : cmdcode
* Output : None
***********************************************************************/
void lcd_cmd(unsigned char cmd)
{
sendbyte(0xf8);
sendbyte(cmd& 0xf0);
sendbyte((cmd<< 4) & 0xf0);
delay_yjp(2);
}
/********************************************************************
* Name : lcd_dat()
* Function : Write serial data
* Input : dat
* Output : None
***********************************************************************/
void lcd_dat(unsigned char dat)
{
sendbyte(0xfa);
sendbyte(dat&0xf0);
sendbyte((dat<<4)&0xf0);
delay_yjp(2);
}
/*—————Display Characters—————-*/
void co_disp(uchar x,uchar y,uchar a,uchar b ,uchar code *chn)
{
if(x==0)x=0x80;
if(x==1)x=0x90;
if(x==2)x=0x88;
if(x==3)x=0x98;
lcd_cmd(0x30);
lcd_cmd(x+y);
lcd_dat(chn[a]); /*Display the ath character in the array chn*/
lcd_dat(chn[b]);/*Display the bth character in the array chn*/
}
/*—————Display Chinese Characters—————-*/
void cn_disp(uchar x,uchar y,uchar lie ,uchar code*chn)
{
if(x==0)x=0x80;
if(x==1)x=0x90;
if(x==2)x=0x88;
if(x==3)x=0x98;
lcd_cmd(0x30);
lcd_cmd(x+y);
lcd_dat(chn[2*lie]); /*According to the timing (see timing diagram for details): Data transmission is divided into two times, the first transmission is the high 4 bits, and the second transmission is the low 4 bits, since one character occupies two addresses, so multiply by 2.*/
lcd_dat(chn[2*lie+1]);
}
/*————–Clear DDRAM——————*/
void clrram (void)
{
lcd_cmd(0x30);
lcd_cmd(0x01); //Clear display command.
delay_yjp(180);
}
/*——————Initialization—————-*/
void lcd_init (void)
{
CS=1; /*Enable serial transmission chip select signal*/
PSB=0; /*Serial communication mode*/
lcd_cmd(0x30); /*30—Basic instruction action*/
lcd_cmd(0x01); /*Clear screen, address pointer points to00H*/
delay_yjp(100);
lcd_cmd(0x06); /*Cursor moving direction*/
lcd_cmd(0x0c); /*Turn on display, turn off cursor*/
}
4. lcd12864cx.h: The content in the header file mainly completes the declaration of functions that need to be used externally and the bit declaration operations of hardware. To avoid repeated compilation when the header file is referenced by multiple source files, it is necessary to handle non-repetitive inclusion. The specific content of lcd12864cx.h is as follows.
#ifndef __LCD12864CX_H__
#define __LCD12864CX_H__
#include <STC89C5xRC.H>/*Including this header file allows the use of its extended instruction set, such as bit variable declaration, sbit bit declaration, etc.*/
#define uint unsigned int
#define uchar unsigned char
sbit CS=P3^5; /*Serial chip select signal, high level to enable, generally set in the LCD initialization function.*/
sbit SID=P3^6; /*Data signal*/
sbit SCLK=P3^4; /*Clock signal*/
sbit PSB=P3^7; /*Serial/parallel selection bit, if high then parallel transmission is used, at this time the hardware needs to connect the LCD’s D0-D7 to the microcontroller’s I/O, transmitting one byte at a time, with a higher transmission rate; if low, serial communication is used, SID transmits data, SCLK synchronizes data transmission, similar to the I2C bus transmission mode. If serial transmission is used, it is generally set to low level in the LCD initialization function.*/
extern void delay_yjp(unsigned int z);/*LCD12864Serial transmission requires 24 bits (timing requirement) to transmit one byte, and the internal ST7920 controller requires some time to process serial data, so a longer delay is needed.*/
extern void sendbyte(unsigned char dat);/*Sending a byte in serial transmission.*/
extern void lcd_cmd(unsigned char cmd);/*Write serial command.*/
extern void lcd_dat(unsigned char dat);/*Write serial data*/
extern void co_disp(uchar x,uchar y,uchar a,uchar b,uchar code *chn);/*Display character (numbers and letters, etc.) function: x, y are the display row and column numbers; chn[a], chn[b] are the specific contents to be displayed.*/
extern void cn_disp(uchar x,uchar y,uchar lie ,uchar code*chn);/*Display Chinese character function: x, y are the display row and column numbers; chn[2*lie] is the specific content to be displayed.*/
extern void clrram (void);/*Clear screen command*/
extern void lcd_init(void);/*LCDInitialization*/
#endif