Advertisement

Assembly Code to Interface LCD with 8051 Microcontroller

LCD_interfacing

LCD(Liquid Crystal Display) screen is such a display module and a 16×2 LCD module is very commonly used. These modules are replacing seven segments and other multi segment LEDs for these purposes. The reasons being: LCDs are economical, easily programmable, have no limitation of displaying special & even custom characters (unlike in seven segments), animations and so on. LCD can be easily interfaced with a microcontroller to display a message or status of a device. This topic explains the basics of a 16×2 LCD and how it can be interfaced with AT89C51 to display a character.

LCD Pins Description:
A 16×2 LCD means it can display 16 characters per line and there are 2 such lines. In this LCD each character is displayed in 5×7 pixel matrix.

lcd-pins

lcd-pins

Now we are familiar with the pins of the LCD as well as the commands to the instruction registers of the LCD. Now let us discuss some basic codes which will be required whenever we want to interface LCD with the microcontroller.
An LCD interfaced to microcontroller looks like:
lcd-interface
LCD Interfacing Code Segments:

LCD Initialization Code:
First code we are going to discuss is the LCD initialization code. In this code segment different hex codes (table given above) are passed to the LCD instruction register to properly initialize the LCD.

INITIALIZE_LCD:

MOV A, #38H
LCALL WRITE_CMD
MOV A, #0CH
LCALL WRITE_CMD
MOV A, #06H
LCALL WRITE_CMD
RET

SendingCommand to LCD:

When we want to send some command to LCD we use following code segment.

WRITE_CMD:

CLR RS
MOV P1, A
SETB EN
CLR EN
LCALL DELAY_2ms
RET

When we want to send some command to LCD we set RS pin equal to zero. After clearing the RS pin we move the command to the port of the microcontroller. Next we toggle the EN pin to move the command from the port the LCD.

Sending Character to LCD:

Following code serves the purpose of sending character to LCD.

WRITE_CHAR:

SETB RS
MOV P1, A
SETB EN
CLR EN
LCALL DELAY_2ms
RET

When we want to send some character to LCD we set RS pin equal to one. After setting the RS pin we move the data to the port of the microcontroller. Next we toggle the EN pin to move the data from the port the LCD.

Writing String to LCD:

The following code serves the purpose of writing a string to LCD.

WRITE_STRING:

MOV R1, #0
REPEAT: MOV A, R1
MOVC A,@A+DPTR
JZ EXIT
LCALL WRITE_CHAR
INC R1
SJMP REPEAT
EXIT:RET

In the above code segment we first move value of Zero to R1 and then we move the value from R1 to Accumulator. Next using MOVC command we calculate the address. JZ commands moves the control to the label exit when the value in the accumulator is zero. Next we call Write character function to write it to LCD. Then we increment R1 and this loop continues so on.

Clearing the LCD:

The following code serves the purpose of clearing the LCD.

CLR_LCD:

MOV A, #01H
LCALL WRITE_CMD
RET

Moving To A Particular Address Of LCD:
The following code provides us the service to move to a particular address of the LCD.

GOTO_ADDR:
ADD A, #80H
LCALL WRITE_CMD

Here you go.

Comments

  1. By Nikkie

    Reply

    • By admin

      Reply

  2. By Raf

    Reply

    • By Admin

      Reply

  3. By SAKSHAMA GHOSLYA

    Reply

    • By Aamir Arsalan (Admin)

      Reply

  4. By Marry

    Reply

    • By Aamir Arsalan (Admin)

      Reply

  5. By melissa

    Reply

    • By Aamir Arsalan (Admin)

      Reply

  6. By sid

    Reply

    • By Aamir Arsalan (Admin)

      Reply

  7. By sruthi k

    Reply

    • By Aamir Arsalan (Admin)

      Reply

  8. By aanand

    Reply

    • By Aamir Arsalan (Admin)

      Reply

  9. By usman

    Reply

    • By Aamir Arsalan (Admin)

      Reply

  10. By Tania

    Reply

  11. By Tania

    Reply

  12. By vikash selwal

    Reply

    • By Aamir Arsalan (Admin)

      Reply

  13. By Ahmad hassan

    Reply

    • By Aamir Arsalan (Admin)

      Reply

Leave a Reply