Implementing Table Read and Table Write
Author: Stan D’Souza Microchip Technology Inc.
Table Read Instruction
The PIC17C42 has an expanded instruction set which includes the TABLRD and TLRD instructions. These instructions are speciﬁcally constructed to transfer data from program memory to data memory. If the instruction syntax is: TABLRD t,i,f, the sequence in which this instructionis executed is as follows: a) b) c) d) if t = 1 then the high byte of the table latch (TBLATH) is loaded in the ﬁle register f. else (if t = 0) the low byte of the table latch (TBLATL) is loaded in the ﬁle register f. next, the 16 bit data pointed to by the table pointer (TBLPTR) is loaded into the table latch. lastly, if i = 1 the table pointer (TBLPTR) is incremented. The ﬁrst time thisinstruction is executed in a sequence, the table latch will not be initialized, hence an unknown value will be loaded in the ﬁle register. This is not a problem if the user overwrites the same f register in the next subsequent instruction.
This application note discusses how to retrieve data from program memory to data memory and write data from data memory to program memory.
As in all PIC17CXXX family parts, the simplest method used to retrieve data from program memory to data memory is to use the RETLW K instruction. For example:
; simple program to transfer ; table values to PortB Main movlw call movwf • • • SimpleTableRead addwf Table retlw PC 0 ;add offset to PC ;return a known ;table value based ;on the OFFSET. • • • retlw 10 5,W PortB ;load offset;output to PortB
If the instruction syntax is: TLRD t,f, the sequence in which this instruction is executed is as follows: a) b) if t = 1 then the high byte of the table latch (TBLATH) is loaded in the ﬁle register f. else (if t = 0) the low byte of the table latch (TBLATH) is loaded in the ﬁle register f.
15 TBLPTR 0 DataMemory
In the example above, OFFSET is loaded with the required offset to the Table and the subroutine SimpleTableRead is called. The table value is returned in the W register. In this manner program memory can be transferred to data memory.
© 1997 Microchip Technology Inc.
A simple method oftransferring data from program memory to data memory is to use the TABLRD and TLRD instructions in sequence as shown in the example below:
;transfer 6 bytes of data from program memory ;at 0x500, to data memory at 0x80 ReadInLine movlw 05 movwf clrf tablrd tlrd tablrd tlrd tablrd tlrd tablrd TBLPTRH TBLPTRL 0,1,0x80 0,0x80 1,1,0x81 0,0x82 1,1,0x83 0,0x84 1,1,0x85 ;load table pointer ; with 0x500 ; / ; /;get 16 bit value in ;table latch. ;low byte (1st) @ 80 ;high byte (2nd) @ 81 ;3rd byte @ 82 ;4th byte @ 83 ;5th byte @ 84 ;6th byte @ 85
Reading a Block of Data
In instances where a block of N bytes needs to be transferred from program memory to data memory, the TABLRD and TLRD instruction need to be included in a loop which checks for N transfers.
;transfer 'COUNT' bytes (even values only)of ;data at program memory 'MESSAGE' to data ;memory at: 'RAM_BUFFER' ReadBlock movlw high MESSAGE ;load table pointer movpf W,TBLPTRH ; / movlw low MESSAGE ; / movpf W,TBLPTRL ; / bcf ALUSTA,5 ;enable post auto ;increment of FSR0 movlw RAM_BUFFER ;initialize FSR0 ;to RAM_BUFFER movfp W,FSR0 ; / movlw COUNT/2 ;initialize count tablrd 1,1,RAM_BUFFER ;initialize table ;latch ReadBlockLoop tlrd 1,0x00tablrd decfsz goto return 0,1,0x00 W ReadBlockLoop ;do indirect read ;of high byte ;do indirect read ;of low byte ;check if count=0 ;no then do next ;else end of ;transfer.
Program Simple Table Read (using RETLW) Read In-Line Read Block (using loop) N = Number of bytes to transfer
Code Size N+3 4 + N + N/2 14 + N/2
Transfer Rate 6 cycles/byte 1.5 cycles/byte 3 cycles/byte