51 microcontroller LCD Timer

Source: Internet
Author: User

To achieve human-computer interaction, the display device is indispensable. Previously, I talked about how to use a single-chip microcomputer to control the display of digital tubes. This article focuses on how to control the LCD display and adds the timer function to the LCD screen.

The liquid crystal used here is lcd1602, which can display 16x02 characters (16 columns and 2 rows) at the same time ). The character generation memory (cgrom) in the 1602 LCD module has stored 160 different lattice character graphics, including Arabic numerals, uppercase and lowercase letters, and common symbols, you can directly write an ascii code to it to display the corresponding characters. 1602 a total of 16 pins, with the following features:

The following figure shows the connection between the LCD module and the single-chip microcomputer:

7 ~ The 14-pin is I/O, which is connected to the P0 port of the single-chip microcomputer. The single-chip microcomputer can send data to the P0 port and receive the data in parallel by the LCD module.

The 4-pin is the data command selection end, connected to the single-chip microcomputer P3 ^ 5 port. Therefore, you can write commands or data to the LCD module by controlling the level of the P3 ^ 5 port. Learned by the data manual,

When writing commands, RS = L, RW = L, D0 ~ The D7 is the instruction code, and the E pin is a high-level pulse;

When writing data, RS = H, RW = L, D0 ~ D7 is data, and the E pin is also a high-level pulse.

P3 ^ 4 is connected to the 6-pin LCD module to control the enabling of the LCD module.

First, introduce the instructions of the LCD module.

Initialization command 0x38: Set the LCD to 16x2.

Display switch and cursor settings:

1_1dcb d = 1, on display;

C = 1. Displays the cursor;

B = 1, the cursor blinks.

000001ns n = 1. After reading and writing a character, add 1 to the address pointer and 1 to the cursor;

N = 0. After reading and writing a character, the address pointer minus 1 and the cursor minus 1.

S = 1. When writing a character, the entire screen is displayed as moving left (n = 1) or right (n = 0)

S = 0, do not move

Data Write control commands:

The RAM address ing in the LCD module is as follows:

To write data to the X address, enter the command 0x80 + X;

For example, to display the character in the first line of the second line, write the write_command (0x80 + 0x40) command before passing in the character)

In addition, 0x01h indicates that the screen is displayed, the Data Pointer is cleared, and all displays are cleared;

0x02h indicates that the carriage return is displayed, and the Data Pointer is cleared.

After understanding this, we can know that if we want to control the LCD display, we must first learn how to write instructions and data to the LCD.

Write command functions

// Write the command void write_com (uchar com) {lcdrs = 0; // rs = L p0 = com; // The command output delay (5) To lcd1602 ); // delay lcden = 1; // E = H delay (5); lcden = 0; // After delay, e becomes L, resulting in a high-level pulse, p0 port high/low level written}

The Data Writing method is similar to this:

// Write data to lcd1602/* you only need to set the RS to a high value. Other statements are the same as the write command function */void write_date (uchar date) {lcdrs = 1; // rs = H p0 = date; delay (5); lcden = 1; delay (5); lcden = 0 ;}

With these two functions, we can write commands and data into the LCD module to implement control over them.

To implement a complete encapsulation of the lcd1602 function, we need to write another initialization function to initialize the ls1602 function.

Void inden () {lcden = 0; // do not enable write_com (0x38); // write the initialization command write_com (0x0e); // display on, the cursor is displayed but does not flash write_com (0x06); // after reading and writing, the pointer is added with 1, and the cursor is added with 1 write_com (0x01); // the screen is displayed, and the pointer is cleared}

So far, the function encapsulation of lcd1602 has been completed. In other programs, these functions can be used for LCD display. For example, the following implementation uses the 1602 display timer.

The timer range is 1 hour. The display format is XX: XX (the front is minute, and the back is second)

The timer 0 in the single chip microcomputer is used, and the working mode 1 is used ,. One interruption occurs in 50 ms, so that one number used for counting is automatically increased once. The initial value of number is set to 1. Therefore, when the number is increased from 20, it indicates that the time has reached 1 S. you can do something, that is, add the processing function.

The C file is as follows:

# Include <reg52.h> # define uchar unsigned char # define uint unsigned intsbit lcden = P3 ^ 4; sbit lcdrs = P3 ^ 5; uchar num = 1; uchar Miao = 0; uchar Fen = 0; void inay (); void inital_time (); void write_com (uchar com); void write_date (uchar date); void delay (uchar time ); void clock (); void display (uchar X, uchar y); void main () {inital_time (); inital(); tr0 = 1; while (1) {If (Num> = 20) // 1 s {// do somethingclock (); display (FEN, Miao) ;}}// display the score on the LCD, second void display (uchar X, uchar y) {// in this example, X is minute, Y is second uchar xshi, xge, yshi, yge; xshi = X/10; xge = x % 10; yshi = y/10; yge = Y % 10; write_com (0x01); write_date (xshi + '0 '); write_date (xge + '0'); write_date (':'); write_date (yshi + '0'); write_date (yge + '0 ');} // void clock () {num = 1; Miao ++; // 1 minif (Miao> = 60) {Fen ++; Miao = 0 ;}// 1 H. Restart if (FEN >=60) {Fen = 0; Miao = 0 ;}} // interrupt service function, num auto-increment, re-load Initial Value void ser_timer0 () interrupt 1 {num ++; th0 = 0x4c; tl0 = 0x00 ;} // lcd1602 initialize void incision () {lcden = 0; // do not enable write_com (0x38); // write the initialization command write_com (0x0e); // display on, the cursor shows but does not flash write_com (0x06); // Add 1 to the pointer after reading and writing, and 1 to write_com (0x01) to the cursor; // display the clear screen, pointer erasing} // The timer initializes void inital_time () {tr0 = 0; // sets the timer working mode tmod = 0x01; // enable the timer to interrupt Ea = 1; et0 = 1; // initial loading value th0 = 0x4c; tl0 = 0x00;} // write the void write_com (uchar com) command to lcd1602 {lcdrs = 0; // rs = L p0 = com; // P0 port output delay (5); // delay lcden = 1; // E = H delay (5); lcden = 0; // After the delay, e becomes L, resulting in a high-level pulse. The P0 port is written to the high-level} // write data to lcd1602/* you only need to set the RS to a high level, other statements are the same as the write command function */void write_date (uchar date) {lcdrs = 1; // rs = H p0 = date; delay (5); lcden = 1; delay (5); lcden = 0;} void delay (uchar time) {uchar y, z; For (y = 0; y <= time; y ++) {z = 110; while (Z --);}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.