Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LCD won't initilise with ASM

Status
Not open for further replies.
I have written the folowing code simply to test that everything in my code is working properly before moving on. The problem that I am having is when the LCD is supposed to initilise. No matter how many times I run the code that I have writen, the program will not initilise the LCD, but as soon as I run the program that the BASIC compiler generates, it works perfectly, even after I add my code and run it. This has left me assuming that my problem lies in my LCD initilise section of code.

Code:
;PIC18F242
;Port A is connected to the LCD
;Port B is connected to the keypad
;Port C is connected to the lights
;
;Cristal frequncy for this version is 4MHz
;
key equ 0x000
rows equ 0x001
CHAR equ 0x002
INS equ 0x003
mult equ 0x004
d1 equ 0x005
d2 equ 0x006
d3 equ 0x007
d4 equ 0x008
d5 equ 0x009
 org 0018h
 retfie
 org 0008h
 retfie
 org 0000h
 movlw 0x05
 movwf mult
start_l call delay_100us
 decfsz mult, f
 goto start_l 
 movlw b'000000'
 movwf TRISA
 clrf PORTA
 movlw b'11110000'
 movwf TRISB
 clrf PORTB
 movlw b'00000000'
 movwf TRISC
 clrf PORTC
    ;63h and BDh need to be loaded
    ;into the T0 high and T0 low register, respectively
LCD_START
 bcf PORTA,4
 bcf PORTA,5
 movlw 0x0A
 movwf mult
sloop call delay_100us
 decfsz mult,f
 goto sloop
 movlw 0x33
 call SEND_INS
 movlw 0x33
 call SEND_INS
 movlw 0x32
 call SEND_INS
 movlw 0x2C
 call SEND_INS
 movlw 0x06
 call SEND_INS
 movlw 0x0C
 call SEND_INS
 movlw 0x01
 call SEND_INS
 movlw 0x02
 call SEND_INS
LCD_WELCOME
 movlw 0x48  ;H
 call SEND_CHAR
 movlw 0x65  ;e
 call SEND_CHAR
 movlw 0x6C  ;l
 call SEND_CHAR
 movlw 0x6C  ;l
 call SEND_CHAR
 movlw 0x6F  ;o
 call SEND_CHAR
 movlw 0x20  ;space
 call SEND_CHAR
 movlw 0x57  ;W
 call SEND_CHAR
 movlw 0x6F  ;o
 call SEND_CHAR
 movlw 0x72  ;r
 call SEND_CHAR
 movlw 0x6C  ;l
 call SEND_CHAR
 movlw 0x64  ;d
 call SEND_CHAR
 movlw 0x21  ;!
 call SEND_CHAR
 
Next_word
 movlw 0x10
 movlw mult
nloop call delay_1ms
 decfsz mult,f
 goto nloop
 movlw 0x01
 call SEND_INS
 movlw 0x02
 call SEND_INS
 movlw 0x57  ;W
 call SEND_CHAR
 movlw 0x68  ;h
 call SEND_CHAR
 movlw 0x61  ;a
 call SEND_CHAR 
 movlw 0x74  ;t 
 call SEND_CHAR
 movlw 0x20  ;space
 call SEND_CHAR
 movlw 0x6B  ;k
 call SEND_CHAR
 movlw 0x65  ;e
 call SEND_CHAR
 movlw 0x79  ;y
 call SEND_CHAR
 movlw 0x3F
 call SEND_CHAR ;?
 movlw 0xC0
 call SEND_INS ;go to line 2 home
Notify
 movlw 0xff
 movwf PORTC
Chk_Keys    
 movlw   0x00          ;wait until no key pressed 
       movwf   PORTB       ;set all output pins low 
       movf    PORTB,   W 
       andlw   0x0F          ;mask off high byte 
       sublw   0x0F 
       btfsc   STATUS, Z       ;test if any key pressed 
       goto    Keys          ;if none, read keys 
       call    delay_1ms 
       goto    Chk_Keys       ;else try again 
Keys        
 call    Scan_Keys 
        movlw   0x10          ;check for no key pressed 
        subwf   key, w 
        btfss   STATUS, Z 
        goto    Key_Found 
       call    delay_1ms 
       goto    Keys 
Key_Found
 clrf PORTC    
 movf    key, w 
       andlw   0x0f 
       call    J_TBL       ;lookup key in table    
       movwf   key          ;save back in key 
       return             ;key pressed now in W 
Scan_Keys    
 clrf    key 
       movlw   0xF0          ;set all output lines high 
        movwf   PORTB 
        movlw   0x04 
        movwf   rows          ;set number of rows 
        bcf     STATUS, C       ;put a 0 into carry 
Scan        
 rrncf PORTB, f 
        bsf     STATUS, C       ;follow the zero with ones 
;comment out next two lines for 4x3 numeric keypad. 
        btfss   PORTB, 7
      goto    Press 
        incf    key, f 
        btfss   PORTB, 6 
        goto    Press 
        incf    key, f 
        btfss   PORTB, 5 
        goto    Press 
        incf    key, f 
        btfss   PORTB, 4 
        goto    Press 
        incf    key, f 
        decfsz  rows, f 
        goto    Scan 
Press        
 return 
 
J_TBL
 dcfsnz key, f
 goto _0
 dcfsnz key, f
 goto _1
 dcfsnz key, f
 goto _2
 dcfsnz key, f
 goto _3
 dcfsnz key, f
 goto _4
 dcfsnz key, f
 goto _5
 dcfsnz key, f
 goto _6
 dcfsnz key, f
 goto _7
 dcfsnz key, f
 goto _8
 dcfsnz key, f
 goto _9
 dcfsnz key, f
 goto _A
 dcfsnz key, f
 goto _B
 dcfsnz key, f
 goto _C
 dcfsnz key, f goto _D
 dcfsnz key, f
 goto _E
 dcfsnz key, f
 goto _F
 return
_0
 movlw 0x30
 call SEND_CHAR
 goto CHK_KEYS
_1
 movlw 0x31
 call SEND_CHAR
 goto CHK_KEYS
_2
 movlw 0x32
 call SEND_CHAR
 goto CHK_KEYS
_3
 movlw 0x33
 call SEND_CHAR
 goto CHK_KEYS
_4
 movlw 0x34
 call SEND_CHAR
 goto CHK_KEYS
_5
 movlw 0x35
 call SEND_CHAR
 goto CHK_KEYS
_6
 movlw 0x36
 call SEND_CHAR
 goto CHK_KEYS
_7
 movlw 0x37
 call SEND_CHAR
 goto CHK_KEYS
_8
 movlw 0x38
 call SEND_CHAR
 goto CHK_KEYS
_9
 movlw 0x39
 call SEND_CHAR
 goto CHK_KEYS
_A
 movlw 0x41
 call SEND_CHAR
 goto CHK_KEYS
_B
 movlw 0x42
 call SEND_CHAR
 goto CHK_KEYS
_C
 movlw 0x43
 call SEND_CHAR
 goto CHK_KEYS
_D
 movlw 0x44
 call SEND_CHAR
 goto CHK_KEYS
_E
 movlw 0x45
 call SEND_CHAR
 goto CHK_KEYS
_F
 movlw 0x46
 call SEND_CHAR
 goto CHK_KEYS
LCD_CLR
 movlw 0x01
 call SEND_INS
 return
LCD_1HOME
 movlw 0x02
 call SEND_INS
 return
LCD_2HOME
 movlw 0xC0
 call SEND_INS
 return
LCD_ON
 movlw 0x0C
 call SEND_INS
 return
LCD_OFF
 movlw 0x08
 call SEND_INS
 return
LCD_CBLINK
 movlw 0x0D
 call SEND_INS
 return 
LCD_CUL
 movlw 0x0E
 call SEND_INS
 return
LCD_CBLINKUL
 movlw 0x0F
 call SEND_INS
 return
LCD_LEFT
 movlw 0x10
 call SEND_INS
 return
LCD_RIGHT
 movlw 0x14
 call SEND_INS
 return
LCD_SLEFT
 movlw 0x18
 call SEND_INS
 return
LCD_SRIGHT
 movlw 0x1C
 call SEND_INS
 return
LCD_1CLR
 movlw 0x80
 call SEND_INS
 return
LCD_2CLR
 movlw 0xC0
 call SEND_INS
 return
LCD_1POS
 iorlw 0x80
 call SEND_INS
 return
LCD_2POS
 iorlw 0xC0
 call SEND_INS
 return
SEND_CHAR   ;send a charicter to the LCD
 MOVWF  R4L
 BSF  LATA,4
 SWAPF  R4L,F
 MOVLW  0xF0
 ANDWF  LATA,F
 MOVF  R4L,W
 ANDLW 0x0F
 IORWF  LATA,F
 BSF  LATA,5
 NOP
 BCF  LATA,5
 NOP
 SWAPF  R4L,F
 MOVLW  0xF0
 ANDWF  LATA,F
 MOVF  R4L,W
 ANDLW  0x0F
 IORWF  LATA,F
 BSF  LATA,5
 NOP
 BCF  LATA,5
 NOP
 MOVLW  0x64
 MOVWF  R4L
 call delay_100us
 RETURN
 
SEND_INS   ;send an instruction to the LCD
 MOVWF  R4L
 BCF  LATA,4
 SWAPF  R4L,F
 MOVLW  0xF0
 ANDWF  LATA,F
 MOVF  R4L,W
 ANDLW  0x0F
 IORWF  LATA,F
 BSF  LATA,5
 NOP
 BCF  LATA,5
 NOP
 SWAPF  R4L,F
 MOVLW  0xF0
 ANDWF  LATA,F
 MOVF  R4L,W
 ANDLW  0x0F
 IORWF  LATA,F
 BSF  LATA,5
 NOP
 BCF  LATA,5
 NOP
 MOVLW  0x88
 MOVWF  R4L
 MOVLW  0x13
 MOVWF  R4H
 call delay_100us
 RETURN
delay_1us   ;4MHz 1us delay (wait 4 cycles)
 nop   ;1 cycle each
 nop
 return   ;1 cycle
delay_10us   ;40 cycles
 movlw 0x0C
 movwf d1
_10us 
 decfsz d1, f
 goto _10us
 return
delay_100us   ;400 cycles
 movlw 0x84
 movwf d1
_100us
 decfsz d1, f
 goto _100us
 return
delay_1ms
 movlw 0x04
 movwf d2
l_loop
 movlw 0x6F
 movwf d1
_1ms
 decfsz d1,f
 goto _1ms
 decfsz d2,f
 goto l_loop
 return
delay_10ms
 
delay_100ms
 movlw 0x1F ;31
 movwf d1 ;
 movlw 0x4F ;79
 movwf d2 ;4 cycles
_100ms
 decfsz d1, f ;1 cycle (skiping=2)
 goto _100ms1 ;2 cycles
 decfsz d2, f ;1 cycle
_100ms1 goto _100ms ;2 cycles
 return  ;2 cycles
delay_1s
 movlw 0x08 ;8
 movwf d1
 movlw 0x2F ;47
 movwf d2
 movlw 0x03 ;3
 movwf d3 ;6 cycles
_1s
 decfsz d1, f ;1 cycle (2 when skiping)
 goto _1s2 ;2 cycles
 decfsz d2, f ;1 cycle
_1s2 goto _1s3 ;2 cycles
 decfsz d3, f ;1 cycle
_1s3 goto _1s ;2 cycles
 nop
 return  ;3 cycles

That is the code that I wrote (the LCD is initlised at the LCD_START secton), this is the code that the BASIC compiler generates from one of it's example codes:

Code:
Define LCD_BITS = 4
Define LCD_DREG = PORTA
Define LCD_DBIT = 0
Define LCD_RSREG = PORTA
Define LCD_RSBIT = 4
Define LCD_EREG = PORTA
Define LCD_EBIT = 5
 
Lcdinit 0
Lcdout "Hello World!"

The only thing that I can see is done differently is that the BASIC generated code has 3, 4 bit resets where as mine only has 2. (I intend on changing some of the initilisation commands for my code)
 
Last edited:
hi DT,
Too many problems to list, corrected a lot, the LCD does now initialise OK, then displays 'H' and 'k'

Run this.

Code:
;Port A is connected to the LCD  
;Port B is connected to the keypad  
;Port C is connected to the lights  
;
;Cristal frequncy for this version is 4MHz  



    LIST P=18F242        ;directive to define processor
    #include <P18F242.INC>    ;processor specific variable definitions
    errorlevel -302, -207

;
key    equ    0x000
rows    equ    0x001
CHAR    equ    0x002
INS    equ    0x003
mult    equ    0x004
d1    equ    0x005
d2    equ    0x006
d3    equ    0x007
d4    equ    0x008
d5    equ    0x009

R4L    equ     0x00A
R4H    equ    0x00b

    org    0000h
    goto     start_l

    org    0008h
    retfie
        
    org    0018h
    retfie

start_l
;    movlw    0x05
;    movwf    mult
    call    delay_100us
    decfsz    mult, f
    goto    start_l 
    movlw    b'000000'
    movwf    TRISA
    clrf    PORTA
    movlw    b'11110000'
    movwf    TRISB
    clrf    PORTB
    movlw    b'00000000'
    movwf    TRISC
    clrf    PORTC
    ;63h and BDh need to be loaded 
    ;into the T0 high and T0 low register, respectively 
LCD_START 
    bcf    PORTA,4
    bcf    PORTA,5
    movlw    0x0A
    movwf    mult
sloop    call    delay_100us
    decfsz    mult,f
    goto    sloop
    movlw    0x33
    call    SEND_INS
    movlw    0x33
    call    SEND_INS
    movlw    0x32
    call    SEND_INS
    movlw    0x2C
    call    SEND_INS
    movlw    0x06
    call    SEND_INS
    movlw    0x0C
    call    SEND_INS
    movlw    0x01
    call    SEND_INS
    movlw    0x02
    call    SEND_INS
LCD_WELCOME 
    movlw    0x48        ;H
    call    SEND_CHAR
    movlw    0x65        ;e
    call    SEND_CHAR
    movlw    0x6C        ;l
    call    SEND_CHAR
    movlw    0x6C        ;l
    call    SEND_CHAR
    movlw    0x6F        ;o
    call    SEND_CHAR
    movlw    0x20        ;space
    call    SEND_CHAR
    movlw    0x57        ;W
    call    SEND_CHAR
    movlw    0x6F        ;o
    call    SEND_CHAR
    movlw    0x72        ;r
    call    SEND_CHAR
    movlw    0x6C        ;l
    call    SEND_CHAR
    movlw    0x64        ;d
    call    SEND_CHAR
    movlw    0x21        ;!
    call    SEND_CHAR
         
Next_word 
    movlw    0x10
    movlw    mult
nloop    call    delay_1ms
    decfsz    mult,f
    goto    nloop
    movlw    0x01
    call    SEND_INS
    movlw    0x02
    call    SEND_INS
    movlw    0x57        ;W
    call    SEND_CHAR
    movlw    0x68        ;h
    call    SEND_CHAR
    movlw    0x61        ;a
    call    SEND_CHAR 
    movlw    0x74        ;t 
    call    SEND_CHAR
    movlw    0x20        ;space
    call    SEND_CHAR
    movlw    0x6B        ;k
    call    SEND_CHAR
    movlw    0x65        ;e
    call    SEND_CHAR
    movlw    0x79        ;y
    call    SEND_CHAR
    movlw    0x3F
    call    SEND_CHAR    ;?
    movlw    0xC0
    call    SEND_INS    ;go to line 2 home
Notify
    movlw    0xff
    movwf    PORTC
CHK_KEYS      
    movlw    0x00        ;wait until no key pressed 
    movwf    PORTB        ;set all output pins low 
    movf    PORTB,   W 
    andlw    0x0F        ;mask off high byte 
    sublw    0x0F 
    btfsc    STATUS,Z    ;test if any key pressed 
    goto    Keys        ;if none, read keys 
    call    delay_1ms 
    goto    CHK_KEYS    ;else try again 
Keys         
    call    Scan_Keys 
    movlw    0x10        ;check for no key pressed 
    subwf    key, w 
    btfss    STATUS, Z 
    goto    Key_Found 
    call    delay_1ms 
    goto    Keys 
Key_Found 
    clrf    PORTC    
    movf    key, w 
    andlw    0x0f 
    call    J_TBL        ;lookup key in table    
    movwf    key        ;save back in key 
    return            ;key pressed now in W 
Scan_Keys      
    clrf    key 
    movlw    0xF0        ;set all output lines high 
    movwf    PORTB 
    movlw    0x04 
    movwf    rows        ;set number of rows 
    bcf    STATUS,C    ;put a 0 into carry 
Scan         
    rrncf    PORTB, f 
    bsf    STATUS,C    ;follow the zero with ones 
;comment out next two lines for 4x3 numeric keypad.   
    btfss    PORTB, 7
    goto    Press 
    incf    key, f 
    btfss    PORTB, 6 
    goto    Press 
    incf    key, f 
    btfss    PORTB, 5 
    goto    Press 
    incf    key, f 
    btfss    PORTB, 4 
    goto    Press 
    incf    key, f 
    decfsz    rows, f 
    goto    Scan 
Press         
    return     
         
J_TBL
    dcfsnz    key, f
    goto    _0
    dcfsnz    key, f
    goto    _1
    dcfsnz    key, f
    goto    _2
    dcfsnz    key, f
    goto    _3
    dcfsnz    key, f
    goto    _4
    dcfsnz    key, f
    goto    _5
    dcfsnz    key, f
    goto    _6
    dcfsnz    key, f
    goto    _7
    dcfsnz    key, f
    goto    _8
    dcfsnz    key, f
    goto    _9
    dcfsnz    key, f
    goto    _A
    dcfsnz    key, f
    goto    _B
    dcfsnz    key, f
    goto    _C
    dcfsnz    key, f 
    goto _D
    dcfsnz    key, f
    goto    _E
    dcfsnz    key, f
    goto    _F
    return
_0
    movlw    0x30
    call    SEND_CHAR
    goto    CHK_KEYS
_1
    movlw    0x31
    call    SEND_CHAR
    goto    CHK_KEYS
_2
    movlw    0x32
    call    SEND_CHAR
    goto    CHK_KEYS
_3
    movlw    0x33
    call    SEND_CHAR
    goto    CHK_KEYS
_4
    movlw    0x34
    call    SEND_CHAR
    goto    CHK_KEYS
_5
    movlw    0x35
    call    SEND_CHAR
    goto    CHK_KEYS
_6
    movlw    0x36
    call    SEND_CHAR
    goto    CHK_KEYS
_7
    movlw    0x37
    call    SEND_CHAR
    goto    CHK_KEYS
_8
    movlw    0x38
    call    SEND_CHAR
    goto    CHK_KEYS
_9
    movlw    0x39
    call    SEND_CHAR
    goto    CHK_KEYS
_A
    movlw    0x41
    call    SEND_CHAR
    goto    CHK_KEYS
_B
    movlw    0x42
    call    SEND_CHAR
    goto    CHK_KEYS
_C
    movlw    0x43
    call    SEND_CHAR
    goto    CHK_KEYS
_D
    movlw    0x44
    call    SEND_CHAR
    goto    CHK_KEYS
_E
    movlw    0x45
    call    SEND_CHAR
    goto    CHK_KEYS
_F
    movlw    0x46
    call    SEND_CHAR
    goto    CHK_KEYS
LCD_CLR
    movlw    0x01
    call    SEND_INS
    return
LCD_1HOME 
    movlw    0x02
    call    SEND_INS
    return
LCD_2HOME 
    movlw    0xC0
    call    SEND_INS
    return
LCD_ON
    movlw    0x0C
    call    SEND_INS
    return
LCD_OFF
    movlw    0x08
    call    SEND_INS
    return
LCD_CBLINK 
    movlw    0x0D
    call    SEND_INS
    return     
LCD_CUL
    movlw    0x0E
    call    SEND_INS
    return
LCD_CBLINKUL 
    movlw    0x0F
    call    SEND_INS
    return
LCD_LEFT 
    movlw    0x10
    call    SEND_INS
    return
LCD_RIGHT 
    movlw    0x14
    call    SEND_INS
    return
LCD_SLEFT 
    movlw    0x18
    call    SEND_INS
    return
LCD_SRIGHT 
    movlw    0x1C
    call    SEND_INS
    return
LCD_1CLR 
    movlw    0x80
    call    SEND_INS
    return
LCD_2CLR 
    movlw    0xC0
    call    SEND_INS
    return
LCD_1POS 
    iorlw    0x80
    call    SEND_INS
    return
LCD_2POS 
    iorlw    0xC0
    call    SEND_INS
    return
SEND_CHAR             ;send a charicter to the LCD 
    movwf    R4L
    bsf    LATA,4
    swapf    R4L,F
    movlw    0xF0
    andwf    LATA,F
    movf    R4L,W
    andlw    0x0F
    iorwf    LATA,F
    bsf    LATA,5
    nop
    bcf    LATA,5
    nop
    swapf    R4L,F
    movlw    0xF0
    andwf    LATA,F
    movf    R4L,W
    andlw    0x0F
    iorwf    LATA,F
    bsf    LATA,5
    nop
    bcf    LATA,5
    nop
    movlw    0x64
    movwf    R4L
    call    delay_100us
    return
         
SEND_INS             ;send an instruction to the LCD 
    movwf    R4L
    bcf    LATA,4
    swapf    R4L,F
    movlw    0xF0
    andwf    LATA,F
    movf    R4L,W
    andlw    0x0F
    iorwf    LATA,F
    bsf    LATA,5
    nop
    bcf    LATA,5
    nop
    swapf    R4L,F
    movlw    0xF0
    andwf    LATA,F
    movf    R4L,W
    andlw    0x0F
    iorwf    LATA,F
    bsf    LATA,5
    nop
    bcf    LATA,5
    nop
    movlw    0x88
    movwf    R4L
    movlw    0x13
    movwf    R4H
    call    delay_100us
    return
delay_1us             ;4MHz 1us delay (wait 4 cycles) 
    nop            ;1 cycle each
    nop
    return            ;1 cycle
delay_10us             ;40 cycles 
    movlw    0x0C
    movwf    d1
_10us         
    decfsz    d1, f
    goto    _10us
    return
delay_100us             ;400 cycles 
    movlw    0x84
    movwf    d1
_100us
    decfsz    d1, f
    goto    _100us
    return
delay_1ms 
    movlw    0x04
    movwf    d2
l_loop
    movlw    0x6F
    movwf    d1
_1ms
    decfsz    d1,f
    goto    _1ms
    decfsz    d2,f
    goto    l_loop
    return
delay_10ms 
         
delay_100ms 
    movlw    0x1F        ;31
    movwf    d1        ;
    movlw    0x4F        ;79
    movwf    d2        ;4 cycles
_100ms
    decfsz    d1,f        ;1 cycle (skiping=2)
    goto    _100ms1        ;2 cycles
    decfsz    d2,f        ;1 cycle
_100ms1    goto    _100ms        ;2 cycles
    return            ;2 cycles
delay_1s 
    movlw    0x08        ;8
    movwf    d1
    movlw    0x2F        ;47
    movwf    d2
    movlw    0x03        ;3
    movwf    d3        ;6 cycles
_1s
    decfsz    d1,f        ;1 cycle (2 when skiping)
    goto    _1s2        ;2 cycles
    decfsz    d2,f        ;1 cycle
_1s2    goto    _1s3        ;2 cycles
    decfsz    d3,f        ;1 cycle
_1s3    goto    _1s        ;2 cycles
    nop
    return            ;3 cycles
    
    end
 

Attachments

  • DT2.asm
    7 KB · Views: 324
  • AAesp01.gif
    AAesp01.gif
    5.3 KB · Views: 349
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top