Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/23/06 17:18
Modified:
  06/23/06 17:40

Read: times


 
#118959 - LCD 2 line example code
Posted with the intention that it may help someone with the seemingly common second line problem

(for example http://www.8052.com/forum/thr...eNumber=1)

This is based on Craigs LCD tutorial code.

;*************************************************************************************
;  		LCD Test routines
;		23/06/2006
;
;*************************************************************************************



;  		Primary controls

$MOD252              
$TITLE(BYTE LCD Test)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING
;
;


;*************************************************************************************
;  Variable declarations and equates.
;


	EN 	EQU P2.6
	RS 	EQU P2.4
	RW 	EQU P2.5
	DAT 	EQU P1			;LCD connected to port 1
	
;*************************************************************************************


	org	00H
	
	cseg
	
		
INIT:

	
	lcall INIT_LCD

	lcall CLEAR_LCD
	
	
MAIN:	

	mov a, #'H'
	lcall WRITE_TEXT		;WRITE_TEXT routine
	mov a, #'e'			;writes contents of
	lcall WRITE_TEXT		;acc to LCD
	mov a, #'l'
	lcall WRITE_TEXT
	mov a, #'l'
	lcall WRITE_TEXT
	mov a, #'o'
	lcall WRITE_TEXT
	
	SETB EN
	CLR RS
	MOV DAT,#0C4h 			;Move to line 2 pos 4
	CLR EN
	lcall WAIT_LCD

	mov a, #'W'
	lcall WRITE_TEXT
	mov a, #'o'
	lcall WRITE_TEXT
	mov a, #'r'
	lcall WRITE_TEXT
	mov a, #'l'
	lcall WRITE_TEXT
	mov a, #'d'
	lcall WRITE_TEXT




	jmp $				;Sit and stay

			
;******************************************************************************
;
;			Subroutines
;
;******************************************************************************
	
;*************************************************************************************
;	Wait for LCD Subroutine

WAIT_LCD:
	setb EN 			;Start LCD command
	clr RS 				;It's a command
	setb RW 			;It's a read command
	mov DAT,#0FFh 			;Set all pins to FF initially
	mov A,DAT 			;Read the return value
	jb ACC.7,WAIT_LCD 		;If bit 7 high, LCD still busy, so wait until it's finished
	clr EN 				;Finish the command
	clr RW 				;Turn off RW for future commands
RET
;*************************************************************************************

;*************************************************************************************
;	Clear LCD subroutine

CLEAR_LCD:
	setb EN
	clr RS
	mov DAT,#01h			;Clear LCD command
	clr EN
	lcall WAIT_LCD
RET
;*************************************************************************************

;*************************************************************************************
;	Initialise LCD subroutine. 
;
INIT_LCD:
	lcall WAIT_LCD			;Make sure the LCD is ready before we send anything to it
	setb EN
	clr RS
	mov DAT,#38h			;bit 4 = 8-bit I/F, bit 3 = 2-line
	clr EN
	lcall WAIT_LCD
	setb EN
	clr RS
	mov DAT,#0Fh			;Display on, blinking cursor
	clr EN
	lcall WAIT_LCD
	setb EN
	clr RS
	mov DAT,#06h			;Increment cursor
	clr EN
	lcall WAIT_LCD
RET
;*************************************************************************************

;*************************************************************************************
;	Write text subroutine
;	acc contains character to be written on entry


WRITE_TEXT:
					
	setb EN
	setb RS
	mov DAT,A
	clr EN
	lcall WAIT_LCD
RET

;*************************************************************************************


END


The crucial thing seems to be waiting on the LCD to come ready (first line of INIT_LCD) before sending any initialisation commands to it. This avoids (on both different LCD's I've tried) having to send the Function Set command 2 or 3 times.

It would be great if someone else could try this to confirm it works with their LCD too.

List of 8 messages in thread
TopicAuthorDate
LCD 2 line example code            01/01/70 00:00      
   post such things to the code library            01/01/70 00:00      
   Assumes you can read from the LCD            01/01/70 00:00      
      Apparently not...            01/01/70 00:00      
         Second Try            01/01/70 00:00      
            not at all            01/01/70 00:00      
               Shouldn't you be home by now?            01/01/70 00:00      
   Also, in the interests of saving CPU            01/01/70 00:00      

Back to Subject List