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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/23/12 06:49
Read: 853 times


 
#186828 - LCD 4 BIT
Responding to: Andy Neil's previous message
I was using my phone yesterday so couldnt post my code. I had to change alot of it and im sending a string but it works anyway.The wait LCD label is redundant,better off using a delay routine but I was simulating on multism so didnt include it.
...$MOD51	; This includes 8051 definitions for the Metalink assembler

; Please insert your code here.



; Please insert your code here.

  lcd_port equ P3         ;LCD connected to Port3
		DB4 equ p3.0
		DB5 equ p3.1
		DB6 equ p3.2
		DB7 equ p3.3
        EN equ P3.7            ;Enable connected to P3.7
        RS equ P3.5           ;Register select to P3.5
       	RW equ p3.6
		temp equ 50h
		delay equ 51h
lcd_init:
        acall lcd_reset         ;Call LCD Reset sequence
        mov a,#28H              ;4-bit, 2 line, 5x7 dots
        acall lcd_cmd           ;Call LCD command
        mov a,#0CH              ;Display ON cursor OFF
        acall lcd_cmd           ;Call LCD command
        mov a,#06H              ;Set entry mode (Auto increment)
        acall lcd_cmd           ;Call LCD command
        mov a,#80H              ;Bring cursor to line 1
        acall lcd_cmd           ;Call LCD command
        jmp main

lcd_reset:                  ;LCD reset sequence
        mov lcd_port, #0FFH
                   ;20mS delay
        acall delayms
        mov lcd_port, #83H      ;Data = 30H, EN = 1, First Init
        mov lcd_port, #03H      ;Data = 30H, EN = 0
                ;Delay 15mS
        acall delayms
        mov lcd_port, #83H      ;Second Init, Data = 30H, EN = 1
        mov lcd_port, #03H      ;Data = 30H, EN = 0
                ;Delay 5mS
        acall delayms
        mov lcd_port, #83H      ;Third Init
        mov lcd_port, #03H
                 ;Delay 5mS
        acall delayms
        mov lcd_port, #82H      ;Select Data width (20H for 4bit)
        mov lcd_port, #02H      ;Data = 20H, EN = 0
                 ;Delay 5mS
        acall delayms
        ret


 






		
		

		



          ;program////////////////////////////////////////////////////
main:  	

	     			
          LCALL CLEAR_LCD		;clearing lcd.
        mov dptr,#my_string
		ACALL LCD_sendstring
		lcall line2
		 mov dptr,#my_string1
		ACALL LCD_sendstring
		LCALL CLEAR_LCD
		

		
	
          ;////////////////////////////////////////////////////////////







WAIT_LCD: 	ret				;;  Something wrong with this sub
          CLR  EN            ;bring EN low after every instruction
          CLR  RS            ;It's a command
          SETB RW            ;it's a read command
        ;;  MOV  dta, #0FFH   ;Set all pins(P1) High initially
          SETB EN            ;Clock Out Command To LCD
        ;;  MOV  A, dta       ;Read the return value
        ;;  JB   ACC.7, WAIT_LCD     ;if bit 7 high, LCD Still busy
          CLR  EN            ;Finish the command
          CLR  RW            ;turn off RW for future commands
          RET                ;return



lcd_cmd:
        mov temp,a          ;Save a copy of command to temp
        swap a                ;Swap to use higher nibble
        anl a,#0FH            ;Mask the first four bits
        add a,#80H            ;Enable = 1, RS = 0
        mov lcd_port,a        ;Move it to lcd port
        anl a,#0FH            ;Enable = 0, RS = 0
        mov lcd_port,a        ;Move to lcd port

        mov a,temp          ;Reload the command from temp
        anl a,#0FH            ;Mask first four bits
        add a,#80H            ;Enable = 1
        mov lcd_port,a        ;Move to port
        anl a,#0FH            ;Enable = 0
        mov lcd_port,a        ;Move to lcd port

        
        ret


 
CLEAR_LCD:
 CLR   RS
 MOV   A,#01h
 LCALL lcd_cmd ;Write A as two separate nibbles to LCD
 LCALL WAIT_LCD
 RET





line1:		CLR   RS
 MOV   A,#80h
 LCALL lcd_cmd ;Write A as two separate nibbles to LCD
 LCALL WAIT_LCD
 RET


line2:	CLR   RS
 MOV   A,#0C0h
 LCALL lcd_cmd ;Write A as two separate nibbles to LCD
 LCALL WAIT_LCD
 
			ret

lcd_dat:                  ;LCD data Routine
        mov temp,a            ;Keep copy of data in temp
        swap a                ;We need higher nibble
        anl a,#0FH            ;Mask first four bits
        add a,#0A0H           ;Enable = 1, RS = 1
        mov lcd_port,a        ;Move to lcd port
        nop
        clr EN               ;Enable = 0

        mov a,temp            ;Reload the data from temp
        anl a,#0FH            ;we need lower nibble now
        add a,#0A0H           ;Enable = 1, RS = 1
        mov lcd_port,a        ;Move to lcd port
        nop
        clr EN               ;Enable = 0

        mov delay,#1          ;Delay 1mS
        acall delayms
        ret

LCD_sendstring:
         clr   a                 ;clear Accumulator for any previous data
         movc  a,@a+dptr         ;load the first character in accumulator
         jz    exit              ;go to exit if zero
         acall lcd_dat      ;send first char
         inc   dptr              ;increment data pointer
         sjmp  LCD_sendstring    ;jump back to send the next character
		ret

exit:	ret

delayms: ret

my_string:  DB  'LCD Tutorial',00H
my_string1:  DB  'LCD Tutorial',00H



END
 



List of 13 messages in thread
TopicAuthorDate
4 Bit LCD      Sean O Mara      03/22/12 12:10      
   I am no sherlock Holmes, but ...      Erik Malund      03/22/12 13:31      
      LCD      Sean O Mara      03/22/12 13:56      
         Did you think about accesses using DATA1?      Per Westermark      03/22/12 15:41      
            lcd      Sean O Mara      03/22/12 17:18      
               So you found a solution ...      Andy Neil      03/22/12 18:02      
                  LCD 4 BIT      Sean O Mara      03/23/12 06:49      
                     where did you get that idea?      Erik Malund      03/23/12 06:57      
                        Using Read Cycle to LCD      Michael Karas      03/23/12 10:33      
                           of course, you can not use a signal that is not there      Erik Malund      03/23/12 10:45      
                        busy flad check      Sean O Mara      03/23/12 16:02      
                           Why not solve your problem BEFORE trying to save pins?        Richard Erlacher      03/24/12 08:16      
                              Premature optimisation      Andy Neil      03/24/12 09:48      

Back to Subject List