MOVLW 0x00
MOVWF PCLATH
MOVF index,W
ADDWF PCL,F
RETLW 0xFC
RETLW 0xF9
RETLW 0xF3
RETLW 0xE7
RETLW 0xCF
RETLW 0x9F
RETLW 0x3F
;******************************************************************
;
; example table lookup - 4 character table entries
;
Print_PSI
;
; multiply index [000..100] times 4
;
clrc ; clear carry before rotate |B0
clrf PTRH ; clear pointer hi |B0
rlf INDEX,W ; put index (0-100) x 2 in W |B0
movwf PTRL ; index * 2 in PTRL:PTRH |B0
rlf PTRH,f ; |B0
clrc ; clear carry before rotate |B0
rlf PTRL,f ; index * 4 in PTRL:PTRH |B0
rlf PTRH,f ; |B0
;
; now add index*4 to table address
;
movlw low PSI_table ; table address lo |B0
addwf PTRL,f ; add to index x 4 lo byte |B0
btfsc STATUS,C ; overflow? |B0
incf PTRH,f ; increment hi byte |B0
movlw high PSI_table ; table address hi |B0
addwf PTRH,f ; add to index x 4 hi byte |B0
call PutString ; lookup & print 4-char string |B0
Print " psi" ; using the "Print" macro |B0
return ; |B0
;******************************************************************
;
; PutStr - setup PTRL and PTRH to string address before entry
; - string must be terminated with a 00 byte
;
PutString
call GetTable ; get a table character |B0
andlw b'11111111' ; |B0
btfsc STATUS,Z ; a 00 byte, last character? |B0
return ; yes, return |B0
call Put232 ; output char |B0
incfsz PTRL,F ; increment pointer |B0
goto PutString ; |B0
incf PTRH,F ; |B0
goto PutString ; |B0
;
GetTable
movf PTRH,W ; |B0
movwf PCLATH ; |B0
movf PTRL,W ; |B0
movwf PCL ; |B0
;******************************************************************
;
; PSI String Table (00 terminated strings)
;
PSI_table
dt "001/0" ; entry 000
dt "002/0" ; entry 001
dt "003/0" ; entry 002
;******************************************************************
;
; sample usage
;
Print " Controller Menu/r/n/n"
Print "<1> Adjust Azimuth/r/n"
Print "<2> Adjust Elevation/r/n"
Print "<3> Save New Settings/r/n"
;
;******************************************************************
;
; Print macro - print a string to the RS-232 port
;
Print macro str ;
local String,Prt
movlw low String ;
movwf PTRL ;
movlw high String ;
movwf PTRH ;
goto Prt ;
String dt str,0
Prt call PutString ; print string
endm
;******************************************************************
May I suggest that you can easily overcome the limitations of your "simple" table example by simply adjusting PCLATH along with PCL?polymath said:Hi ray21
If you use a simple look-up table you must ensure that it does not cross a 256 word page boundary.
Simply set the ORG 0x???? of the ADDWF PC,F instruction to the start of a page.
eg.
org 0x0500
lookup table addwf PC,F
retlw value1
retlw value2
retlw value3
etc. etc.
put required element no. in W
call lookup ...... returns with value in W
call must be in the same 2k page as look-up table
Polymath
;
; code can be anywhere (within 2kword memory 'page' boundary)
; table can be anywhere (within 2kword memory 'page' boundary)
; table can cross a 256 word boundary
;
Lookup movwf TEMP ; save index value
movlw high Table ; table address hi
movwf PCLATH ; setup PCLATH
movlw low Table ; table address lo
addwf TEMP,W ; add index
skpnc ; overflow? no, skip, else
incf PCLATH,f ; increment address hi
movwf PCL ; perform the branch
;
Table dt value1
dt value2
dt value3
;
Mike said:May I suggest that you can easily overcome the limitations of your "simple" table example by simply adjusting PCLATH along with PCL?polymath said:Hi ray21
If you use a simple look-up table you must ensure that it does not cross a 256 word page boundary.
Simply set the ORG 0x???? of the ADDWF PC,F instruction to the start of a page.
eg.
org 0x0500
lookup table addwf PC,F
retlw value1
retlw value2
retlw value3
etc. etc.
put required element no. in W
call lookup ...... returns with value in W
call must be in the same 2k page as look-up table
Polymath
Code:; ; code can be anywhere (within 2kword memory 'page' boundary) ; table can be anywhere (within 2kword memory 'page' boundary) ; table can cross a 256 word boundary ; Lookup movwf TEMP ; save index value movlw high Table ; table address hi movwf PCLATH ; setup PCLATH movlw low Table ; table address lo addwf TEMP,W ; add index skpnc ; overflow? no, skip, else incf PCLATH,f ; increment address hi movwf PCL ; perform the branch
There are other important considerations for saving, adjusting, and restoring PCLATH when code and/or tables cross PIC 2-kword "page" boundaries (Microchip uses the "page" term to describe 2-kword memory boundaries).Code:; Table dt value1 dt value2 dt value3 ;
Kind regards, Mike
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?