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

Discussion in 'Embedded Systems and Microcontrollers' started by OneSelfLost, Jul 19, 2009.

  1. OneSelfLost

    Thread Starter New Member

    Jul 19, 2009
    4
    0
    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: Jul 19, 2009
  2. OneSelfLost

    Thread Starter New Member

    Jul 19, 2009
    4
    0
    This is the code I am trying to use:
     
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  4. walid el masry

    Active Member

    Mar 31, 2009
    132
    0
    i worked for it before , here the reference i followed it
    www.hutech.edu.vn:8080/dientu/Datasheet/1602a.pdf
    and that my program
    Code ( (Unknown Language)):
    1.  
    2. LIST p=16F876
    3.     #INCLUDE<P16F876.INC>
    4.     __CONFIG _CP_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC
    5. TIME EQU 0X20;USED FOR 1mSEC DELAY METHOD
    6. TEMP1 EQU 0X21;TEMPORARY SWAP PART 1
    7. TEMP2 EQU 0X22;TEMPORARY SWAP PART 2
    8. TIMES EQU 0X23;TIMES TO EXECUTE 1mSEC DELAY
    9.     ORG 0X00
    10.     CLRF TIME
    11.     CLRF TEMP1
    12.     CLRF TEMP2
    13.     CLRF TIMES
    14.     CLRF W
    15.     TRIS PORTB
    16. START
    17.     CALL LCD_INIT
    18.     MOVLW 0X01
    19.     CALL LCD_CMD
    20.     MOVLW 0X00   ;<= ascii code for the character
    21.     CALL LCD_DAT
    22. O
    23. GOTO O
    24. LCD_RESET
    25.     MOVLW 0XFF
    26.     MOVWF PORTB
    27.     MOVLW D'20'
    28.     MOVWF TIMES;20mS delay
    29.     CALL DELAY
    30.     MOVLW 0X83
    31.     MOVWF PORTB;Data = 30H, EN = 1, First Init
    32.     MOVLW 0X03
    33.     MOVWF PORTB;Data = 30H, EN = 0
    34.     MOVLW D'15'
    35.     MOVWF TIMES;Delay 15mS
    36.     CALL DELAY
    37.     MOVLW 0X83
    38.     MOVWF PORTB;Second Init, Data = 30H, EN = 1
    39.     MOVLW 0X03
    40.     MOVWF PORTB;Data = 30H, EN = 0
    41.     MOVLW D'5'
    42.     MOVWF TIMES;Delay 5mS
    43.     CALL DELAY
    44.     MOVLW 0X83
    45.     MOVWF PORTB;Third Init
    46.     MOVLW 0X03
    47.     MOVWF PORTB
    48.     MOVLW D'5'
    49.     MOVWF TIMES;Delay 5mS
    50.     CALL DELAY
    51.     MOVLW 0X82
    52.     MOVWF PORTB;Select Data width (20H for 4bit)
    53.     MOVLW 0X02
    54.     MOVWF PORTB;Data = 20H, EN = 0
    55.     MOVLW D'5'
    56.     MOVWF TIMES;Delay 5mS
    57.     CALL DELAY
    58.     RETURN
    59. LCD_INIT
    60.     CALL LCD_RESET;Call LCD Reset sequence
    61.     MOVLW 0X28
    62.     CALL LCD_CMD;4-bit, 2 line, 5x7 dots
    63.     MOVLW 0X0C
    64.     CALL LCD_CMD;Display ON cursor OFF    
    65.     MOVLW 0X06
    66.     CALL LCD_CMD;Set entry mode (Auto increment)
    67.     MOVLW 0X80
    68.     CALL LCD_CMD;Bring cursor to line 1
    69.     RETURN
    70. LCD_CMD
    71.     MOVWF TEMP1;Save a copy of command to TEMP1 TO WORK REGISTER W
    72.     MOVWF TEMP2;Save a copy of command to TEMP2 TO WORK REGISTER W
    73.     SWAPF TEMP1,1;Swap to use higher nibble
    74.     MOVLW 0X0F
    75.     ANDWF TEMP1,1;Mask the first four bits
    76.     MOVLW 0X80
    77.     ADDWF TEMP1,1;Enable = 1, RS = 0
    78.     MOVF TEMP1,0
    79.     MOVWF PORTB;Move it to lcd port
    80.     MOVLW 0X0F
    81.     ANDWF TEMP1,1;Enable = 0, RS = 0
    82.     MOVF TEMP1,0
    83.     MOVWF PORTB;Move to lcd port
    84.     MOVLW 0X0F
    85.     ANDWF TEMP2,1;Mask first four bits
    86.     MOVLW 0X80
    87.     ADDWF TEMP2,1;Enable = 1
    88.     MOVF TEMP2,0
    89.     MOVWF PORTB;Move to port
    90.     MOVLW 0X0F
    91.     ANDWF TEMP2,1;Enable = 0
    92.     MOVF TEMP2,0
    93.     MOVWF PORTB;Move to lcd port
    94.     CALL DELAY_MS
    95.     RETURN
    96. LCD_DAT
    97.     MOVWF TEMP1;Save a copy of command to TEMP1 TO WORK REGISTER W
    98.     MOVWF TEMP2;Save a copy of command to TEMP2 TO WORK REGISTER W    
    99.     SWAPF TEMP1,1;Swap to use higher nibble
    100.     MOVLW 0X0F
    101.     ANDWF TEMP1,1;Mask the first four bits
    102.     MOVLW 0XA0
    103.     ADDWF TEMP1,1;Enable = 1, RS = 1
    104.     MOVF TEMP1,0
    105.     MOVWF PORTB;Move it to lcd port
    106.     NOP
    107.     BCF PORTB,7;Enable = 0
    108.     MOVLW 0X0F
    109.     ANDWF TEMP2,1;we need lower nibble now
    110.     MOVLW 0XA0
    111.     ADDWF TEMP2,1;Enable = 1, RS = 1
    112.     MOVF TEMP2,0
    113.     MOVWF PORTB;Move to port
    114.     NOP
    115.     BCF PORTB,7;Enable = 0
    116.     CALL DELAY_MS;Delay 1mS
    117.     RETURN
    118. DELAY;DELAY LOOPING ==> DEPEND ON TIMES VALUE
    119.     CALL DELAY_MS
    120.     DECF TIMES,1
    121.     CLRF W
    122.     XORWF TIMES,W
    123.     BTFSC STATUS,Z
    124.     GOTO DELAY;NO
    125.     RETURN;YES
    126. DELAY_MS;DELAY METHOD ==> 1mSEC
    127.     CLRF TIME
    128.     MOVLW 0XFE
    129.     MOVWF TMR0                ;Bank0
    130.     BSF STATUS,RP0           ;Bank0&1
    131.     MOVLW 0X07
    132.     MOVWF OPTION_REG   ;Bank1
    133.     BCF STATUS,RP0          ;Bank0&1
    134. WAIT
    135.     BTFSS INTCON,T0IF      ;Bank0&1
    136.     GOTO WAIT
    137.     BCF INTCON,T0IF          ;Bank0&1
    138.     INCF TIME
    139.     MOVLW 0X01
    140.     XORWF TIME,W
    141.     BTFSS STATUS,Z           ;Bank0&1
    142.     GOTO WAIT
    143.     RETURN
    144.     END
    145.  
    [​IMG]
     
  5. OneSelfLost

    Thread Starter New Member

    Jul 19, 2009
    4
    0
    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
     
  6. OneSelfLost

    Thread Starter New Member

    Jul 19, 2009
    4
    0
    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
     
Loading...