Displaying a message on LCD

Discussion in 'Programmer's Corner' started by maphothoane, Dec 10, 2010.

  1. maphothoane

    Thread Starter New Member

    Dec 10, 2010
    3
    0
    Hello guys,

    I want to display a message "Hello" on a 16x2 LCD. I am using PIC16F877 and I am programing using assembly language. I used proteus to build or design my project. The LCD does not display the message so I would like you guys help me by looking on what is wrong to my code and my circuit.

    my code
    Code ( (Unknown Language)):
    1.  
    2.  
    3.     list      p=16f877            ; list directive to define processor
    4.  #include <p16f877.inc>        ; processor specific variable definitions
    5.  
    6.  __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _RC_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF
    7. ; '__CONFIG' directive is used to embed configuration data within .asm file.
    8. ; The lables following the directive are located in the respective .inc file.
    9. ; See respective data sheet for additional information on configuration word.
    10.  
    11.  
    12.  
    13. ;***** VARIABLE DEFINITIONS
    14. w_temp        EQU     0x7E        ; variable used for context saving
    15. status_temp   EQU     0x7F        ; variable used for context saving
    16.   cblock 0x20
    17.             count            ;used in loops
    18.             count1   ;used in delay routine
    19.    counta   ;used in delay routine
    20.    countb   ;used in delay routine
    21.             templcd ;temp store for 4 bit mode
    22.   endc
    23. LCD_RS Equ 0x04 ;LCD handshake lines
    24. ;LCD_RW connected to the ground
    25. LCD_E Equ 0x07
    26.  
    27.  
    28. ;**********************************************************************
    29.   ORG     0x000             ; processor reset vector
    30.   clrf    PCLATH            ; ensure page bits are cleared
    31.     goto    Main             ; go to beginning of program
    32.  
    33.   ORG     0x004             ; interrupt vector location
    34.   movwf   w_temp            ; save off current W register contents
    35.   movf STATUS,w          ; move status register into W register
    36.   movwf status_temp       ; save off contents of STATUS register
    37.  
    38. ; isr code can go here or be located as a call subroutine elsewhere
    39.  
    40.   movf    status_temp,w     ; retrieve copy of STATUS register
    41.   movwf STATUS            ; restore pre-isr STATUS register contents
    42.   swapf   w_temp,f
    43.   swapf   w_temp,w          ; restore pre-isr W register contents
    44.   retfie                    ; return from interrupt
    45.  
    46.  
    47.  
    48. ;----------------------------------------------------------------------------------------
    49.  
    50. ;Subroutines:
    51. Init
    52.         clrw                    ; Zero.
    53.         movwf   PORTB      ;resets input/output ports
    54.         bsf     STATUS,RP0      ; Select Bank 1
    55.   movlw   b'00000000'    ; Set port B bits 0-3 as outputs b0-b3 connected to lcd  d4-d7 pins
    56.                                 ;and b6-b7 connected to RS,E and RW grounded      
    57.         movwf   TRISB        ; Set TRISB register
    58.         bcf     STATUS,RP0      ; Select Bank 0
    59.        
    60.         retlw 0
    61.  
    62. LCD_Init
    63.         movlw 0x20   ;Set 4 bit mode
    64.   call LCD_Cmd
    65.         movlw 0x28   ;Set display shift
    66.   call LCD_Cmd
    67.   movlw 0x06   ;Set display character mode
    68.   call LCD_Cmd
    69.   movlw 0x0d   ;Set display on/off and cursor command
    70.   call LCD_Cmd
    71.   call LCD_Clr   ;clear display
    72.   retlw 0x00
    73.  
    74. LCD_Cmd  
    75.         movwf templcd
    76.   swapf templcd, w ;send upper nibble
    77.   andlw 0x0f   ;clear upper 4 bits of W
    78.   movwf PORTB
    79.   bcf     PORTB,LCD_RS ;RS line to 0
    80.   call Pulse_e   ;Pulse the E line high
    81.   movf templcd, w ;send lower nibble
    82.   andlw 0x0f   ;clear upper 4 bits of W
    83.   movwf PORTB
    84.   bcf     PORTB,LCD_RS;RS line to 0
    85.   call Pulse_e   ;Pulse the E line high
    86.   call  Delay5
    87.   retlw 0x00
    88. LCD_CharD addlw 0x30
    89. LCD_Char
    90.       movwf templcd
    91.   swapf templcd,w ;send upper nibble
    92.   andlw 0x0f   ;clear upper 4 bits of W
    93.   movwf PORTB
    94.   bsf     PORTB,LCD_RS   ;RS line to 1
    95.   call Pulse_e   ;Pulse the E line high
    96.   movf templcd,w ;send lower nibble
    97.   andlw 0x0f   ;clear upper 4 bits of W
    98.   movwf PORTB
    99.   bsf     PORTB,LCD_RS   ;RS line to 1
    100.   call Pulse_e   ;Pulse the E line high
    101.   call  Delay5
    102.   retlw 0x00
    103. LCD_Clr  
    104.         movlw 0x01   ;Clear display
    105.   call LCD_Cmd
    106.   retlw 0x00
    107. Delay255
    108.         movlw 0xff  ;delay 255 mS
    109.   goto d0
    110. Delay100
    111.      movlw d'100'  ;delay 100mS
    112.   goto d0
    113. Delay50
    114.   movlw d'50'  ;delay 50mS
    115.   goto d0
    116. Delay20
    117.      movlw d'20'  ;delay 20mS
    118.   goto d0
    119. Delay5
    120.   movlw 0x05  ;delay 5.000 ms (4 MHz clock)
    121. d0  movwf count1
    122. d1  movlw 0xC7   ;delay 1mS
    123.   movwf counta
    124.   movlw 0x01
    125.   movwf countb
    126. Delay_0
    127.   decfsz counta, f
    128.   goto $+2
    129.   decfsz countb, f
    130.   goto Delay_0
    131.   decfsz count1 ,f
    132.   goto d1
    133.   retlw 0x00
    134. Text
    135.   addwf PCL, f
    136.   retlw 'H'
    137.   retlw 'e'
    138.   retlw 'l'
    139.   retlw 'l'
    140.   retlw 'o'
    141.   retlw 0x00
    142. Pulse_e  
    143.         bsf PORTB,LCD_E
    144.   nop
    145.   bcf PORTB,LCD_E
    146.   retlw 0x00
    147.  
    148.  
    149. EndMessage
    150.  
    151. Stop  goto Stop   ;endless loop
    152.  
    153.  
    154. ;Program Start:
    155.  
    156.  
    157. Main
    158.         call Init
    159.         call Delay100  ;wait for LCD to settle
    160.         call   LCD_Init
    161.         clrf count   ;set counter register to zero
    162. message
    163.         movf count, w  ;put counter value in W
    164.         call Text   ;get a character from the text table
    165.         xorlw 0x00  ;is it a zero?
    166.         btfsc STATUS,Z
    167.   goto EndMessage
    168.         call LCD_Char
    169.         incf count, f
    170.         goto message
    171.  
    172.         END
    173.  
    On the attachment I have the hex file and the proteus design and the picture of my circuit
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    As I can see you have used _RC_OSC setting in the config word. But in your circuit you use a xtal configuration. That may give you some trouble
     
  3. maphothoane

    Thread Starter New Member

    Dec 10, 2010
    3
    0
    I have changed it to
    Code ( (Unknown Language)):
    1.  
    2.  list      p=16f877            ; list directive to define processor
    3.  #include <p16f877.inc>        ; processor specific variable definitions
    4.  
    5.  __CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF
    6.  
    Still there was no text displayed
     
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Your LCD initialisation routine seems to be lacking some steps and is missing critical delays between instructions.
    Get the relevant data sheet for your LCD display controller and follow the suggested initialisation sequence to the letter paying particular attention to the delays that must occur between instructions during the early steps of initialisation.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Timing is critical in LCD displays. Then setting it up, some delays must added. Or it will be quite dead. You will find the timing details in the LCD datasheet. Feel free to ask about this matter here
    Now will be a good time to crank up the software simulator in MPLAB. Take a good look on my posting here.
     
Loading...