Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

4-bit LCD interface not working with pic18f4220

Status
Not open for further replies.

shibendu.mahata

Newbie level 3
Joined
Jun 3, 2009
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,351
Hi friends, I am trying to send a string "GREAT" to the JHD162A LCD operating in 4-bit mode, interfaced with pic18f4220 uC through PORTB<7:4>.PORTB<2> and PORTB<3> are the RS and E resp. and RW is permanently grounded.However, the LCD doesn't display anything except 16 squares in the first line. I have used MPLAB for the coding, with the uC operated at 4MHz. I have tried sending single character as well but to no avail.Can you kindly tell me where the initialization of the LCD is wrong.


;RB2=RS,RB3=E
#INCLUDE<P18f4220.INC>

CONFIG OSC=XT
CONFIG WDT=OFF
CONFIG BOR=OFF
CONFIG CCP2MX=ON
CONFIG PBAD=DIG
CONFIG FSCM=OFF
CONFIG IESO=OFF

TEMP EQU 0X00
TEMP1 EQU 0X01

ORG 0

BRA MAIN

MAIN:ORG 0X20
AGAIN:CALL DELAY
MOVLW 0X0F
MOVWF ADCON1
CLRF TRISB
CLRF TRISC
CLRF TRISD
CLRF PORTB
CLRF PORTC
SETF PORTD

BCF PORTB,3

MOVLW 0X30
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X30
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X30
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X20
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT


MOVLW 0X28
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X28
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X28
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X0E
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X01
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X06
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X84
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW A'G'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT

MOVLW A'R'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT

MOVLW A'E'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT

MOVLW A'A'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT

MOVLW A'T'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT
GOTO AGAIN
CMDWT:MOVFF TEMP1,WREG
ANDLW 0X0F0
MOVWF PORTB
BCF PORTB,2
BSF PORTB,3

CALL DELAY

BCF PORTB,3

MOVFF TEMP,WREG
ANDLW 0X0F
MOVWF TEMP
RLNCF TEMP
RLNCF TEMP
RLNCF TEMP
RLNCF TEMP

CALL DELAY

MOVFF TEMP,PORTB
BCF PORTB,2
BSF PORTB,3

CALL DELAY

BCF PORTB,3
RETURN


DATWT:MOVF TEMP1,W
ANDLW 0X0F0
MOVWF PORTB
BSF PORTB,2;;;;;;;;;
BSF PORTB,3
CALL DELAY
BCF PORTB,3

MOVFF TEMP,WREG
ANDLW 0X0F
MOVWF TEMP
RLNCF TEMP
RLNCF TEMP
RLNCF TEMP
RLNCF TEMP

CALL DELAY

MOVFF TEMP,PORTB
BSF PORTB,2;;;;;;;;;;;;;;;
BSF PORTB,3
CALL DELAY
BCF PORTB,3

RETURN

DELAY:BCF INTCON,TMR0IF
MOVLW 0X00
MOVWF TMR0H
MOVWF TMR0L
MOVLW B'10010111'
MOVWF T0CON
LOOP:BTFSS INTCON,TMR0IF
BRA LOOP
BCF INTCON,TMR0IF
RETURN

;GOTO AGAIN
END
 

I may be wrong, but if you're using the 4-bit interface, I'm sure you should be, at one point, swapping the upper four bits with the lower four bits during the sending of the instruction/character to the LCD.

Another very typical problem is the delay used to wait for the LCD to acknowledge the command(s) and react to them. The first delay (after the first command being issued) should be quite lengthy and the others somewhat shorter (the datasheet usually says how much it needs).
 

Can you kindly tell me where the initialization of the LCD is wrong.

you can learn more about what is wrong if you look at the datasheet.
 

Thanks jumper2high and millwood for your reply..I've cosulted the datasheet and have actually sent a pretty lengthy delay using the timer0 of the uC.So i guess my timing requirements are ok.And i've also swapped the upper and lower nibbles, not by using the SWAPF instruction but by AND'ing with 0xF0 and sending it first and then AND'ing with 0x0F and rotating it left(not through the CF).So i guess that is also taken care of.
Can you please suggest anymore reasons??
Thanks in advance
 

Given that your code is in assembler, and furthermore not commented, it takes a long while to actually understand what parts of your code do.

Can you upload the block diagram of the algorithm it's supposed to perform?
 

four-bit lcd routines working well: it takes 3ms to display a 16-char string in 4-bit mode (can be user configured to run on 8-bit mode as well).

and the code is C-compliant, meaning that it can be compiled for any mcu - the beauty of working in C.
 

Attachments

  • USB1286 lcd-4bit.PNG
    USB1286 lcd-4bit.PNG
    41.7 KB · Views: 119

the same code, configured in the 8-bit mode. all you needed to do is to recompile.

notice that the pins are also user configurable.

now, do that in assembly, :)
 

Attachments

  • USB1286 lcd-8bit.PNG
    USB1286 lcd-8bit.PNG
    40.4 KB · Views: 122
Thank you millwood..can you please go through my ALP. I am putting my documented code(with 4MHz clock frq):

;RB2=RS,RB3=E
Am using PORTB<7:4> as data lines,PORTB<3> as Enable,PORTB<2> as RS and have permanently grounded RW line because I don’t want to read anything from the LCD(am not operating using BUSY flag).The commented code is given below:
#INCLUDE<P18f4220.INC>
CONFIG OSC=XT
CONFIG WDT=OFF
CONFIG BOR=OFF
CONFIG CCP2MX=ON
CONFIG PBAD=DIG
CONFIG FSCM=OFF
CONFIG IESO=OFF

TEMP EQU 0X00
TEMP1 EQU 0X01
ORG 0
BRA MAIN
MAIN:ORG 0X20
AGAIN:CALL DELAY
MOVLW 0X0F
MOVWF ADCON1
CLRF TRISB
CLRF TRISC
CLRF TRISD
CLRF PORTB
CLRF PORTC
SETF PORTD; to check whether my program is running or not

BCF PORTB,3; clear the EN line

MOVLW 0X30;initialize LCD in 8-bit mode
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X30; ;initialize LCD in 8-bit mode again
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X30;initialize LCD in 8-bit mode again as is required by the datasheet
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X20;initialize LCD in 4-bit mode
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT


MOVLW 0X28;initialize LCD in 4-bit mode,5X7dots and 2 lines
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X0E;set the entire display and cursor
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X01;clear display
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X06; setting the cursor move direction and the shift display
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW 0X84
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL CMDWT

MOVLW A'G';starting from here, the string “GREAT” is sent
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT

MOVLW A'R'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT

MOVLW A'E'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT

MOVLW A'A'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT

MOVLW A'T'
MOVWF TEMP
MOVWF TEMP1
CALL DELAY
CALL DATWT
GOTO AGAIN;repeat the program

CMDWT:MOVFF TEMP1,WREG
ANDLW 0X0F0; the upper 4 bits of the 8 bit data are sent by ANDing using 0xF0
MOVWF PORTB; masked data sent to PORTB<7:4>
BCF PORTB,2;RS is 0,for selecting instruction register
BSF PORTB,3;setting EN
CALL DELAY;delay greater than 50ms is applied through timer0
BCF PORTB,3;clear EN
MOVFF TEMP,WREG;get the data once again
ANDLW 0X0F; the lower 4 bits of the 8 bit data are sent by ANDing using 0x0F and this data is rotated left 4 times not through the CF so that the data can be sent through PORTB<7:4>
MOVWF TEMP
RLNCF TEMP
RLNCF TEMP
RLNCF TEMP
RLNCF TEMP

CALL DELAY

MOVFF TEMP,PORTB
BCF PORTB,2; clear RS
BSF PORTB,3;set EN

CALL DELAY

BCF PORTB,3;clear EN
RETURN


DATWT:MOVF TEMP1,W;repeatation of the same subrtn as CMDWT except for the fact that RS=1.
ANDLW 0X0F0
MOVWF PORTB
BSF PORTB,2;;;;;;;;;
BSF PORTB,3
CALL DELAY
BCF PORTB,3

MOVFF TEMP,WREG
ANDLW 0X0F
MOVWF TEMP
RLNCF TEMP
RLNCF TEMP
RLNCF TEMP
RLNCF TEMP

CALL DELAY

MOVFF TEMP,PORTB
BSF PORTB,2;;;;;;;;;;;;;;;
BSF PORTB,3
CALL DELAY
BCF PORTB,3

RETURN

DELAY:BCF INTCON,TMR0IF;timer0 has been used to create the delay of 0.6 sec
MOVLW 0X00
MOVWF TMR0H
MOVWF TMR0L
MOVLW B'10010111'
MOVWF T0CON
LOOP:BTFSS INTCON,TMR0IF
BRA LOOP
BCF INTCON,TMR0IF
RETURN

;GOTO AGAIN
END
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top