Lessons
2012 Microchip Technology Inc. DS41628B-page 89
the lower eight bits is not automatically carried over into PCLATH. Because of this, be
sure to check the Carry flag in the STATUS register immediately after the table offset
addition, so that the PCLATH can be modified accordingly.
EXAMPLE 3-46:
3.13.7.1.2 brw instruction
Once the ADC value is loaded into WREG, the Program Counter can be modified in a
somewhat more indirect method than above. The program counter will be offset by the
value in the working register. This is the most efficient method for small tables.
EXAMPLE 3-47:
3.13.7.1.3 Indirect Addressing
The FSRx/INDFx pair can be used nicely in this example. Any large table look-ups
should follow this method.
The below code loads the starting address of the gray code table. Then, the ADC value
is added into FSR0 as the offset, which points to the corresponding gray code value.
EXAMPLE 3-48:
3.13.7.1.4 Table Reads
This method uses special SFRs that are used strictly for Flash program memory
reads/writes. Writing to program memory is more complex and restrictive, but reading
a single word from program memory is straightforward.
;Using the Program Counter (PC) directly
movlw high TableStart ; get high order part of the beginning of the table
movwf PCLATH
movlw low TableStart ; load starting address of table
addwf temp,w ; add offset from ADC conversion
btfsc STATUS,C ; did it overflow?
incf PCLATH,f ; yes: increment PCLATH
movwf PCL ; modify PCL
EnhancedMethod:
brw ;jumps ahead by the amount currently in wreg
TableStart:
retlw b'0000' ; 0
……
movlw high TableStart ; get high order part of the beginning of the table
movwf FSR0H
movlw low TableStart ; get lower order part of the table
addwf temp, w ; add offset from ADC conversion
btfsc STATUS, C ; did it overflow?
incf FSR0H, f ; yes: increment high byte
movwf FSR0L ; modify lower byte
moviw FSR0++ ; move the value that FSR0 is pointing to into wreg
return ; grey code now in wreg, return to MainLoop