pjmelect
Advanced Member level 2
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.
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.
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.
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.