You are on page 1of 17

; LCD Display Driver Demo. ; Timing code assumes 1.

2MHz Clock

#include <bitdef.h> #include <io51.h>

;LCD Registers addresses LCD_CMD_WR equ LCD_DATA_WR equ LCD_BUSY_RD equ LCD_DATA_RD equ 0 1 2 3

;LCD Commands LCD_CLS LCD_HOME equ equ 2 4 8 16 equ equ equ 32 64 128 1

LCD_SETMODE equ LCD_SETVISIBLE equ LCD_SHIFT equ

LCD_SETFUNCTION LCD_SETCGADDR LCD_SETDDADDR

;Reset vector org 0000h jmp start

;Start of the program org 0100h

string1a:db ' !! A M A Z I N G !! ' db 0 string1b:db '!! A M A Z I N G !! ' db 0 string2:db ' A virtual LM032L... ' db 0

string3:db ' driven by a virtual ' db 0 string4:db ' 8051 processor!' db 0

start:

mov A,#038h call wrcmd

loop:

mov A,#LCD_SETVISIBLE+6 call wrcmd

;Make the display & blink visible:

mov R7,#2 loop2:

mov DPTR,#string1a call wrstr

mov DPTR,#200 call wtms

mov A,#LCD_CLS call wrcmd

;Clear screen

mov DPTR,#string1b call wrstr

mov DPTR,#200 call wtms

mov A,#LCD_CLS call wrcmd

;Clear screen

djnz R7,loop2

mov DPTR,#string1a call wrstr

mov DPTR,#400 call wtms

mov A,#LCD_SETDDADDR+64 call wrcmd

mov DPTR,#string2 call wrslow

mov DPTR,#200 call wtms

mov A,#LCD_CLS call wrcmd

;Clear screen

mov DPTR,#string3 call wrslow

mov A,#LCD_SETDDADDR+64 call wrcmd

mov DPTR,#string4 call wrslow

mov A,#LCD_SETVISIBLE+7 call wrcmd

;Show the blink cursor as well.

mov DPTR,#2000 call wtms

mov A,#LCD_CLS call wrcmd

;Clear screen

jmp loop

;Sub routine to write null terminated string at DPTR in program ram. wrstr: mov R0,#LCD_DATA_WR wrstr1: clr A movc A,@A+DPTR jz wrstr2 movx @R0,A call wtbusy inc DPTR push DPL push DPH pop DPH pop DPL jmp wrstr1 wrstr2: ret

;Sub routine to write null terminated string at DPTR in program ram. Slowly

wrslow: mov R0,#LCD_DATA_WR wrslw1: clr A movc A,@A+DPTR jz wrslw2 movx @R0,A call wtbusy inc DPTR push DPL push DPH mov DPTR,#100 call wtms pop DPH pop DPL jmp wrslw1 wrslw2: ret

;Sub routine to write command: wrcmd: mov R0,#LCD_CMD_WR movx @R0,A jmp wtbusy

;Sub routine to write character: wrchar: mov R0,#LCD_DATA_WR

movx @R0,A

;Subroutine to wait for busy clear wtbusy: mov R1,#LCD_BUSY_RD movx A,@r1 jb ACC.7,wtbusy ret

;Wait for number of seconds in A wtsec: push ACC call wtms pop ACC dec A jnz wtsec ret

;Wait for number of milliseconds in DPTR wtms: xrl DPL,#0FFh xrl DPH,#0FFh inc DPTR wtms1: mov TL0,#09Ch mov TH0,#0FFh mov TMOD,#1 setb TCON.4 wtms2: jnb TCON.5,wtms2 ;Timer 0 mode 1 ;Timer 0 runs ;100 ticks before overflow = 1ms at 1.2MHz Clock ;Can't do DEC DPTR, so do the loop by forming 2's complement ;and incrementing instead.

clr TCON.4 clr TCON.5 inc DPTR mov A,DPL orl A,DPH jnz wtms1 ret

;Timer 0 stops

END

1. $MOD51 ; instructions for assembler 2. 3. ; variables 4. ARED equ P1.0 5. AYEL equ P1.1 6. AGRE equ P1.2 7. BRED equ P1.3 8. BYEL equ P1.4 9. BGRE equ P1.5 10. 11. ORG 0000H 12. LJMP BEGGIN 13. ORG 0030H 14. 15. BEGGIN: 16. LCALL CLEARALL 17. SJMP MAIN 18. 19. MAIN: 20. LCALL SIT1 21. LCALL SIT3 22. LCALL SIT2 23. LCALL SIT4 24. SJMP MAIN 25. 26. ; the four possible situations 27. SIT1: 28. SETB ARED 29. SETB BGRE 30. MOV R0, #0AH ; 10 seconds 31. LCALL WAIT 32. LCALL CLEARALL 33. RET

Assembly Language program for LCD code


In this post some routine in Assembly language are written to interface lcd with microcontroller 8051.Most projects you create with the 8051 CPU or any other microcontroller require some form ofdisplay. The display may be LED or LCD. But now use of LCD is increasing/ The most common way to accomplish this is with the LCD (Liquid Crystal Display). LCDs have become a cheap and easy way to get text display for an embedded system Common displays are set up as 16 to 20 characters by 1 to 4 lines. variate of LCD are available and most of them can work on thees ways describes in the sub routines. Data to be Displayed on LCD using micro controller requires following set of instructions in the form of code as follows.

lcd_datadisplay: // this is a part of main program call sub routine, the function of
this sub routine is to display //data on lcd. SETB RS //Telling the LCD that the data which is being send is to be displayed MOV P1,A //Character to be displayed is in Acc SETB EN // the enable pin of lcd is set to logic one CLR EN //High to Low pulse on EN to latch the data CALL DELAY //Delay so that LCD finishes its internal operations ret // return from this routine is called

Command or Special Instruction.


lcd_command: // in this sub-routine of lcd program, how different commands are sent to lcd is shown CLR RS //Telling the LCD that the data which is being send is a command MOV P1,A //Character to be displayed is in Acc SETB EN CLR EN //High to Low pulse on EN to latch the data CALL DELAY //Delay so that LCD finishes its internal operations ret // returning to the main code from where this sub-routine was called

Busy flag checking


ready: // In this sub-routine the busy flag is checked setb P1.7 ;D7 as input clr P3.6 ;RS=0 cmd setb P3.5 ;RW=1 for read again: setb P3.7 ;H->L pulse on E clr P3.7 jb P1.7, again ret // returning back to main program where this function is called

Data write Routine


data: // in this sub-routine how data will be written on lcd is shown mov P1, A ;//move acc. data to port setb P3.6 ;RS=1 data clr P3.5 ;RW=0 for write setb P3.7 ;H->L pulse on E clr P3.7

lcall ready ret // end of this sub-routine

Command write Routine


command: // this is another sub-routine showing how to write commands on lcd, you can use any of these //cammand subroutine mov P1, A ;move acc. data to port clr P3.6 ;RS=0 for cmd clr P3.5 ;RW=0 for write setb P3.7 ;H->L pulse on E clr P3.7 lcall ready ret

Initialization of LCD
initialization: // in this sub-routine initialization of LCD is described mov A, #38H ; Initialize, 2-lines, 5X7 matrix. lcall Command mov A, #0EH ; LCD on, cursor on lcall Command mov A, #01H ; Clear LCD Screen lcall Command mov A, #06H ; Shift cursor right lcall Command

Display clear
clear: // this is sub-routine to clear the display of LCD setb p3.7 ;enable EN clr 3.6 ;RS=0 for cmd. mov DATA,#01h clr p3.7 ;disable EN lcall ready RET Note- As we need to clear the LCD frequently and not the whole initialisation , it is better to use this routine separately.

Displaying "HI Friends"


lcall initialization lcall clear mov A,#'H' acall data mov A,#'I' lcall data

mov A,#'F' lcall data mov A,#'r' lcall data lcall data mov A,#'i' lcall data mov A,#'e' lcall data mov A,#'n' lcall data mov A,#'d' lcall data mov A,#'s' ret

Now following is complete ASSEMBLY LANGUAGE code to show something on LCD


lcall Initialization lcall clear mov a,#'H' lcall data mov a,#'I' lcall data mov a,#8ah lcall command mov a,#'8' lcall data mov a,#'0' lcall data mov a,#'5' lcall data mov a,#'1' lcall data mov a,#'M' lcall data mov a,#'C' lcall data end

Program.
MOV A,#38H // Use 2 lines and 5x7 matrix ACALL CMND MOV A,#0FH // LCD ON, cursor ON, cursor blinking ON ACALL CMND MOV A,#01H //Clear screen ACALL CMND MOV A,#06H //Increment cursor ACALL CMND MOV A,#82H //Cursor line one , position 2 ACALL CMND MOV A,#3CH //Activate second line ACALL CMND MOV A,#49D ACALL DISP MOV A,#54D ACALL DISP MOV A,#88D ACALL DISP MOV A,#50D ACALL DISP MOV A,#32D ACALL DISP MOV A,#76D ACALL DISP MOV A,#67D

ACALL DISP MOV A,#68D ACALL DISP

MOV A,#0C1H //Jump to second line, position 1 ACALL CMND MOV A,#67D ACALL DISP MOV A,#73D ACALL DISP MOV A,#82D ACALL DISP MOV A,#67D ACALL DISP MOV A,#85D ACALL DISP MOV A,#73D ACALL DISP MOV A,#84D ACALL DISP MOV A,#83D ACALL DISP MOV A,#84D ACALL DISP MOV A,#79D ACALL DISP MOV A,#68D ACALL DISP MOV A,#65D ACALL DISP MOV A,#89D ACALL DISP HERE: SJMP HERE CMND: MOV P1,A CLR P3.5 CLR P3.4 SETB P3.3 CLR P3.3 ACALL DELY RET;

DISP:MOV P1,A SETB P3.5 CLR P3.4 SETB P3.3 CLR P3.3 ACALL DELY RET; DELY: CLR P3.3 CLR P3.5 SETB P3.4 MOV P1,#0FFh SETB P3.3 MOV A,P1 JB ACC.7,DELY CLR P3.3 CLR P3.4 RET;
END

http://www.circuitstoday.com/interfacing-16x2-lcd-with-8051

You might also like