Tips & Trick - PIC

 

 

Lookup Tables


On the 17Cxxx and 18Cxxx PICs, you can access program memory via the 'table read' instructions.

On the 16Fxxx PICs, you can access program memory via a set of SFR (special function registers).

On all other PICs, you'll have to use a 'computed GOTO/RETW-style' table.

 
 
 
dt - Data Table. 

Generates a series of RETLW instructions, one instruction for each expr. Each expr must be an 8-bit value. Each character in a string is stored in its own RETLW instruction.
Simple Example 
dt "A Message", 0
dt FirstValue, SecondValue, EndOfValues
 

T

;   Get the prime number indicated by W.  The first prime number is 2 and is
 ;   indicated by W being 0.  W = 1 results in 3, W = 2 in 5, etc.  The
 ;   prime number will be in W.
 ;
 ;   WARNING: This code has not been tested, not even syntax checked.
 ;
           movwf   tblptrl     ;save table offset
           movlw   upper prime
           movwf   tblptru     ;init table entry address high byte
           movlw   high prime
           movwf   tblptrh     ;init table entry address middle byte
           movlw   low prime
           addwf   tblptrl     ;make table entry address low byte
           movlw   0
           addwfc  tblptrh     ;propagate carry to make final entry address
           addwfc  tblptru
 
           tblrd*              ;read the addressed byte into TABLAT
           movf    tablat, w   ;get the indexed table value into W
           ...
  
  
 
 .prime   code_pack
 prime                        ;table of prime numbers
           db      2           ;table index 0
           db      3           ;table index 1
           db      5
           db      7
           db      11
           db      13
           db      17
           ...

TABLE1:
 movwf POINTER       ;save table offset
 clrf  PCLATU
 movlw HIGH (DATAS)
 movwf PCLATH
 rlncf POINTER,W
 addlw LOW (DATAS)
 btfsc STATUS,C
 incf PCLATH,F
 movwf PCL
 
DATAS:
retlw '  '                                          
retlw 'O ' 
retlw 'P' 
retlw 'E' 
retlw 'N' 
retlw ':'

 
;mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
 
 
In 18F series, use tablat*+
 
TABLE1:
; Set up table pointers and send a messag to LCD
 movlw UPPER(MSG)
 movwf TBLPTRU
 movlw HIGH(MSG)
 movwf TBLPTRH
 movlw LOW(MSG)
 movwf TBLPTRL
 
 tblrd *+            ; Read the table character to tablat, then increment tblptr
 movf TABLAT,W ; Get the character from the table (MSG)
 return

MSG: 
DB "INICIALIZANDO!!!",0x00
 

 

Copyright © afg . Tutti i diritti riservati.
Aggiornato il 12/06/12 .