16F876 LCD issue! - I'm a noob to programming...

Thread Starter

OneSelfLost

Joined Jul 19, 2009
4
Okay.. here's what I got..

I'm trying to get a basic message on a standard 16*2 LCD (HD44780) with PIC16F876. I have the code working beautifully on a 16F627, however, I need more ports and so I'm trying to put the program onto the F876. I would like to use the internal clock. I've done what I can to the code but it still doesn't want to work for me, my connections are as follows:

EN -> RB3
RS -> RB2
R/W - > gnd

DB7 to DB4 of the LCD -> RB7 to RB4 respectively.

I don't understand whats wrong, but I am a complete noob to programming. That's why I am here. If anybody can tell me what I'm doing wrong I would greatly appreciate it, thank you.
 
Last edited:

AlexR

Joined Jan 16, 2008
732
Before you can use an LCD display you have to initialise it.

The initialisation sequence is complex and time delays between instruction can be critical.

Take a look at this pdf document http://fab.cba.mit.edu/classes/MIT/863.05/classes/11_14/44780.pdf with special attention to pages 45-46 for the initialisation sequence. Pay close attention to the timing requirements and the minimum delays between instructions.
If you get the timing wrong the display won't.
 

walid el masry

Joined Mar 31, 2009
132
i worked for it before , here the reference i followed it
www.hutech.edu.vn:8080/dientu/Datasheet/1602a.pdf
and that my program
Rich (BB code):
LIST p=16F876
    #INCLUDE<P16F876.INC>
    __CONFIG _CP_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC
TIME EQU 0X20;USED FOR 1mSEC DELAY METHOD
TEMP1 EQU 0X21;TEMPORARY SWAP PART 1
TEMP2 EQU 0X22;TEMPORARY SWAP PART 2
TIMES EQU 0X23;TIMES TO EXECUTE 1mSEC DELAY
    ORG 0X00
    CLRF TIME
    CLRF TEMP1
    CLRF TEMP2
    CLRF TIMES
    CLRF W
    TRIS PORTB
START
    CALL LCD_INIT
    MOVLW 0X01
    CALL LCD_CMD
    MOVLW 0X00   ;<= ascii code for the character
    CALL LCD_DAT
O
GOTO O
LCD_RESET
    MOVLW 0XFF
    MOVWF PORTB
    MOVLW D'20'
    MOVWF TIMES;20mS delay
    CALL DELAY
    MOVLW 0X83
    MOVWF PORTB;Data = 30H, EN = 1, First Init
    MOVLW 0X03
    MOVWF PORTB;Data = 30H, EN = 0
    MOVLW D'15'
    MOVWF TIMES;Delay 15mS
    CALL DELAY
    MOVLW 0X83
    MOVWF PORTB;Second Init, Data = 30H, EN = 1
    MOVLW 0X03
    MOVWF PORTB;Data = 30H, EN = 0
    MOVLW D'5'
    MOVWF TIMES;Delay 5mS
    CALL DELAY
    MOVLW 0X83
    MOVWF PORTB;Third Init
    MOVLW 0X03
    MOVWF PORTB
    MOVLW D'5'
    MOVWF TIMES;Delay 5mS
    CALL DELAY
    MOVLW 0X82
    MOVWF PORTB;Select Data width (20H for 4bit)
    MOVLW 0X02
    MOVWF PORTB;Data = 20H, EN = 0
    MOVLW D'5'
    MOVWF TIMES;Delay 5mS
    CALL DELAY
    RETURN
LCD_INIT
    CALL LCD_RESET;Call LCD Reset sequence
    MOVLW 0X28
    CALL LCD_CMD;4-bit, 2 line, 5x7 dots
    MOVLW 0X0C
    CALL LCD_CMD;Display ON cursor OFF    
    MOVLW 0X06
    CALL LCD_CMD;Set entry mode (Auto increment)
    MOVLW 0X80
    CALL LCD_CMD;Bring cursor to line 1
    RETURN
LCD_CMD
    MOVWF TEMP1;Save a copy of command to TEMP1 TO WORK REGISTER W
    MOVWF TEMP2;Save a copy of command to TEMP2 TO WORK REGISTER W
    SWAPF TEMP1,1;Swap to use higher nibble
    MOVLW 0X0F
    ANDWF TEMP1,1;Mask the first four bits
    MOVLW 0X80
    ADDWF TEMP1,1;Enable = 1, RS = 0
    MOVF TEMP1,0
    MOVWF PORTB;Move it to lcd port
    MOVLW 0X0F
    ANDWF TEMP1,1;Enable = 0, RS = 0
    MOVF TEMP1,0
    MOVWF PORTB;Move to lcd port
    MOVLW 0X0F
    ANDWF TEMP2,1;Mask first four bits
    MOVLW 0X80
    ADDWF TEMP2,1;Enable = 1
    MOVF TEMP2,0
    MOVWF PORTB;Move to port
    MOVLW 0X0F
    ANDWF TEMP2,1;Enable = 0
    MOVF TEMP2,0
    MOVWF PORTB;Move to lcd port
    CALL DELAY_MS
    RETURN
LCD_DAT
    MOVWF TEMP1;Save a copy of command to TEMP1 TO WORK REGISTER W
    MOVWF TEMP2;Save a copy of command to TEMP2 TO WORK REGISTER W    
    SWAPF TEMP1,1;Swap to use higher nibble
    MOVLW 0X0F
    ANDWF TEMP1,1;Mask the first four bits
    MOVLW 0XA0
    ADDWF TEMP1,1;Enable = 1, RS = 1
    MOVF TEMP1,0
    MOVWF PORTB;Move it to lcd port
    NOP
    BCF PORTB,7;Enable = 0
    MOVLW 0X0F
    ANDWF TEMP2,1;we need lower nibble now
    MOVLW 0XA0
    ADDWF TEMP2,1;Enable = 1, RS = 1
    MOVF TEMP2,0
    MOVWF PORTB;Move to port
    NOP
    BCF PORTB,7;Enable = 0
    CALL DELAY_MS;Delay 1mS
    RETURN
DELAY;DELAY LOOPING ==> DEPEND ON TIMES VALUE
    CALL DELAY_MS
    DECF TIMES,1
    CLRF W
    XORWF TIMES,W
    BTFSC STATUS,Z
    GOTO DELAY;NO
    RETURN;YES
DELAY_MS;DELAY METHOD ==> 1mSEC
    CLRF TIME
    MOVLW 0XFE
    MOVWF TMR0                ;Bank0
    BSF STATUS,RP0           ;Bank0&1
    MOVLW 0X07
    MOVWF OPTION_REG   ;Bank1
    BCF STATUS,RP0          ;Bank0&1
WAIT 
    BTFSS INTCON,T0IF      ;Bank0&1
    GOTO WAIT
    BCF INTCON,T0IF          ;Bank0&1
    INCF TIME
    MOVLW 0X01
    XORWF TIME,W
    BTFSS STATUS,Z           ;Bank0&1
    GOTO WAIT
    RETURN
    END
 

Thread Starter

OneSelfLost

Joined Jul 19, 2009
4
Walid! Thank you! I'm overwhelmed with how much that helps me! In order to make you code display text would I add something like this?:

Message movf count, w
call Text
xorlw 0x00
btfsc STATUS, Z
goto NextMessage
call LCD_Char
call Delay255
incf count, f
goto Message

Text addwf PCL, f
retlw 'I'
retlw 'n'
retlw 'i'
retlw 't'
retlw 'i'
retlw 'a'
retlw 'l'
retlw 'i'
retlw 's'
retlw 'i'
retlw 'n'
retlw 'g'
retlw '.'
retlw '.'
retlw '.'
retlw 0x00
 

Thread Starter

OneSelfLost

Joined Jul 19, 2009
4
Hey everyone! I finally got the code working! I've decided to share my code because I had a lot of troubles and I'm sure someone else is... Here it is!

Thank you Walid for all your help, I learned a lot! And thank you Nigel Goodwin for the code, however, I added an extra message in there :D
 

Attachments

Top