4-bit mode of LCD-1602 using 8052 µc

Thread Starter

KiroloesAmir035

Joined Jan 6, 2021
5
I try writing "NO" in the LCD using 4-bit mode. So, I programmed the AT89S52 to send the upper nibble first to the last 4 data inputs of the LCD. I used AND operation to eliminate the lower nibble. Afterwards, I cleared the RS bit and send a HIGH-TO-LOW enable pulse. The same code for the lower nibble but I added Swapping the nibbles of the data byte. The problem is that the LCD writes a strange character, then it clears it. when I press the reset button many times, the string appears in the LCD. I uploaded the following assembly code to the AT89S52:
The crystal value is 24MHz

4-BIT LCD:
                ORG 00H
                RS BIT P2.7
                E BIT P2.6
                MOV SP, #25H
                MOV P1, #00H
                MOV P2, #00H
                MOV TMOD, #01H
;;;;;;;;;;;;;;;;;;;;;;;;;;;COMMANDS;;;;;;;;;;;;;;;;;;;;;;;;;;;
                MOV R1, #0C8H
                ACALL DELAY_MS
               
                MOV R0, #30H            ;8-BIT INITIALIZATION
                CLR RS                    ;COMMAND MODE
                SETB E
                ACALL DELAY_MS
                CLR E
                ACALL DELAY_MS
                MOV R1, #04H            ; 4MS DELAY
                ACALL DELAY_MS
               
                MOV R0, #20H            ;4-BIT WITH 1 LINE INITIALIZATION
                CLR RS                    ;COMMAND MODE
                SETB E
                ACALL DELAY_MS
                CLR E
                ACALL DELAY_MS
                MOV R1, #05H            ;50 MICROSECONDS DELAY
                ACALL DELAY_US
               
                MOV R0, #28H            ;4-BIT MODE OF LCD INITIALIZATION
                ACALL CMD              
                MOV R1, #05H            ;50 MICROSECONDS DELAY
                ACALL DELAY_US
   
                MOV R0, #0EH            ;DISPLAY ON, CURSOR ON
                ACALL CMD              
                MOV R1, #05H            ;50 MICROSECONDS DELAY
                ACALL DELAY_US
               
               
                MOV R0, #06H            ;ENTRY MODE
                ACALL CMD              
                MOV R1, #05H            ;50 MICROSECONDS DELAY
                ACALL DELAY_US
               
                MOV R0, #01H            ;CLEAR LCD
                ACALL CMD
                MOV R1, #02H            ;2 MS DELAY
                ACALL DELAY_MS
               
                MOV R0, #80H            ;HOME CURSOR
                ACALL CMD
                MOV R1, #02H            ;2 MS DELAY
                ACALL DELAY_MS
                MOV R1, #015H            ;200 MS DELAY
                ACALL DELAY_MS
;;;;;;;;;;;;;;;;;;;;;;;;;;;PRINTING;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
               
                MOV R0, #'N'
                ACALL PRNT
                MOV R1, #02H
                ACALL DELAY_MS
                MOV R0, #'O'
                ACALL PRNT
                MOV R1, #02H
                ACALL DELAY_MS
               
;;;;;;;;;;;;;;;;;;;;;;;;;;;NO LOOPING;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
               
HERE:            SJMP HERE

;;;;;;;;;;;;;;;;;;;;;;;;;;;COMMAND FUNCTION;;;;;;;;;;;;;;;;;;;;;;;;;;

CMD:          
;SENDING THE UPPER NIBBLE
                MOV A, R0
                ANL A, #0F0H
                MOV P1, A
               
                CLR RS                    ;COMMAND MODE
                SETB E
                ACALL DELAY_MS
                CLR E
                ACALL DELAY_MS
;SENDING THE LOWER NIBBLE
                MOV A, R0
                ANL A, #0FH
                SWAP A
                MOV P1, A
                CLR RS
                SETB E
                ACALL DELAY_MS
                CLR E
                ACALL DELAY_MS
                RET
               
;;;;;;;;;;;;;;;;;;;;;;;;;;;PRINT FUNCTION;;;;;;;;;;;;;;;;;;;;;;;;;;;              
               
PRNT:
;SENDING THE UPPER NIBBLE
                MOV A, R0
                ANL A, #0F0H
                MOV P1, A
               
                SETB RS                    ;DATA MODE
                SETB E
                ACALL DELAY_MS
                CLR E
                ACALL DELAY_MS
;SENDING THE LOWER NIBBLE
                MOV A, R0
                ANL A, #0FH
                SWAP A
                MOV P1, A
                SETB RS
                SETB E
                ACALL DELAY_MS
                CLR E
                ACALL DELAY_MS
                RET
               
;;;;;;;;;;;;;;;;;;;;;;;;;;;;DELAY FUNCTION;;;;;;;;;;;;;;;;;;;;;;;;;;              
               
DELAY_MS:          
                MOV TH0, #0F8H
                MOV TL0, #30H
                SETB TR0
LOOP:            JNB TF0, LOOP
                CLR TR0
                CLR TF0
                DJNZ R1, DELAY_MS
                RET
               
               
               
DELAY_US:        MOV TH0, #0FFH
                MOV TL0, #0ECH
                SETB TR0
LOOP1:            JNB TF0, LOOP1
                CLR TR0
                CLR TF0
                DJNZ R1, DELAY_US
                RET
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;              
               
                END
this is the circuit of the LCD:

Simulation2.jpg


a pic of what the LCD writes after it's powered:
20210612_223409.jpg



a pic of what the LCD writes when the reset button is pressed many times:

20210612_223320.jpg
 

JohnInTX

Joined Jun 26, 2012
4,787
Welcome to AAC!

I don't think you are writing your init codes to the port:
MOV R0, #30H ;8-BIT INITIALIZATION R0 is not the output port
CLR RS ;COMMAND MODE
SETB E
ACALL DELAY_MS You are calling DELAY_MS with no time parameter in R1
CLR E
ACALL DELAY_MS You are calling DELAY_MS with no time parameter in R1
MOV R1, #04H ; 4MS DELAY
ACALL DELAY_MS

Review the init sequence for the display. You have to send 3h as a 4 bit nibble THREE times then send 2h ONCE before moving on to the normal two nibble commands. See page 20 of the attached spec. It's a for a clone controller but easier to understand than the original Hitachi datasheet.

Fix that and see what happens. There may be more things to do.
Good luck!
 

Attachments

Top