Interfacing LCD with 8051

Here i will explain you all about interfacing LCD Displays.  The standard LCD Model HD44780 Character LCD  is the best. Although it was developed by Hitachi , now it is available from a number of other companies. The 16×2 LCD costs approx Rs80/-.

Character LCDs use a standard 14-pin interface and those with backlights have 16 pins. The pinouts are as follows:

  1. Ground
  2. VCC (+3.3 to +5V)
  3. Contrast adjustment (VO)
  4. Register Select (RS). RS=0: Command, RS=1: Data
  5. Read/Write (R/W). R/W=0: Write, R/W=1: Read
  6. Clock (Enable). Falling edge triggered
  7. Bit 0 (Not used in 4-bit operation)
  8. Bit 1 (Not used in 4-bit operation)
  9. Bit 2 (Not used in 4-bit operation)
  10. Bit 3 (Not used in 4-bit operation)
  11. Bit 4
  12. Bit 5
  13. Bit 6
  14. Bit 7
  15. Backlight Anode (+)
  16. Backlight Cathode (-)

Link to datasheet HD44780 16×2 character LCD

An online LCD simulator

An opensouce group

You can interface with a PC  Parallel port or microcontroller, but the algo is same for both the case.

  1. intialisation
  2. set mode command mode
  3. send command
  4. set mode to data mode
  5. send data … so on

if u don’t know about microcontrollers , start with parallel port here or ……  here.

The 8051 microcontroller has 4 ports, but the actual chip may have less or more number of ports. Choose 2  ports. one is for data(must have 8 pins), another for command/control  signals(at least 3 pins necessary). Connect the data lines (D0-D7) and the control lines RS, RW, EN to the PORTs you have choosed. Connect the Vcc(+ve) and Vss(-ve) and Vdd as shown (through a voltage divider arrangement by a pot). The Vdd used to adjust contrast.

interfacing LCD with 8051

LCD interfacing with 8051


Algorithm to send data to LCD:

1.Set R/W low

2.Set

  • RS=0 ;if data byte is command
  • RS=1 ;if data byte is data (ASCII value)

3.Place data byte on data register

4.Pulse E (HIGH to LOW)

5.Repeat the steps to send another data byte

LCD Initialization:

This is the pit fall for beginners.Proper working of LCD depend on the how the LCD is initialized. We have to send few command bytes to initialize the lcd. Simple steps to initialize the LCD

1.Specify function set:

Send 38H for 8-bit,double line and 5×7 dot character format.

2.Display On-Off control:

Send 0FH for display and blink cursor on.

3.Entry mode set:

Send 06H for cursor in increment position and shift is invisible.

4. Clear display:

Send 01H to clear display and return cursor to home position.

Assembly Code for interfacing LCD with 8051

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

; Interfacing LCD 16×2 in 4-bit mode.
; Port0 to higher nibble data pins of LCD
; Crystal 3.579545 MHz

; AT89C51

;P2.0 to RS pin

;P2.1 to Enable Pin

ORG 0000H
AJMP MAIN
ORG 0030H
MAIN:
MOV SP,#60H ;STACK POINTER
ACALL LCD_INIT ;Initialize lcd
MOV DPTR,#MESSAGE1
CALL LCD_STRING ;Display message on LCD
CALL NEXT_LINE ;Place cursor to;second Line
MOV DPTR,#MESSAGE2 
CALL LCD_STRING ;Display message on LCD
HERE: AJMP HERE

LCD_INIT: ;initialize LCD in 4-bit mode
ANL P0,#0F0H
CALL LOOP
MOV DPTR,#LCDCODE
MOV A,#0H
MOV R6,#0H
MOV R7,#0H
CLR P2.0 ;RS COMMAND
NEXT: ;8-bit code is split into two 4-bit nibbles.
INC R6
MOVC A,@A+DPTR
MOV R7,A
ANL A,#0F0H
SWAP A
ANL P0,#0F0H
ORL P0,A
ACALL ENABLE ;PULSE E sending first nibble
MOV A,R7
ANL A,#0FH
ANL P0,#0F0H
ORL P0,A
ACALL ENABLE ;PULSE E sending second nibble
MOV A,R6
CJNE R6,#09H,NEXT
RET

LCD_STRING:
MOV P0,#00H
SETB P2.0 ;RS DATA
MOV A,#00H
MOV R6,#00H
NC: ;checks the end of message string
MOVC A,@A+DPTR
CJNE A,#2FH,NC1
RET
NC1:
LCALL LCD_WRITE
INC R6
MOV A,R6
AJMP NC

LCD_WRITE:
SETB P2.0 ;RS DATA
CALL LO
RET

NEXT_LINE:
MOV P0,#00H
CLR P2.0 ;RS COMMAND
MOV A,#0C0H
CALL LO
RET

LCD_CLEAR: ;This Subroutine is used to clear LCD
CALL DELAYL
ANL P0,#00H
MOV A,#01H
CLR P2.0 ; RS command
LO: ;8-bit code is split into two 4-bit nibbles.
MOV R7,A
ANL A,#0F0H
SWAP A
ANL P0,#0F0H
ORL P0,A
CALL ENABLE
MOV A,R7
ANL A,#0FH
ANL P0,#0F0H
ORL P0,A
CALL ENABLE
RET

ENABLE: ;Give High-to-low pulse at enable pin
SETB P2.1
CALL DELAYL
CLR P2.1
CALL DELAYL
RET

;With respect to crystal frequency 3.579 MHz
DELAYL: ; 5ms DELAY
SETB PSW.4 ; SELECT BANK 2
MOV R7,#25
HDH:
MOV R6,#60
DJNZ R6,$
DJNZ R7,HDH
CLR PSW.4 ;DEFAULT BANK
RET

LOOP: ;1 SEC DELAY
MOV R7,#100
LOOP1:
CALL DELAYL
CALL DELAYL
DJNZ R7,LOOP1
RET

;LCD INITIALIZING CODE (DO NOT DISTURB THIS)

LCDCODE:
DB 02H
DB 02H
DB 02H
DB 28H
DB 28H
DB 28H
DB 0CH
DB 06H
DB 01H

;DATA TO BE DISPLAYED

;Maximum message length = 16 characters.

;To notify end of message place ‘/’ at the end.

MESSAGE1: DB “LCD INTERFACING /” ;Change Message1

MESSAGE2: DB ” IT IS EASY /” ;Change Message2

END

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

a

http://xtronix.in

mail me at asim@xtronix.in






Leave a Reply