Assembly Program to Display a Scrolling Message on an LCD
How to Write an Assembly Program to Display a Scrolling Message on an LCD
Introduction
In this blog post, we will explore how to write an assembly program to display a scrolling message on an LCD. This guide will walk you through the logical steps, provide the complete assembly code, and show the output on an LCD screen. Whether you are a beginner or an experienced programmer, this tutorial will help you understand the basics of interfacing an LCD with a microcontroller and creating a scrolling text effect.
Logical Steps
-
Initialize the LCD:
- Set up the data direction registers.
- Configure the LCD in 4-bit or 8-bit mode.
- Send initialization commands to the LCD.
-
Define the Scrolling Message:
- Store the message in memory.
- Determine the length of the message.
-
Display the Message:
- Write a subroutine to send characters to the LCD.
- Create a loop to display each character of the message.
-
Scroll the Message:
- Use a delay to control the speed of scrolling.
- Shift the message left by updating the LCD display in a loop.
Program
; Assembly Program to Display a Scrolling Message on an LCD
ORG 0000H
; Define LCD control pins
RS EQU P2.0
RW EQU P2.1
EN EQU P2.2
; Define message
MESSAGE DB 'Hello, World! ' ; The spaces at the end are important for smooth scrolling
; Initialize the LCD
LCD_INIT:
MOV A, #38H ; Function set: 8-bit, 2 line, 5x7 dots
ACALL LCD_CMD
MOV A, #0EH ; Display on, cursor on
ACALL LCD_CMD
MOV A, #01H ; Clear display
ACALL LCD_CMD
MOV A, #06H ; Entry mode, auto increment with no shift
ACALL LCD_CMD
RET
; Send command to LCD
LCD_CMD:
MOV P1, A
CLR RS
CLR RW
SETB EN
CLR EN
ACALL DELAY
RET
; Send data to LCD
LCD_DATA:
MOV P1, A
SETB RS
CLR RW
SETB EN
CLR EN
ACALL DELAY
RET
; Delay subroutine
DELAY:
MOV R2, #255
D1: MOV R1, #255
D2: DJNZ R1, D2
DJNZ R2, D1
RET
; Main program
MAIN:
ACALL LCD_INIT
MOV DPTR, #MESSAGE
MAIN_LOOP:
MOV R0, #00H ; Initialize counter
MOV R1, #14 ; Length of the message + spaces
DISPLAY_LOOP:
CLR A
MOVC A, @A+DPTR
ACALL LCD_DATA
INC DPTR
DJNZ R1, DISPLAY_LOOP
ACALL SCROLL_DELAY
ACALL LCD_INIT
MOV DPTR, #MESSAGE
INC DPTR
SJMP MAIN_LOOP
; Scroll delay subroutine
SCROLL_DELAY:
MOV R2, #5
D3: MOV R1, #255
D4: DJNZ R1, D4
DJNZ R2, D3
RET
END
Output
When this program is loaded onto an 8051 microcontroller connected to an LCD, it will display the message "Hello, World!" scrolling from right to left across the screen.
Conclusion
In this tutorial, we have covered how to write an assembly program to display a scrolling message on an LCD. We started by initializing the LCD, defining the scrolling message, displaying the message, and finally creating the scrolling effect. By following these steps, you can create your own custom messages and display them on an LCD using assembly language.