• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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

KiroloesAmir

New Member
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
Code:
                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:
View attachment 20210612_223409.jpg


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

View attachment 20210612_223320.jpg
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Timing..... check the 50 microsecond delay as it looks like 5. Also you need at least 5ms between the init routines... You set only 1

BTW have you a thread on AAC as I just answered a VERY similar post there!!
 

Latest threads

EE World Online Articles

Loading
Top