Skip to main content

LCD interfacing with 8051 in assembly



Liquid Crystal Display also called as LCD is very helpful in providing user interface as well as for debugging purpose. The most common type of LCD controller is HITACHI 44780 which provides a simple interface between the controller & an LCD. 





These LCD's are very simple to interface with the controller as well as are cost effective.
The most commonly used ALPHANUMERIC displays are

  • 1x16 (Single Line & 16 characters),
  • 2x16 (Double Line & 16 character per line),
  • 4x20 (four lines & Twenty characters per line

Pin description of LCD: table show the pin description of LCD. 8 pins are required to send data on LCD and 3 pins are required to control the data/command and read/write operation

PinSymbolFunction
1VssGround
2VddSupply Voltage
3VoContrast Setting
4RSRegister Select
5R/WRead/Write Select
6EnChip Enable Signal
7-14DB0-DB7Data Lines
15A/VeeGnd for the backlight
16KVcc for backlight



Mode of operation: there are two modes of operation 8 bit and 4 bit.
  • 8 bit required 8 data lines to send data/command
  • 4 bit required only 4 pins and pin D4-D7 are connected to Vcc
Control pins: 3 pins RS,R/W and EN are control pins and used as
  • EN - the ENABLE pin is used to latch the data present on the data pins. A HIGH - LOW signal is required to latch the data. The LCD interprets and executes our command at the instant the EN line is brought low. If you never bring EN low, your instruction will never be executed.
  • R/W - when R/W is low , we are writing to LCD and when R/W is High we reading from LCD. Most of the times there is no need to read from the LCD so this line can directly be connected to GND thus saving one controller line.
  • RS -  When RS is low (0), the data is to be treated as a command. When RS is high (1), the data being sent is considered as text data which should be displayed on the screen.
DDRAM: character displayed on LCD are the data stored on Display Data RAM [ DDRAM ] and address of DDRAM for 2x16 LCD are varies from 80h to 8Fh for first line and 0C0h to 0CFh for second line.  So if we want to display 'N' on the 7th poistion of the first line then we will write it at location 87h.

To display data on LCD we first need to initialize the LCD by sending different command and then we send data to LCD

Initialization: table show to different value which are need to send on LCD when control pin RS is Low [ we are sending command ] and control pin EN goes High to Low [ show that we are sending some thing to LCD ]



  

"*" - Not Used/Ignored.  This bit can be either "1" or "0"
Clear display:
Send 1 to pins to clear display 

Set Cursor Move Direction:

ID - Increment the Cursor After Each Byte Written to Display if Set
S - Shift Display when Byte Written to Display  Enable Display/Cursor


Enable Display/Cursor: 

D - Turn Display D=High [ Display on] and D=Low  [ Display off ]
C - Turn Cursor C=High [ Cursor shown ] and c=Low [ Cursor hide ]
B - Cursor Blink B=High [ Blink on ] and B=Low [ blink off ]


Move Cursor/Shift Display

SC - Display Shift SC=High [ Shift on ] and SC=Low [ Shift off ] 
RL - Direction of Shift  RL=high [ Shift to right ] and RL=low [ shift to left ]

Set Interface Length

DL - Set Data Interface DL=1 [ 8 bit mode ] and DL=0 [ 4 bit mode ]
N - Number of Display Lines N=1 [ two line display ] and N=0 [ single line display ]
F - Character Font F=1 [ 5x10 font ] and F=0 [ 5x7 font ]
Poll the "Busy Flag" BF - This bit is set while the LCD is processing
Move Cursor to CGRAM/Display
A - Address
Read/Write ASCII to the Display
D - Data

so we send different code to LCD to get different characteristic like Display on/off, Cursor blink on/off, Cursor move direction etc
now lets write some code to initialize our LCD but before it we need two subroutine (function) to send command and data on LCD

our LCD is connected to port  P1 to send data

Send Data on LCD: 
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lcd_data:
               SETB RS          ;Telling the LCD that the data which is being send is to be displayed
               MOV P1, A       ;data to displayed is stored in A and now send to port P1
               SETB EN         ;EN is High
               ACALL delay    ;a short delay
               CLR   EN         ;EN is low to make a  high-low pulse 
               RET ; return to main
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++





 Send command to LCD:
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lcd_cmd: 
        CLR RS       ;Telling the LCD that the data is a command
        MOV P1,A     ; command is send to the port
        SETB EN      ; make EN high
        ACALL DELAY  ;a short delay
        CLR EN       ;EN  is low to make a higt-low pulse
        RET          ;return 
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



Initialize LCD:
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lcd_init:
           MOV A,#38h      ;telling the LCD that we use 8 bit mode,
                           ;2 line display and 5x7 font
                           ;#38h because  here  DL=1,N=1 and F=0 
                           ;in "set interface length" 
           ACALL lcd_cmd   ;sending command to LCD     
           MOV A,#0Eh      ;we use Display on,Cursor on and Cursor blinking off
                           ;#0Ch to Display on,Cursor off and Cursor blinking off 
           ACALL lcd_cmd   ;sending command to LCD
           MOV A,#06h      ; cursor position auto increment and move cursor to  
                           ;right #04 cursor moved to left
           ACALL lcd_cmd   ;sending command to LCD
           RET
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


  


Clear LCD: clear the lcd and move cursor to home position 
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
lcd_clr:
          MOV A,#01h         ;clear LCD
          ACALL lcd_cmd   s   ;sending command to LCD
          RET
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
          

l

now lets send to text to display LCD. we are sending "ElectroNets" to display .here we use simple method but you can also use DPTR to send a string to lcd. 
  if don't  know how to use DPTR, leave a command i will add in post.
;.........................................

;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
$mod51
RW EQU P2.0             ;change pins according your design 
RS EQU P2.1
EN EQU P2.2

ORG 0000H

CLR RW                     ;because we are writing to LCD
ACALL lcd_clr             ;clear display
ACALL lcd_init            ;initialize the LCD

MOV A,'E'                 ;here we sending a single character at a time and then call lcd_data to tell  
ACALL lcd_data         ;LCD that we sending some thing to display on LCD
MOV A,'l'
ACALL lcd_data
MOV A,'e'
ACALL lcd_data
MOV A,'c'
ACALL lcd_data
MOV A,'t'
ACALL lcd_data
MOV A,'r'
ACALL lcd_data
MOV A,'o'
ACALL lcd_data
MOV A,'N'
ACALL lcd_data
MOV A,'e'
ACALL lcd_data
MOV A,'t'
ACALL lcd_data
MOV A,'s'
ACALL lcd_data
;......................
all the subroutine goes here

;......................

END              ; end of code
;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

i use multisim to simulate my code. and this is the circuit that i used (crystal and capacitor not needed in multisim so i no connect ). if you face any error  a reply me.
i will attach .asm and multisim circuit file as soon as possible. thanx for reading......  



don't forget to check  LCD interfacing with 8051 in 4 bit mode

thanks madiha mahmood to point out the delay problem.
here is full working code, protues and keil project files.

Comments

  1. hi, that's an excelente post ...

    can you post te code of "Send command to LCD", isn't here.

    tks.

    ReplyDelete
  2. Glad to know you like it.
    the code is removed by blogger script, i will add it soon as possible because the original file is deleted from my system.

    ReplyDelete
  3. @/\/\/\/\/\/\/\ : i update code.. reply if you face any problem..

    ReplyDelete
  4. Hello! Can you send me the simulation file please! carlitos_castro87@hotmail.com

    Really nice post!

    ReplyDelete
  5. i'm Sorry CarlitosC, my computer is formatted due to a wrong software installation, all the files are deleted. I will send if I redo it soon.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. @madiha: in which section you getting error and paste your error here. i think it run perfectly when i tested it.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. do you use simulator (which one) or doing it one hardware.

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. ok, may be you got the error like "controller receiver command whilst busy" and same for lcd. this is due to insufficient delay between en=1 to en=0, (lcd process slow then controller). i will attach a zip file which include proteus ckt and .asm and .hex files.

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. fill are added, you can download now.

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. it is Proteus 7.6 SP0, i hope it works now
    (I also decide to write a tutorial in 4 bit mode it will be here soon)

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete
  21. This comment has been removed by the author.

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. sorry, i am busy little bit..
    the ckt file is in the download.
    and if still not work than rply me. or you can chat with me on facebook or gtalk..

    ReplyDelete
  25. glad to know that now you are able to interface lcd. :)

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. @madiha, @madhu - why did you remove all your comments.

    ReplyDelete
  29. how could you edit this code so there is also a line of characters appearing on the bottom part of the lcd.

    ReplyDelete
  30. Read again DDRAM topic in this post..
    you can you command 0c0h to print on second line

    ReplyDelete
  31. i need an assembly language of the same program but its display must be animated!!! plz help

    ReplyDelete

Post a Comment

Popular posts from this blog

Multiplex 4 Seven Segment Display With 8051 In Assembly

Multiplexing is very essential part while working with 8051 because of its limited number of ports. which offer 32 pins for connecting external devices. 32 seems large but when you working with seven segment, keypad, LCD, ADC etc. or if you project have many parts to operate simultaneously then you definitely need to multiplex some of ports.

LCD Interfacing with 8051 in 4 bit mode : assembly tutorial

Hello friends, in this tutorial we are going to interface LCD with 8051 microcontroller but in 4 bit mode. we already interfaced LCD with 8051 in 8 bit and also done scrolling of text in previous tutorials.

how to get 7805 in multisim - Replacement of 7805 in multisim

  finding some parts in multisim is really tough work because multisim  not provide all general purpose component like 7085 and other voltage regulator. one more  bad information is that manufacturer of these component also not provide any PSPICE because output of these component are predetermined and not changed according to the circuit.