INITIALIZING THE LCD
Before you may really use the LCD, you must initialize
and configure it. This is accomplished by sending a number of initialization
instructions to the LCD.
The first instruction we send must tell the LCD whether
we'll be communicating with it with an 8-bit or 4-bit data bus. We also
select a 5x8 dot character font. These two options are selected by sending
the command 38h to the LCD as a command. As you will recall from the
last section, we mentioned that the RS line must be low if we are sending
a command to the LCD. Thus, to send this 38h command to the LCD we must
execute the following 8051 instructions:
Init_lcd:
mov r1,#00000001b ;Display clear
acall write_inst ;
mov r1,#00111000b ;Function set,
;Data 8 bit,2 line font 5x7
acall write_inst ;
mov r1,#00001100b ;Display on, ;cursor off,cursor blink off
acall write_inst
mov r1,#00000110b ;Entry mode, Set increment
acall write_inst
ret
Write_inst: clr P2.0 ; RS = P2.0 = 0, write mode instruction mov P0,R1 ; D7 s/d D0 = P0 = R1 setb P2.1 ; EN = 1 = P2.1 call delay; call delay time clr P2.1 ; EN = 0 = P2.1 ret
The write_inst routine that we just wrote will send the instruction to pick the address or initializing.
Write_data: setb P2.0 ; RS = P2.0 = 1, write mode data mov P0,R1 ; D7 s/d D0 = P0 = R1 setb P2.1 ; EN = 1 = P2.1 call delay; call delay time clr p2.1 ; EN = 0 = P2.1 ret
The write_data routine that we just wrote will send
the character in the accumulator to the LCD which will, in turn, display
it. Thus to display text on the LCD all we need to do is load the data
in R1 to P0 that already connected to DB7 - DB0. Pretty easy, huh?
EXAMPLE :
writing " WELCOME TO " to LCD Character. Now that we have
all the component subroutines written, writing the classic "WELCOME
TO" program--which displays the text "WELCOME TO " on
the LCD is a relatively trivial matter. Consider
call init_LCD mov R1,#80h call write_inst mov R1,#'W' call write_data mov R1,#'E' call write_data mov R1,#'L' call write_data mov R1,#'C' call write_data mov R1,#'O' call write_data mov R1,#'M' call write_data mov R1,#'E' call write_data mov R1,#'' call write_data mov R1,#'T' call write_data mov R1,#'O' call write_data
The above program, should when executed, initiated LCD, choose address
DDRAM, and display WELCOME TO upper left hand corner the display.
CURSOR POSITIONING
The lcd module contains a certain amount of memory
which is assigned to the display. All the text we write to the LCD module
is stored in this memory, and the LCD module subsequently reads this
memory to display the text on the LCD module itself. This memory can
be represented with the following "memory map":
Figure 2.12. Memori Map LCD Module
In the above memory map, the area shaded in blue is
the visible display. As you can see, it measures 16 characters per line
by 2 lines. The numbers in each box is the memory address that corresponds
to that screen position.
Thus, the first character in the upper left-hanad corner is at address
00h. The following character position (character #2 on the first line)
is address 01h, etc. This continues until we reach the 16th character
of the first line which is at address 0Fh.
However, the first character of line 2, as shown in
the memory map, is at address 40h. This means if we write a character
to the last position of the first line and then write a second character,
the second character will not appear on the second line. That is because
the second character will effectively be written to address 10h--but
the second line begins at address 40h.
Thus we need to send a command to the LCD that tells
it to position the cursor on the second line. The "Set Cursor Position"
instruction is 80h. To this we must add the address of the location
where we wish to position the cursor. In our example, we said we wanted
to display "World" on the second line on the tenth character
position.
Referring again to the memory map, we see that the
tenth character position of the second line is address 4Ah. Thus, before
writing the word "WELCOME TO" to the LCD, we must send a "Set
Cursor Position" instruction--the value of this command will be
80h (the instruction code to position the cursor) plus the address 00h.
80h + 00h = 80h. Thus sending the command 80h to the LCD will position
the cursor on the firs line at the first DDRAM.
NEXT
|