18f1320 and lcd troubles

Discussion in 'Embedded Systems and Microcontrollers' started by tryingtolearn, Apr 15, 2010.

  1. tryingtolearn

    Thread Starter New Member

    Apr 4, 2010
    17
    0
    Hello everyone. I have been studying and practicing on the 18f1320 and have gotten LEDs to blink and do different things. However, I am trying to implement a LCD screen and am having problems and based on what I have read everywhere on the internet I think it is with the initialization or with the setting of my internal oscillator but haven't been able to resolve it regardless of what I try. It turns on but shows black squares all the way across the top. I have the 44780 LCD set up as 8 bit with D0-D7 as B0-B7 and E-A2, RW-A1, RS-A0. I know it may be alot to ask but I would be really thankful if someone would take a look at the code I put together and maybe see if they see something. I am using MPLAB ASM and have no errors or warnings but still can't get it to work. Thanks.

    Code ( (Unknown Language)):
    1.  
    2. #include<p18f1320.inc>
    3.  
    4.     __config    _CONFIG1H, _INTIO2_OSC_1H
    5.     __config    _CONFIG2L,  _PWRT_ON_2L
    6.     __config    _CONFIG2H, _WDT_OFF_2H
    7.     __config    _CONFIG3H, _MCLRE_OFF_3H
    8.     __config    _CONFIG4L, _LVP_OFF_4L
    9.  
    10.    
    11. ;set ports
    12. LCD_DATA        EQU    PORTB           ; LCD data lines interface
    13. LCD_DATA_TRIS   EQU    TRISB
    14. LCD_CTRL        EQU    PORTA           ; LCD control lines interface
    15.  
    16. ;misc
    17. LCD_TEMP        EQU    0x020           ; LCD subroutines internal use
    18. TABLE_INDEX        EQU    0x021            ; Index to table strings
    19. COUNT            EQU    0x022            ; A counter
    20. DELAY           EQU    0x023           ; Used in DELAYxxx routines
    21. X_DELAY         EQU    0x024           ; Used in X_DELAYxxx routines
    22.  
    23. ;PORTA Bits
    24. LCD_E           EQU    2               ; LCD Enable control line
    25. LCD_RW          EQU    1               ; LCD Read/Write control line
    26. LCD_RS          EQU    0               ; LCD Register-Select control line
    27.    
    28. ;PORTB Bits
    29. DB7        EQU    7        ; LCD dataline 7 (MSB)
    30. DB6        EQU    6        ; LCD dataline 6
    31. DB5        EQU    5        ; LCD dataline 5
    32. DB4        EQU    4        ; LCD dataline 4
    33. DB3        EQU    3        ; LCD dataline 3
    34. DB2        EQU    2        ; LCD dataline 2
    35. DB1        EQU    1        ; LCD dataline 1
    36. DB0        EQU    0        ; LCD dataline 0 (LSB)
    37.  
    38. ;Screen Lines
    39. LCD_LINE0    EQU    0x000
    40. LCD_LINE1    EQU    0x040
    41.  
    42.     org 0
    43.  
    44.         GOTO    START
    45.  
    46. START:
    47.         movlw   b'01110000'     ; setup INTOSC for 8-MHz          |B1
    48.         movwf   OSCCON      ; or b'01110000'
    49.         movlw   B'01111111'        ; Set all analog ports to digital only (both porta & portb)
    50.         movwf   ADCON1
    51.         movlw   b'11111111' ; Set PORTA direction all input
    52.          movwf   TRISA      
    53.          movlw   b'00000000' ; Set PORTB direction all output
    54.          movwf   TRISB
    55.         CLRF    INTCON        ; Clear int-flags, Disable interrupts
    56.         CLRF    PCLATH        ; Keep in lower 2KByte
    57.         CLRF    PORTA        ; ALL PORT output should output Low.
    58.         CLRF    PORTB
    59.  
    60.          cALL    LCDINIT        ; Initialize LCDisplay
    61.  
    62.         MOVLW    0x030        ; ASCII '0'
    63.         MOVWF    COUNT
    64.         MOVLW    LCD_LINE0
    65.  
    66.         CALL    LCDSDDA        ; Position cursor leftmost on first line
    67.  
    68.         CALL    TABLE_MSG    ; Display message
    69.         MOVLW    LCD_LINE0 + 0x013
    70.  
    71.         CALL    LCDSDDA        ; Position cursor
    72.         MOVF    COUNT, W
    73.  
    74.         CALL    LCDPUTCHAR    ; Display line number
    75.  
    76.         INCF    COUNT, F
    77.         MOVLW    LCD_LINE1
    78.  
    79.         CALL    LCDSDDA
    80.  
    81.         CALL    TABLE_MSG
    82.         MOVLW    LCD_LINE1 + 0x013
    83.  
    84.         CALL    LCDSDDA
    85.         MOVF    COUNT, W
    86.  
    87.         CALL    LCDPUTCHAR
    88.  
    89. LOOP
    90.         GOTO    LOOP        ; Stay here forever
    91.  
    92.  
    93. TABLE_MSG
    94.         MOVLW    0            ; Startindex of table message
    95. DISP_MSG
    96.         MOVWF    TABLE_INDEX    ; Holds message address
    97.  
    98.         CALL    MSG1
    99.         ANDLW    0x0FF        ; Check if at end of message
    100.         BTFSC    STATUS, Z    ; (zero returned at end)
    101.  
    102.         GOTO    TABLE_MSG_END            
    103.  
    104.         CALL    LCDPUTCHAR    ; Display character
    105.         MOVF    TABLE_INDEX, W    ; Point to next character
    106.         ADDLW    1
    107.  
    108.         GOTO    DISP_MSG
    109. TABLE_MSG_END    RETURN
    110.  
    111.  
    112.  
    113. LCDINIT
    114.                     ; Busy-flag is not yet valid
    115.         CLRF    LCD_CTRL    ; ALL PORT output should output Low.
    116.                     ; power-up delay
    117.         MOVLW    0x01E
    118.         CALL    X_DELAY500    ; 30 * 0.5mS = 15mS
    119.                     ; Busy Flag should be valid from here
    120.         MOVLW    0x038        ; 8-bit-interface, 2-lines
    121.         CALL    LCDPUTCMD
    122.         MOVLW    0x000        ; disp.off, curs.off, no-blink
    123.         CALL    LCDDMODE
    124.         CALL    LCDCLEAR
    125.         MOVLW    0x004        ; disp.on, curs.off
    126.         CALL    LCDDMODE
    127.         MOVLW    0x002        ; auto-inc (shift-cursor)
    128.         CALL    LCDEMODE
    129.         RETURN
    130.  
    131. LCD_ENABLE
    132.         BSF    LCD_CTRL, LCD_E    ; LCD E-line High
    133.         NOP
    134.         BCF    LCD_CTRL, LCD_E    ; LCD E-line Low
    135.         NOP
    136.         BSF    LCD_CTRL, LCD_E    ; LCD E-line High
    137.         NOP
    138.         BCF    LCD_CTRL, LCD_E    ; LCD E-line Low
    139.         RETURN
    140.  
    141. LCDBUSY
    142.         MOVLW    0x0FF        ; Set PORTB for input
    143.         MOVWF    LCD_DATA_TRIS
    144.         BCF    LCD_CTRL, LCD_RS; Set LCD for command mode
    145.         BSF    LCD_CTRL, LCD_RW; Setup to read busy flag
    146.         BSF    LCD_CTRL, LCD_E    ; LCD E-line High
    147.         MOVF    LCD_DATA, W    ; Read busy flag + DDram address
    148.         BCF    LCD_CTRL, LCD_E    ; LCD E-line Low
    149.         ANDLW    0x80        ; Check Busy flag, High = Busy
    150.         BTFSS    STATUS, Z
    151.         GOTO    LCDBUSY
    152.  
    153. LCDNOTBUSY    BCF    LCD_CTRL, LCD_RW
    154.         MOVLW    0x000
    155.         MOVWF    LCD_DATA_TRIS    ; Set PORTB for output
    156.         RETURN
    157.  
    158. LCDCLEAR
    159.         MOVLW    0x001
    160.         CALL    LCDPUTCMD
    161.         RETURN
    162.  
    163. LCDHOME
    164.         MOVLW    0x002
    165.         CALL    LCDPUTCMD
    166.         RETURN
    167.  
    168. LCDEMODE
    169.         ANDLW    0x003        ; Strip upper bits
    170.         IORLW    0x004        ; Function set
    171.         CALL    LCDPUTCMD
    172.         RETURN
    173.  
    174. LCDDMODE
    175.         ANDLW    0x007        ; Strip upper bits
    176.         IORLW    0x008        ; Function set
    177.         CALL    LCDPUTCMD
    178.         RETURN
    179.  
    180. LCDSCGA
    181.         ANDLW    0x03F        ; Strip upper bits
    182.         IORLW    0x040        ; Function set
    183.         CALL    LCDPUTCMD
    184.         RETURN
    185.  
    186. LCDSDDA
    187.         IORLW    0x080        ; Function set
    188.         CALL    LCDPUTCMD
    189.         RETURN
    190.  
    191. LCDGADDR
    192.         MOVLW    0x0FF        ; Set PORTB for input
    193.         MOVWF    LCD_DATA_TRIS
    194.         BCF    LCD_CTRL, LCD_RS; Set LCD for command mode
    195.         BSF    LCD_CTRL, LCD_RW; Setup to read busy flag
    196.         BSF    LCD_CTRL, LCD_E    ; LCD E-line High
    197.         MOVF    LCD_DATA, W    ; Read busy flag + RAM address
    198.         BCF    LCD_CTRL, LCD_E    ; LCD E-line Low
    199.         ANDLW    0x07F        ; Strip upper bit
    200.         BCF    LCD_CTRL, LCD_RW
    201.         MOVLW    0x000
    202.         MOVWF    LCD_DATA_TRIS    ; Set PORTB for output
    203.         RETURN
    204.  
    205. LCDPUTCHAR
    206.         MOVWF    LCD_TEMP    ; Character to be sent is in W
    207.         CALL    LCDBUSY        ; Wait for LCD to be ready
    208.         BCF    LCD_CTRL, LCD_RW; Set LCD in read mode
    209.         BSF    LCD_CTRL, LCD_RS; Set LCD in data mode
    210.         BSF    LCD_CTRL, LCD_E    ; LCD E-line High
    211.         MOVF    LCD_TEMP, W
    212.         MOVWF    LCD_DATA    ; Send data to LCD
    213.         BCF    LCD_CTRL, LCD_E    ; LCD E-line Low
    214.         RETURN
    215.  
    216. LCDPUTCMD
    217.         MOVWF    LCD_TEMP    ; Command to be sent is in W
    218.         CALL    LCDBUSY        ; Wait for LCD to be ready
    219.         BCF    LCD_CTRL, LCD_RW; Set LCD in read mode
    220.         BCF    LCD_CTRL, LCD_RS; Set LCD in command mode
    221.         BSF    LCD_CTRL, LCD_E    ; LCD E-line High
    222.         MOVF    LCD_TEMP, W
    223.         MOVWF    LCD_DATA    ; Send data to LCD
    224.         BCF    LCD_CTRL, LCD_E    ; LCD E-line Low
    225.         RETURN
    226.  
    227. DELAY500    
    228.         MOVLW    D'165'            ; +1        1 cycle
    229.         MOVWF    DELAY                ; +2        1 cycle
    230. DELAY500_LOOP    DECFSZ    DELAY, F    ; step 1    1 cycle
    231.         GOTO    DELAY500_LOOP        ; step 2    2 cycles
    232. DELAY500_END    RETURN                ; +3        2 cycles
    233.  
    234.  
    235. X_DELAY500    
    236.         MOVWF    X_DELAY            ; +1        1 cycle
    237. X_DELAY500_LOOP    
    238.         CALL    DELAY500    ; step1        wait 500uSec
    239.         DECFSZ    X_DELAY, F            ; step2        1 cycle
    240.         GOTO    X_DELAY500_LOOP        ; step3        2 cycles
    241. X_DELAY500_END    RETURN                ; +2        2 cycles
    242.  
    243. MSG1
    244.         addwf    PCL ,F        ;Jump to char pointed to in W reg
    245.         retlw    'T'
    246.         retlw    'E'
    247.         retlw    'S'
    248.         retlw    'T'
    249.         retlw    ' '
    250.         retlw    'L'
    251.         retlw    'I'
    252.         retlw    'N'
    253.         retlw    'E'
    254.         retlw    ' '
    255.         retlw    'N'
    256.         retlw    'U'
    257.         retlw    'M'
    258.         retlw    ' '
    259.         retlw    '#'
    260.         retlw    ' '
    261. MSG1_END
    262.    
    263.     END                ; End of program
    264.  
     
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    Get hold of the 44780 data sheet from http://fab.cba.mit.edu/classes/MIT/863.05/classes/11_14/44780.pdf and follow the initialisation sequence on page 45 to the letter. Pay special attention to the delay at power up and the delays that are needed between some of the initialisation instructions.
    Get the initialisation right and everything should work fine, get it wrong and you will have all sorts of problems.
     
Loading...