OBJECTIVE:
- Students understand the microcontroller interface circuit with a 2 x 16 Character LCD
- Students can understand assembly programs to display data on a 2 x 16 Character LCD.
- Students understand some basic assembly instructions, MOV, Setb, Clr, and delay time.
- Students understand how to print characters in certain row and column positions.
Figure 4.1. Interface circuit to 2 x 16 Character LCD
The Character LCD module can be easily connected to a microcontroller such as AT89S51. The LCD that we will practice has a display width of 2 rows 16 columns or commonly called a 2x16 Character LCD, with 16 connector pins, which are defined as follows:
Figure 4.2. 2x16 Character LCD Module
Table 1.1 Pins and Functions
| PIN | Name | Function |
|-----|------|------------------------------------------------------------------------|
| 1 | VSS | Ground voltage |
| 2 | VCC | +5V |
| 3 | VEE | Contrast voltage |
| 4 | RS | Register Select 0 = Instruction Register 1 = Data Register |
| 5 | R/W | Read/ Write, to choose write or read mode 0 = write mode 1 = read mode |
| 6 | E | Enable 0 = start to lacht data to LCD character 1= disable |
| 7 | DB0 | LSB |
| 8 | DB1 | - |
| 9 | DB2 | - |
| 10 | DB3 | - |
| 11 | DB4 | - |
| 12 | DB5 | - |
| 13 | DB6 | - |
| 14 | DB7 | MSB |
| 15 | BPL | Back Plane Light |
| 16 | GND | Ground voltage |
The character display on the LCD is controlled by the EN, RS and RW pins:
The EN line is called Enable. This line is used to tell the LCD that you are sending data. To send data to the LCD, then through the EN program must be made low logic "0" and set on the other two control lines RS and RW. When the other two lines are ready, set EN to logic "1" and wait for a certain amount of time (according to the datasheet of the LCD) and then set EN to logic low "0" again.
The RS line is the Register Select line. When RS is low logic "0", the data will be considered as a special command or instruction (such as clear screen, cursor position etc). When RS is high logic "1", the data sent is text data that will be displayed on the LCD display. For example, to display the letter "T" on the LCD screen, RS must be set to high logic "1".
The RW line is the Read/Write control line. When RW is logic low (0), then the information on the data bus will be written to the LCD screen. When RW is logic high "1", then the program will read the memory from the LCD. While in general applications the RW pin is always given logic low "0".
Ultimately, the data bus consists of 4 or 8 lines (depending on the operating mode selected by the user). In the case of an 8-bit data bus, the lines are referred to as DB0 to DB7. Some basic commands to understand are the initialization of the Character LCD.
Function Set
Set the interface data width, number of rows and character font size.
| RS | R/W | DB7 | DB6 | DB5 | DB4 | DB3 | DB2 | DB1 | DB0 |
|----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| 0 | 0 | 0 | 0 | 1 | DL | N | F | X | X |
NOTES:
- X : Don't care
- DL: Sets the data width
- DL=1, Data interface width 8 bits (DB7 to DB0)
- DL=0, Data interface width 4 bits (DB7 to DB4)
- When using 4 bit data width, data must be transmitted twice.
Entry Mode Set
Set the increment/decrement and shift mode.
| RS | R/W | DB7 | DB6 | DB5 | DB4 | DB3 | DB2 | DB1 | DB0 |
|----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | I/D | S |
NOTES:
- I/D: Increment/decrement of the DDRAM address by 1 when the character code is written to the DDRAM.
- I/D = "0", decrement
- I/D= "1", increment
- S: Slide the entire display to the right and left
- S=1, shift left or right depending on I/D
- S=0, display does not shift
Display On/Off Cursor
Sets the display status ON or OFF, cursor ON/OFF and Cursor Blink function.
| RS | R/W | DB7 | DB6 | DB5 | DB4 | DB3 | DB2 | DB1 | DB0 |
|----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| 0 | 0 | 0 | 0 | 0 | 0 | 1 | D | C | B |
NOTES:
- D : Set the display
- D = 1, Display is ON
- D = 0, Display is OFF
In this case the display data still remains in DDRAM, and can be displayed again directly by setting D=1.
- C : Displays the cursor
- C = 1, cursor is displayed
- C = 0, cursor is not displayed
- B : The character is indicated by a blinking cursor.
- B=1, cursor blink
Clear Display
This command clears the screen.
| RS | R/W | DB7 | DB6 | DB5 | DB4 | DB3 | DB2 | DB1 | DB0 |
|----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
Shift Cursor and Display
Move the cursor or display position to the right or left without writing or reading display data. This function is used for display correction or search.
| RS | R/W | DB7 | DB6 | DB5 | DB4 | DB3 | DB2 | DB1 | DB0 |
|----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
| 0 | 0 | 0 | 0 | 0 | 0 | S/C | R/L | X | X |
NOTE: x = Dont care
| S/C | R/L | Note |
|-----|-----|---------------------------------------|
| 0 | 0 | Shift cursor position to the left |
| 0 | 1 | Shift cursor position to the right |
| 1 | 0 | Shift the entire display to the left |
| 1 | 1 | Shift the entire display to the right |
Cursor Position
The LCD module consists of a number of memories used for display. All the text we write to the LCD module is stored in this memory, and the LCD module sequentially reads this memory to display the text to the LCD module itself.
On the memory map, the blue areas (00 to 0F and 40 to 4F) are the visible displays. As you can see, there are 16 characters per row with two rows. The number in each box is the memory address corresponding to the position on the screen. Thus the first character in the upper left corner is at address 00h. The next character position is at address 01h and so on.
However, the first character of line 2 as shown in the memory map is at address 40h. Thus we need to send a command to the LCD to set the cursor position to a particular row and column. The Set Cursor Position instruction is 80h.
For this we need to add the address of the location where we want to place the cursor. For example, we want to display the word "World" on the second row at the tenth column position. According to the memory map, the character position in column 11 of the second row, has the address 4Ah, so before we write the word "World" on the LCD, we have to send the cursor position set instruction, and the command for this instruction is 80h plus the address 80h+4Ah =0Cah. So by sending the Cah command to the LCD, it will place the cursor on the second row and 11th column of the DDRAM.
Installation
Before we can use the LCD module, we must initialize and configure it. This is done by sending a number of instructions to the LCD. Among others: setting the width of the interface data to 8 bits or 4 bits of data bus, selecting the size of the character font 5x8 or 5x7 and others, with the following instructions.
Init_lcd:
mov r1,#00000001b ;Display clear
call write_inst ;EN=pulse dan RS=0
mov r1,#00111000b ;Function set, ;Data 8 bit,2 line font 5x7
call write_inst ;Set bit EN=pulse dan RS=0
mov r1,#00001100b ;Display on, ;cursor off,cursor blink off
call write_inst ;EN=pulse dan RS=0
mov r1,#00000110b ;Entry mode, Set increment
call write_inst
ret
Experiment 4.1. Write a Character on a Character LCD
In this experiment, the character 'A' will be displayed on 7 Segment Display 1. To perform this experiment, do the following steps:
- Install the LCD_EN jumper, which functions to provide a +5V power supply
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program: ( download file prog41a.asm )
org 0h call init_LCD start: mov R1,#80h ; Lokasi Display RAM, Row=1 Col=1 call write_inst mov R1,#'A' ; Cetak Karakter A call write_data stop: sjmp stop Init_lcd: mov r1,#00000001b ;Display clear call write_inst mov r1,#00111000b ;Function set,Data 8 bit,2 line font 5x7 call write_inst mov r1,#00001100b ;Display on, ;cursor off,cursor blink off call write_inst mov r1,#00000110b ;Entry mode, Set increment call write_inst ret ; Write_inst: clr P3.6 ; P3.6 = RS =0 mov P0,R1 ; P0 = D7 s/d D0 = R1 setb P3.7 ; P3.7 =EN = 1 call delay ; call delay time clr P3.7 ; P3.7 =EN = 0 ret ; Write_data: setb P3.6 ; P3.6 = RS =1 mov P0,R1 ; P0 = D7 s/d D0 = R1 setb P3.7 ; P3.7 =EN = 1 call delay ; call delay time clr p3.7 ; P3.7 =EN = 0 ret ; delay: mov R0,#0 delay1:mov R7,#0fh djnz R7,$ djnz R0,delay1 ret ; end
- Save the program you typed and name it: prog41a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Program the microcontroller using the ISP Software Program (See Instructions for Use)
- Make observations on the LED.
- Make modifications to the program to print other characters, according to the table:
| Nama Karakter | Posisi Display |
|---------------|----------------|
| 1 | Row 1, Col 2 |
| Z | Row 1, Col 8 |
| & | Row 2, Col 12 |
Experiment 4.3. Write Characters With Look Up Table
In this experiment, the character 'A' will be displayed on 7 Segment Display 1. To perform this experiment, do the following steps:
- Install the LCD_EN jumper, which functions to provide a +5V power supply
- Connect the Microcontroller Trainer module to the +5V power supply.
- Connect the Microcontroller Trainer module to the programmer circuit
- Open the M-IDE Studio for MCS-51 program, as a program editor and compiler.
- Type the following program: ( download file prog43a.asm )
org 0h call init_LCD start: call write_char stop: sjmp stop ; write_char: mov dptr,#word1 ;DPTR = [ address word1 ] mov r3,#16 ;R3=16,number character to be display mov r1,#80h ;R1=80h,address DDRAM start position call write_inst ; write1:clr a ; A = 0 movc a,@a+dptr ; A = [A+ DPTR] mov r1,A ; R1 = A inc dptr ; DPTR = DPTR +1 call write_data; djnz r3,write1 ; R3 = R3-1, ret ; Init_lcd: mov r1,#00000001b ;Display clear call write_inst ; mov r1,#00111000b ;Function set, Data 8 bit,2 line font 5x7 call write_inst ; mov r1,#00001100b ;Display on, cursor off,cursor blink off call write_inst mov r1,#00000110b ;Entry mode, Set increment call write_inst ret ; Write_inst: clr P3.6 ; P3.6 = RS =0 mov P0,R1 ; P0 = D7 s/d D0 = R1 setb P3.7 ; P3.7 =EN = 1 call delay ; call delay time clr P3.7 ; P3.7 =EN = 0 ret ; Write_data: setb P3.6 ; P3.6 = RS =1 mov P0,R1 ; P0 = D7 s/d D0 = R1 setb P3.7 ; P3.7 =EN = 1 call delay ; call delay time call delay ; call delay time clr p3.7 ; P3.7 =EN = 0 ret ; delay: mov R0,#0 delay1:mov R7,#0fh djnz R7,$ djnz R0,delay1 ret ; word1: DB ' Welcome Home '; Karakter yang disimpan di ROM ; end
- Save the program you typed and name it: prog43a.asm
- In the MIDE program, select Build /F9 or to compile the program from *.asm to *.hex.
- Program the microcontroller using the ISP Software Program (See Instructions for Use)
- Make observations on the LCD.
- Make modifications to the program to print other characters, according to the table:
| Nama Karakter | Start Posisi Display |
|-----------------|----------------------|
| Selamat Datang | Row 1, Col 1 |
| Mikrokontroller | Row 2, Col 1 |