PIC18 Table read

Oct 14, 2006 5 Replies

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.

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.

"F" or "W"?

"F" or "W"?

A RETURN without a CALL?

[snip]

You're right. My bad.

Did you figure out what was wrong?

[snip

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

You da man! :)

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

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required