51 microcontroller | Example of realizing SMC1602 LCD screen display

Publisher:代码漫游者Latest update time:2023-11-30 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

LCD1602
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  • 1602 means that each line displays 16 characters, and a total of two lines can be displayed. If it is a 12232 liquid crystal (graphic liquid crystal), it means that it is composed of 122 columns and 32 rows, that is, a total of 122*32 dots

  • ps. Pay attention to the usage temperature. Usually the working range of liquid crystal is 0~+55℃, and the storage temperature range is -22~+60℃.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Pin conditions:


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Basic operation sequence:

stateenteroutput
read statusRS=0R/W=1E=1D0~D7=status word
Read dataRS=1R/W=1E=1D0~D7=data
write commandRS=0R/W=0E=1D0~D7=command code E=1none
write dataRS=1R/W=0E=1D0~D7=Data E=1none
    stateenteroutput
    read statusRS=0R/W=1E=1D0~D7=status word
    Read dataRS=1R/W=1E=1D0~D7=data
    write commandRS=0R/W=0E=1D0~D7=command code E=1none
    write dataRS=1R/W=0E=1D0~D7=Data E=1none


      stateenteroutput
      read statusRS=0R/W=1E=1D0~D7=status word
      Read dataRS=1R/W=1E=1D0~D7=data
      write commandRS=0R/W=0E=1D0~D7=command code E=1none
      write dataRS=1R/W=0E=1D0~D7=Data E=1none


        stateenteroutput
        read statusRS=0R/W=1E=1D0~D7=status word
        Read dataRS=1R/W=1E=1D0~D7=data
        write commandRS=0R/W=0E=1D0~D7=command code E=1none
        write dataRS=1R/W=0E=1D0~D7=Data E=1none


          stateenteroutput
          read statusRS=0R/W=1E=1D0~D7=status word
          Read dataRS=1R/W=1E=1D0~D7=data
          write commandRS=0R/W=0E=1D0~D7=command code E=1none
          write dataRS=1R/W=0E=1D0~D7=Data E=1none


          stateenteroutput
          read statusRS=0R/W=1E=1D0~D7=status word
          Read dataRS=1R/W=1E=1D0~D7=data
          write commandRS=0R/W=0E=1D0~D7=command code E=1none
          write dataRS=1R/W=0E=1D0~D7=Data E=1none
            stateenteroutput
            read statusRS=0R/W=1E=1D0~D7=status word
            Read dataRS=1R/W=1E=1D0~D7=data
            write commandRS=0R/W=0E=1D0~D7=command code E=1none
            write dataRS=1R/W=0E=1D0~D7=Data E=1none


              stateenteroutput
              read statusRS=0R/W=1E=1D0~D7=status word
              Read dataRS=1R/W=1E=1D0~D7=data
              write commandRS=0R/W=0E=1D0~D7=command code E=1none
              write dataRS=1R/W=0E=1D0~D7=Data E=1none


                stateenteroutput
                read statusRS=0R/W=1E=1D0~D7=status word
                Read dataRS=1R/W=1E=1D0~D7=data
                write commandRS=0R/W=0E=1D0~D7=command code E=1none
                write dataRS=1R/W=0E=1D0~D7=Data E=1none


                  stateenteroutput
                  read statusRS=0R/W=1E=1D0~D7=status word
                  Read dataRS=1R/W=1E=1D0~D7=data
                  write commandRS=0R/W=0E=1D0~D7=command code E=1none
                  write dataRS=1R/W=0E=1D0~D7=Data E=1none


                  stateenteroutput
                  read statusRS=0R/W=1E=1D0~D7=status word
                  Read dataRS=1R/W=1E=1D0~D7=data
                  write commandRS=0R/W=0E=1D0~D7=command code E=1none
                  write dataRS=1R/W=0E=1D0~D7=Data E=1none
                    stateenteroutput
                    read statusRS=0R/W=1E=1D0~D7=status word
                    Read dataRS=1R/W=1E=1D0~D7=data
                    write commandRS=0R/W=0E=1D0~D7=command code E=1none
                    write dataRS=1R/W=0E=1D0~D7=Data E=1none


                      stateenteroutput
                      read statusRS=0R/W=1E=1D0~D7=status word
                      Read dataRS=1R/W=1E=1D0~D7=data
                      write commandRS=0R/W=0E=1D0~D7=command code E=1none
                      write dataRS=1R/W=0E=1D0~D7=Data E=1none


                        stateenteroutput
                        read statusRS=0R/W=1E=1D0~D7=status word
                        Read dataRS=1R/W=1E=1D0~D7=data
                        write commandRS=0R/W=0E=1D0~D7=command code E=1none
                        write dataRS=1R/W=0E=1D0~D7=Data E=1none


                          stateenteroutput
                          read statusRS=0R/W=1E=1D0~D7=status word
                          Read dataRS=1R/W=1E=1D0~D7=data
                          write commandRS=0R/W=0E=1D0~D7=command code E=1none
                          write dataRS=1R/W=0E=1D0~D7=Data E=1none


                            stateenteroutput
                            read statusRS=0R/W=1E=1D0~D7=status word
                            Read dataRS=1R/W=1E=1D0~D7=data
                            write commandRS=0R/W=0E=1D0~D7=command code E=1none
                            write dataRS=1R/W=0E=1D0~D7=Data E=1none


                            stateenteroutput
                            read statusRS=0R/W=1E=1D0~D7=status word
                            Read dataRS=1R/W=1E=1D0~D7=data
                            write commandRS=0R/W=0E=1D0~D7=command code E=1none
                            write dataRS=1R/W=0E=1D0~D7=Data E=1none
                              stateenteroutput
                              read statusRS=0R/W=1E=1D0~D7=status word
                              Read dataRS=1R/W=1E=1D0~D7=data
                              write commandRS=0R/W=0E=1D0~D7=command code E=1none
                              write dataRS=1R/W=0E=1D0~D7=Data E=1none


                                stateenteroutput
                                read statusRS=0R/W=1E=1D0~D7=status word
                                Read dataRS=1R/W=1E=1D0~D7=data
                                write commandRS=0R/W=0E=1D0~D7=command code E=1none
                                write dataRS=1R/W=0E=1D0~D7=Data E=1none


                                  stateenteroutput
                                  read statusRS=0R/W=1E=1D0~D7=status word
                                  Read dataRS=1R/W=1E=1D0~D7=data
                                  write commandRS=0R/W=0E=1D0~D7=command code E=1none
                                  write dataRS=1R/W=0E=1D0~D7=Data E=1none


                                    stateenteroutput
                                    read statusRS=0R/W=1E=1D0~D7=status word
                                    Read dataRS=1R/W=1E=1D0~D7=data
                                    write commandRS=0R/W=0E=1D0~D7=command code E=1none
                                    write dataRS=1R/W=0E=1D0~D7=Data E=1none


                                      stateenteroutput
                                      read statusRS=0R/W=1E=1D0~D7=status word
                                      Read dataRS=1R/W=1E=1D0~D7=data
                                      write commandRS=0R/W=0E=1D0~D7=command code E=1none
                                      write dataRS=1R/W=0E=1D0~D7=Data E=1none


                                      stateenteroutput
                                      read statusRS=0R/W=1E=1D0~D7=status word
                                      Read dataRS=1R/W=1E=1D0~D7=data
                                      write commandRS=0R/W=0E=1D0~D7=command code E=1none
                                      write dataRS=1R/W=0E=1D0~D7=Data E=1none


                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      RAM internal mapping: 80 bytes


                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      Status word description: //The status word reflects the current situation of the LCD screen. When STA7 is 1, it means that the display is busy. , cannot be operated

                                      • STA0~STA6 represent the value of the current address pointer

                                      • STA7 indicates read/write enable, =1 indicates base, =0 indicates permission


                                      ps In principle, read and write detection must be performed before each read and write operation on the controller to ensure that STA7=0. In actual operation, since the operating speed of the microcontroller is slower than the response speed of the liquid crystal control, read and write detection may not be performed but only a brief Delay.

                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      Data pointer setting:
                                      Users can access the internal 80-byte RAM through the following data address pointer to set the data address
                                      80H+address code (0~27H, 40~67H)
                                      Example: operate on the first address, 0x80+0x01
                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      -other Script code:

                                      01HThe display is cleared (the data pointer is cleared and all displays are cleared)
                                      02HDisplay carriage return (data pointer cleared)


                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      Initialization:

                                      • Display mode setting: 38H


                                      • Display on/off and cursor settings


                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      Read and write operation timing:

                                      • Read operation


                                      • write operation


                                      • Operating time parameters


                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      -Operation method:

                                      • Operations on the LCD can be divided into two parts: data and instructions.

                                      • By operating RS, R/W, and E, four operations are performed: read status, read data, write instructions, and write data.

                                      • D0~D7 are used to input and output instruction codes and data

                                      ————————————————————————————————————————————
                                      LCD1602 usage examples:
                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      Design requirements:
                                      Use LCD1602 to display two lines of characters, display two lines of words, and two buttons to control the display or clear the screen

                                      animated cover


                                      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                      -Design ideas:

                                      • Initialize LCD display //Refer to "Initialization" above

                                        • Open enable

                                        • Script operation

                                          • Set 16*2 display, 5*7 dot matrix, 8-bit data structure

                                          • Turn on the display, display the cursor without flashing

                                          • After reading and writing characters, the address pointer +1, the cursor +1, and the entire screen display does not move.

                                          • clear screen

                                          • Set the first character of the address display



                                      • initialization interrupt

                                        • Turn on interrupts

                                        • External interrupts 0 and 1


                                      • Infinite loop waiting for interrupt

                                      • key interrupt

                                        • "Display" button

                                          • Loop to write data (character array)

                                          • Command "newline"

                                          • Write the second row of data in a loop


                                        • Button "clear screen"

                                          • Command "clear screen"



                                      • Subfunction //Refer to the "Read and Write Operation Timing Table" above

                                        • write data

                                        • write command

                                       1 #include

                                       2 typedef unsigned char uchar;

                                       3 sbit en = P3 ^ 4;

                                       4 sbit rs = P3 ^ 5;

                                       5 uchar code table1[] =

                                       6 {

                                       7 "SMC1602 0x01..."

                                       8 };

                                       9 uchar code table2[] =

                                      10 {

                                      11 "Hugh Dong."

                                      12};

                                      13 void Delay(uchar m)

                                      14 {

                                      15 uchar i, j;

                                      16 for (i = m; i > 0; --i)

                                      17 for (j = 110; j > 0; --j);

                                      18}

                                      19 /*Write data*/

                                      20 void write_date(uchar date)

                                      twenty one {

                                      22 //Reference write operation timing diagram

                                      23 rs = 1; //RS high level, write data

                                      24 P2 = date; //Write data, corresponding to the timing diagram t_SP2 data establishment

                                      25Delay(5);

                                      26 en = 1; //Turn on the enable, give E a high pulse, and start writing data

                                      27Delay(5);

                                      28 en = 0; //Close enable and write data is completed

                                      29 }

                                      30 /* Write command*/

                                      31 void write_com(uchar com)

                                      32 {

                                      33 //Basic principles are the same as writing data

                                      34 rs = 0; //RS low level, write instruction

                                      35 P2 = com;

                                      36Delay(5);

                                      37 en = 1;

                                      38Delay(5);

                                      39 en = 0;

                                      40}

                                      41 /* Initialize LCD */

                                      42 voidInit()

                                      43 {

                                      44 en = 1;

                                      45 write_com(0x38); //00111000 Set 16*2 display, 5*7 dot matrix, 8-bit data interface

                                      46 write_com(0x0E); //00001110 Turn on display, display cursor, do not flash

                                      47 write_com(0x06); //00000110 After reading and writing characters, the address pointer +1, the cursor +1, and the screen does not move

                                      [1] [2]
                                      Reference address:51 microcontroller | Example of realizing SMC1602 LCD screen display

                                      Previous article:MCS-51 microcontroller storage address space division
                                      Next article:51 microcontroller interrupt mechanism (timer/counter)

                                      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号