Maker Pro
Maker Pro

PIC18 Table read

R

Randy Day

I'm having trouble getting my head around this.
I want to add a two-byte offset to TBLPTR to
read a large table. I tried to:

add the low byte to TBLPTRL
inc TBLPTRH if carry is true
inc TBLPTRU if carry is true again
add the high byte to TBLPTRH
inc TBLPTRU if carry is true

Without the code between the comments, the
routine returns the first word in TABLE0
properly. What am I missing?

TABLE_TO_TEMP
MOVLW LOW TABLE0
MOVWF TBLPTRL

MOVLW HIGH TABLE0
MOVWF TBLPTRH

MOVLW UPPER TABLE0
MOVWF TBLPTRU

RLCF TEMPLO ; multiply offset by
RLCF TEMPHI ; 2 for word-size data

; ======== calculate offset into table
MOVF TEMPLO
ADDWF TBLPTRL, 1 ; changes from 7A to 7F
; when templo=0x00. WTF?!?

BTFSC STATUS, C ; did we wrap?
CALL INCTABLE ; inc hi/upper

MOVF TEMPHI
ADDWF TBLPTRH, 1 ; add high byte
BTFSC STATUS, C ; did we wrap?
INCF TBLPTRU ; inc upper

; ======== perform read; put data in TEMP vars
TBLRD*+
MOVFF TABLAT, TEMPLO
TBLRD*
MOVFF TABLAT, TEMPHI

RETURN

INCTABLE
INCF TBLPTRH ; inc high byte
BTFSC STATUS, C ; did we wrap again?
INCF TBLPTRU ; if so, inc upper
RETURN

TABLE0 dw 0xAABB, 0xCCDD, 0xEEFF, 0xFFAB, 0x0004, 0x0005, 0x0006,
0x0007 ; etc.
 
A

Anthony Fremont

Randy said:
I'm having trouble getting my head around this.
I want to add a two-byte offset to TBLPTR to
read a large table. I tried to:

add the low byte to TBLPTRL
inc TBLPTRH if carry is true
inc TBLPTRU if carry is true again
add the high byte to TBLPTRH
inc TBLPTRU if carry is true

Without the code between the comments, the
routine returns the first word in TABLE0
properly. What am I missing?

TABLE_TO_TEMP
MOVLW LOW TABLE0
MOVWF TBLPTRL

MOVLW HIGH TABLE0
MOVWF TBLPTRH

MOVLW UPPER TABLE0
MOVWF TBLPTRU

RLCF TEMPLO ; multiply offset by
RLCF TEMPHI ; 2 for word-size data

; ======== calculate offset into table
MOVF TEMPLO

Do you mean "MOVF TEMPLO,F" or "MOVF TEMPLO,W" ?
I don't know what the default is, but I'm guessing it's "F" (but you
seem to want "W") since you're having a problem right here. You should
always specify it for clarity.
ADDWF TBLPTRL, 1 ; changes from 7A to 7F
; when templo=0x00. WTF?!?

Always use "F" or "W", never use "1" or "0". It varies from PIC to PIC.
BTFSC STATUS, C ; did we wrap?
CALL INCTABLE ; inc hi/upper

MOVF TEMPHI

"F" or "W"?
ADDWF TBLPTRH, 1 ; add high byte

"F" or "W"?
BTFSC STATUS, C ; did we wrap?
INCF TBLPTRU ; inc upper

; ======== perform read; put data in TEMP vars
TBLRD*+
MOVFF TABLAT, TEMPLO
TBLRD*
MOVFF TABLAT, TEMPHI

RETURN

A RETURN without a CALL?
 
R

Randy Day

Anthony Fremont wrote:

[snip]
Do you mean "MOVF TEMPLO,F" or "MOVF TEMPLO,W" ?
I don't know what the default is, but I'm guessing it's "F" (but you
seem to want "W") since you're having a problem right here. You should
always specify it for clarity.




Always use "F" or "W", never use "1" or "0". It varies from PIC to PIC.

You're right. My bad.
 
A

Anthony Fremont

Randy said:
Anthony Fremont wrote:

[snip]
Do you mean "MOVF TEMPLO,F" or "MOVF TEMPLO,W" ?
I don't know what the default is, but I'm guessing it's "F" (but you
seem to want "W") since you're having a problem right here. You
should always specify it for clarity.




Always use "F" or "W", never use "1" or "0". It varies from PIC to
PIC.

You're right. My bad.

Did you figure out what was wrong?
 
R

Randy Day

Anthony Fremont wrote:

[snip
Did you figure out what was wrong?

Just finished testing it. Apparently, that's
all it was. I made the change, and now it
works perfectly.

You da man! :)
 
J

Johnny Boy

I didn't know that this varied PIC to PIC. Luckily, I've always used "F"
or "W", but I, too, will keep it in mind in the future. Thanks for the tip,
Anthony.
.... Johnny
 
Top