A Brief Discussion on Using Single Chip Microcomputer to Control 12864 LCD

Publisher:恬淡岁月Latest update time:2012-08-31 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
From now on, I will slowly write down my thoughts during the learning process as a series. There is no fixed order. I will write down whatever I have learned. I will give it a unified name called "My Study Notes: *****". Without further ado, let's start with the first note:

Speaking of 12864, there is love and hate.

Let me introduce some background: some time ago, we participated in an electronics competition, the topic was "Wireless Remote Control Drawing Car", which meant to make an A terminal as the remote control part and a B terminal as the drawing main part. Each part is controlled by the main control IC. We use the enhanced 51 single-chip microcomputer STC12C5A32S2. There are two reasons for choosing it: one is that ordinary 51 single-chip microcomputers cannot meet the requirements in terms of main frequency and RAM; the other is that before the competition, we suddenly learned that the 128 of the AVR series was discontinued. The only 128 left on the market has doubled in price, reaching 45~55 yuan per piece, which is too expensive. So I exclaimed: the era of AVR is about to pass. Finally, we chose the STC12C5A32S2 microcontroller, with a main frequency of 24M, without frequency division, which is equivalent to the 288M frequency of the traditional 51 microcontroller, and the speed is sufficient; the 32K+28K memory is enough compared to the 4K and 8K storage space of the 51; and its price is only 7 yuan/chip (purchased directly from the chip manufacturer). In this way, both the performance and price meet the requirements, and it can be said to be a microcontroller with a very high cost performance. Well, the main control is introduced. Because I mainly want to talk about the 12864 LCD this time, I will mainly introduce the A end and its control.

The A end is mainly composed of five parts: power module, main control chip and its working circuit, Omron non-coding 4*4 matrix keyboard, nrf24L01 wireless transmission module, 12864 LCD display module. The following mainly introduces some problems encountered in the use of 12864 LCD and their solutions: (I will give you some suggestions, and welcome your advice)

12864 LCD, literally speaking, is a 128*64 resolution LCD screen, which is the same as the display resolution mentioned in the computer. It's just that 12864 is a monochrome LCD screen, and the most common colors on the market are cyan and blue. We use a blue LCD screen with a Chinese character library, which is much more convenient to use. About Chinese characters and ASCII codes: Chinese characters occupy 16*16 screen space in 12864, and ASCII code characters occupy 16*8 screen space. So we can know: a 12864 uses an internal character library and can display up to 32 Chinese characters or 64 ASCII code characters. If we feel that the screen displays too little, we can abandon the internal character library of the LCD and make a character library ourselves, in which Chinese characters and ASCII codes can occupy 8*8 screen space, so that our 12864 can display up to 128 Chinese characters or ASCII code characters. So this method can be used when there is a lot of content to display.

As a background, let's talk about the driver of 12864. The driving circuit of LCD is quite complicated. An engineer with several years of work experience may not be able to design a 12864 driver by himself. But the good thing is that when we buy 12864, the manufacturer has already made the driver. All we have to do is use it through the 20P interface left by the manufacturer (this is the specialization of the profession. We don't have to care about its internal driver, as long as we can use it). I won't go into the specific name and function of the 20P pin left by the manufacturer. There are a lot of them on the Internet. After the I/O and power lines of the microcontroller are welded to the LCD, the hardware is built. Let's start software programming to control the 12864 LCD to make it display.

When it comes to software programming, we first need to prepare the platform: First: solder a download circuit for the microcontroller. The most commonly used download circuit for the 51 microcontroller is the serial port download, which requires a serial port, 5 104 capacitors, a max232 chip, a serial port cable/USB to serial port cable (the latter is mainly designed for laptops and other computers without serial ports), and several wires. This download circuit is also available on the Internet, so I won’t go into details. Second: a computer (for programming) is required. Third: a corresponding development platform is required. The most commonly used 51 microcontroller is keil, and the more popular ones are keil2 and keil3. Each has its own characteristics and can be selected according to your own habits. I personally choose the keil3 platform. With the above 3 points, the software and hardware development platform has been built. The following introduces program writing.

First, open the Keil software, create a project, and add a file to the project, then you can write the program. First, write the basic program structure: header file, main function, while loop. In order to make the program easier to understand, write functions and call them in the main function. The following introduces the writing of various functional functions.

The first is the most basic initialization operation, which requires reference to the operation timing diagram provided by the manufacturer (not detailed here, only the code is listed)

void init_12864()
{

lcd12864_psb=1; //Select parallel mode
write_cmd(0x30); //Select basic instruction
write_cmd(0x0C); //Turn on the display and turn off the cursor
write_cmd(0x01); //Clear the screen and return the address to zero
}

Next is the basic read and write operation: refer to the operation timing diagram provided by the manufacturer (not detailed here, only the code is listed)

//Write a command byte into 12864
void write_cmd(uchar cmd)
{
lcd12864_rs=0;//Pull the rs pin low to indicate a command
lcd12864_rw=0;//Indicates writing, not reading
P0=cmd;//Send the command byte to the data line

lcd12864_en=0; //Give the en pin a high pulse
delay_ms(5);
lcd12864_en=1;
delay_ms(5);
lcd12864_en=0;
}


//Write a byte of data into 12864
void write_dat(uchar dat)
{
lcd12864_rs=1;//Write data
lcd12864_rw=0;//Write
P0=dat;//Send data to the data line

lcd12864_en=0; // give the en pin a high pulse
delay_ms(5);
lcd12864_en=1;
delay_ms(5);
lcd12864_en=0;
}

The basic function is written. The operation of 12864 is as follows: first, it needs to be configured, that is, the initialization function is executed, and then the characters can be displayed. If we want to display the Chinese character "好" on the screen, we need to do the following: first write the command, the content is the display address (the first line of the first space is 0x80), and then write the data, the content is the character we want to display (the content is "好"), so our code is written like this:

init_12864();

write_cmd(0x80);

write_dat("OK");

In this way, we compile the code and download the file to the microcontroller, and then we can see the word "good" in the upper left corner of the screen on the 12864. Now let's do some more complicated operations. That is, display any string anywhere on the screen (of course, the required display space must be sufficient, otherwise there will be no place to display it). The code is as follows:

void set_xy(uchar row,uchar line) //Set the display address to row x and column y
{
switch(row) //Judge the row
{
case 1: {write_cmd(0x80|line);break;} //First row, set the column positioncase
2: {write_cmd(0x90|line);break;} //Second row, set the column positioncase
3: {write_cmd(0x88|line);break;} //Third row, set the column positioncase
4: {write_cmd(0x98|line);break;} //Fourth row, set the column position
}
}

void write_xy(uchar row,uchar line,uchar *string) //Display the string at the coordinates of row x and column y
{
uchar lcd_temp; //Define temporary storage variable for display data
set_xy(row,line); //Set the display address to row x and column y
lcd_temp=*string; //Assign the content of string to lcd_temp
while(lcd_temp!=0x00) //Judge the end mark of the string
{
write_dat(lcd_temp); //Write the corresponding content of the string
lcd_temp=*(++string); //Read the next character of the string
}
}

In this way, a function is written. If we want to display "I love electronics" in the third row and second column, we do the following: row x=3, column y=2, the string is "I love electronics", so we write the following code in the main function:

unsigned char string = "I love electronics";

write_xy(3,2,uchar *string);
In this way, we compile the code and download the file to the microcontroller. Then we can see "I love electronics" displayed on the third row and second column of the screen on 12864. The following is an introduction to partial display of pictures on 12864. Before displaying the picture, we need to obtain the binary code of the displayed picture. This can be done with the help of , and then defined in the function (I define it as logo[ ]={ }). The following is the function of partial display of pictures

void lcd12864_display(uchar code *img)/*Display function*/
{
uchar x,y;
uint i=0;//Cannot be defined as uchar, the number is not enough

for(y=24;y<=31;y++) //We can change the minimum and maximum values ​​of y to control the display area
{
for(x=1;x<3;x++)//Each x corresponds to 2 bytes, we can change the minimum and maximum values ​​of x to control the display area
{
write_cmd(0x36); //Expand the instruction and enable the graphic display at the same
timewrite_cmd(0x80+y);//Vertical addresswrite_cmd
(0x80+x);//Horizontal addresswrite_cmd
(0x30);//Change to basic instruction and then perform basic inputwrite_dat
(img[i++]); //Data writewrite_dat
(img[i++]);
}
}
for(y=0;y<=23;y++) //We can change the minimum and maximum values ​​of y to control the display area
{
for(x=1;x<3;x++) //We can change the minimum and maximum values ​​of x to control the display area
{
write_cmd(0x36);//Expand the command and enable the graphic display at the same time
write_cmd(0x80+y);//Vertical address
write_cmd(0x88+x); //Display the lower half of the screen, the y coordinate remains unchanged, and the x coordinate increases by 8 (see the picture on the datasheet)
write_cmd(0x30);//Change to basic command and then perform basic input
write_dat(img[i++]); //Write data
write_dat(img[i++]);
}
}
}

In this way, a function is written. If we can display the picture we want in any area (of course, the written area should not contain Chinese characters or character content, otherwise they will overlap. The solution to this part will be introduced below)

When I applied 12864, when I switched from basic mode to drawing mode, the screen would be distorted, which I could not solve. Finally, I wrote a function to clear the picture content. The code is as follows:

void clear_img()
{
uchar p,q;
write_cmd(0x34);
write_cmd(0x36);
for(p=0;p<32;p++)
{
write_cmd(0x80|p);
write_cmd(0x80);
for(q=0 ;q<32;q++)
write_dat(0);
}

}

After carefully studying this code, you can find that there is nothing special. It is mainly to write 0 to all parts of the image area to shield its display content, so as to solve the screen distortion phenomenon after mode switching. Then in the main function, after calling the initialization function, execute the above clear_img() function to avoid the screen distortion phenomenon after mode switching. The effect is still good, but it takes a long time. When I first used the ordinary 51 microcontroller, I chose 11.05926M crystal oscillator. After 12-frequency division, the screen clearing speed was still very slow (nearly 8 seconds). Later, I switched to 24M non-frequency-divided 51 microcontroller, and the screen clearing speed was much faster (about 0.4 seconds). Therefore, this method is not suitable for ordinary 51, otherwise the time to clear the screen will drive people crazy. It is only recommended to use this method for non-frequency-divided microcontrollers.

I had never used 12864 LCD before the competition. I started from scratch and finally made a simple operation interface on 12864 LCD. It took me two days. The problem of image clearing took up a lot of time, so I wrote this article to provide solutions to the problems for later generations. Finally, everyone is welcome to communicate with me~~

Reference address:A Brief Discussion on Using Single Chip Microcomputer to Control 12864 LCD

Previous article:Infrared receiving (simulating serial port receiving)
Next article:The Collection and Processing of High Resistance Measurement Data Based on 89C51 Discharge Method

Recommended ReadingLatest update time:2024-11-16 20:34

PIC microcontroller drives LCD12864 to display characters source code
#include pic.h //Call the header file. You can go to the PICC software to find the header file of the PIC16F87XA microcontroller. __CONFIG(XT&WDTDIS&LVPDIS);     //Define configuration word, crystal type: XT, turn off the gate dog, prohibit low voltage programming #define Rs RB0 //parallel data/control word selection
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号