PIC Table reads AN556 example 5 error (solved)

Status
Not open for further replies.

pjmelect

Advanced Member level 2
Joined
May 13, 2012
Messages
576
Helped
127
Reputation
254
Reaction score
131
Trophy points
1,323
Visit site
Activity points
5,899
Microchip issue an interesting document AN556 showing how to implement table reads. However the example they give to implement a table read (example 5) where the table can start at any memory location and can cross page boundaries contains errors. I looked online and found many people had reported that the routine does not work but no solutions to fix the program were presented so I decided to do it myself.

Code:
;This is the original code in AN556 example 5
    movlw    LOW TABLE
    addwf    OFFSETL,F
    movlw    HIGH TABLE    ;Get high byte of BASE address
    btfsc    STATUS,C
    addlw    1H
    movwf    PCLATH        ;write to PCLATH
    movf    OFFSETL,W
    call    TABLE
    
        org    01FDH
TABLE    addwf    PCL,F
    retlw    "A"
    retlw    "B"
    retlw    "C

I realised that the instruction addwf OFFSET,F was a typo and it should have been addwf OFFSET,W however even then the program did not work in every case as the author of the program had forgotten that addwf PCL,F instruction takes one byte before the data. To fix this I changed the TABLE pointers to (TABLE+1) The program now works correctly in every case.

Code:
    movlw    LOW (TABLE+1)
    addwf    OFFSETL,W
    movlw    HIGH (TABLE+1)    ;Get high byte of BASE address
    btfsc    STATUS,C
    addlw    1H
    movwf    PCLATH        ;write to PCLATH
    movf    OFFSETL,W
    call    TABLE
    
        org    1FDH
TABLE    addwf    PCL,F
    retlw    "A"
    retlw    "B"
    retlw    "C"

I have written a series of different table read routines including ones to read tables larger than 256 bytes, if you would like a copy of them please PM me.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…