![]() |
![]() |
Tables can be used to read a list of data from the memory of the PICmicro. The PICmicro instruction set makes table reads easy with the ADDWF and RETLW instructions.
First, use a counter to pre-load W with the position of the table element that you want to retreive.Let's use position 4 as an example.
Next, perform a call to the first table location. At this location, an ADDWF PCL instruction is used to offset the program counter by the table position loaded in W. So, in this case the program counter, which was pointing to the location following the ADDWF PCL instruction will be offset by 4, moving the program counter to the fourth table item. This item will now be retured to your main code by a RETLW instruction.
All of this works well unless the table straddles a 256-btye memory boundary. The ADDWF PCL instruction only works on the lower 8-bits of the program counter. The first Get_Char code, below, shows a typical ADDWF PCL table read. The second Get_Char table read code checks for a memory boundary overlap and allows for proper table operation in this case.
;A normal table read using the ADDWF PCL instruction will work if the
;Get_Char and Table1 code are entirely in the first 8-bit page of program
;memory (the first 256 memory locations).
.
Get_Char MOVF Counter,W ;Load W with table offset and
CALL Table1 ;use it to get data
.
.
Table1 ADDWF PCL ;Offset program counter by adding W
RETLW 'D' ;to Table1
RETLW 'a'
RETLW 't'
RETLW 'a'
|
RETLW 0 ;End of message
;This Table Read can be directly interchanged with the one above, and will
;work wherever the Get_Char and Table2 code is located in memory.
.
Get_Char MOVLW HIGH Table2 ;Move the High Byte of the first table
;location into W
MOVWF PCLATH ;and then into PCLATH
MOVLW Table2 + 1 ;Load W with the address of the first
;piece of data in Table2
ADDWF Counter,W ;Calculate the offset address and
BTFSC STATUS,C ;see if it overflows
INCF PCLATH ;If so, increment PCLATH and
CALL Table2 ;Jump to the data table
.
.
Table2 MOVWF PCL ;W contains low program counter
;byte and points to next location
;plus Counter offset
RETLW 'D'
RETLW 'a'
RETLW 't'
RETLW 'a'
|
RETLW 0 ;End of message
All projects, plans, schematic diagrams, programs and program subroutines at this site are provided on an "as is" basis, without any warranty, either expressed or implied. These materials are provided for educational purposes only, and Sirius microSystems does not assume any liability for damages, either incidental or consequential, arising out of the application, use, or misuse of any of the materials contained herein. Sirius microSystems further reserves the right to make any changes to these materials in order to improve their function, design, or reliability.
© 2002 Sirius microSystems