Search This Blog

Tuesday, 9 June 2015

LCD interfacing with 8051

Hello Everyone!!!
Many people face a lot of problems in interfacing LCD with 8051 micro controller, but in reality it is very easy and even fun to interface an LCD!!
16*2 LCD LM0161

WHAT DOES DIFFERENT PINS STAND FOR IN LCD????

First of all we will need to understand what does the pins in the LCD means...

VSS: To be provided with Ground.
VDD: To be provided with Source voltage.
VEE: Can be grounded or a potentiometer can be used to adjust the contrast of LCD.
RS: Is the register select pin decide either to write comment or data
RW: Is the read write pin decide either to write or read data
E: Is the enable pin used as clock
D0-D7 : Are the data pins messages are send on these pins.

There are two things to be written on LCD 
1) Command
2) Data 

What is a Command??? 

A command is a code for LCD that order it to set itself as per our requirements.
for example:
01H will clear your LCD
06H cursor will be incremented by 1 unit

How to write command?

In order to write a command we will need to :
  • clear the RS pin (RS=0)
  • clear the RW pin (RW=0)
  • Give clock on E pin with some delay
CODE:
For clearing LCD:

mov DATA,#01H
clr RS
clr RW
setb E
nop
nop
clr E

this code will clear you LCD many commands are available on internet.


Now What is a Data???

The data is simply the message we want to display on LCD.

How to write Data?

In order to write a data we will need to :

  • High the RS pin (RS=1)
  • clear the RW pin (RW=0)
  • Give clock on E pin with some delay
CODE:

For displaying letter 'A' on LCD:

mov DATA,#'A'
setb RS
clr RW
setb E
nop
nop
clr E

this code will display letter A on the LCD.


WRITING ON LCD!!!

As you are familiar with coding of LCD let us write our topic name on LCD!!!

SCHEMATIC:



CODE:

;---INIITIALIZING PORTS AND BITS----

rs bit P3.0     ; initializing port 3.0 as register select pin
rw bit P3.1      ; initializing port 3.1 as read/write pin
en bit P3.2     ; initializing port 3.2 as enaable pin
dat equ P2     ; initializing port 2 as data bus

org 00h  
jmp 30h
org 30h

;---WRITING COMMANDS ON LCD ----

mov A,#38h     ;Use 2 lines and 5×7 matrix for LCD
acall com_wrt ; calling function to enter command on LCD 
mov A,#0Fh ; LCD ON, Cursor ON, Cursor blinking ON
acall com_wrt ; calling function to enter command on LCD 
mov A,#01h     ; clearing the LCD
acall com_wrt ; calling function to enter command on LCD  
mov A,#80h     ; Force cursor to the beginning of 1st line
acall com_wrt ; calling function to enter command on LCD 

;---CLEARING/SETTING PORTS/PINS----


mov P2,#00H ; clearing port 2 to eleminate garbage value

;---START OF PROGRAM----

START:
call clear ; calling function to clear LCD

DISPLAY_MSG:
mov dptr,#msg ; copy message in data pointer to be displayed on LCD
call dmsg ; calling function to display message on LCD
DISPLAY_MSG1:
call nline
mov dptr,#msg1
call dmsg
jmp exit
clear:  ; LCD clearing module
mov a,#01h          ; clearing LCD 
acall com_wrt  ; calling function to enter command on LCD
ret  ; returning back 

dmsg:
call delay   ; calling some time delay
clr A   
movc a,@a+dptr   ; moves code byte of data located in program code ROM into Accumulator A
call char_wrt   ; calling function to write character 
inc dptr   ; incriment the value of data pointer by 1
jz display_msg1
jmp dmsg


;---CHARACTER DISPLAYING MODULE----

char_wrt:  
acall delay  ; calling some time delay
setb rs  ; setting bit register select high 
clr rw  ; clearing bit to write data on LCD
mov dat,A  ; moving character to data port
setb en  ; setting bit enable for starting clock
nop  ; time delay
nop  ; time delay
clr en  ; clearing bit enable for ending clock
ret      ; return back

;---COMMAND DISPLAYING MODULE----

com_wrt:
acall delay  ; calling some time delay
clr rs  ; rs=0 for writting comment
clr rw  ; rw=0 for writting comment
mov dat,A  ; copying acumulator in data port (p2)
setb en  ; en=1 for clock
nop  ; time delay
nop  ; time delay
clr en  ; en=0 for clock
ret  ; return to previous position

;---DELAY MODULE----

delay:
mov r6,#088h
loop:
djnz r5,$
djnz r6,loop
ret

nline:
mov a,#0c1h  ;next line
call com_wrt   ; calling function write comment on LCD
mov a,#0c0h  ;2nd line 1st position
call com_wrt  ; calling function write comment on LCD
ret


msg: db'LCD INTERFACING ',0
msg1: db' WITH OUR BLOG',0

Exit:
jmp msg1

end




No comments:

Post a Comment