Microchip Technology DM164130-9 用户手册
Lessons
2012 Microchip Technology Inc.
DS41628B-page 91
3.13.7.3 PROGRAM COUNTER
Like the Enhanced Core implementation, the differences being that PCLATU is used to
address the full 21 bits, and that the ADC value is multiplied by two, because the 16-bit
PIC18 instructions are byte-addressable on the even addresses.
PIC18 instructions are byte-addressable on the even addresses.
EXAMPLE 3-50:
3.13.7.4 TABLE READ
This code is identical to the above using the Program Counter except that it is using the
TBLPTR registers and not the program counter directly. Also, note how a multiply by
two is performed on the temporary register where the ADC result is stored. This is only
necessary when the data is stored in the lower bytes of the program words. When data
is stored as bytes, then the multiply by two for table access is unnecessary, because
program memory is byte-addressable. Any single rotate to the left performs a multiple
of two and a rotate to the right is a division of two.
TBLPTR registers and not the program counter directly. Also, note how a multiply by
two is performed on the temporary register where the ADC result is stored. This is only
necessary when the data is stored in the lower bytes of the program words. When data
is stored as bytes, then the multiply by two for table access is unnecessary, because
program memory is byte-addressable. Any single rotate to the left performs a multiple
of two and a rotate to the right is a division of two.
EXAMPLE 3-51:
Once properly configured, a read can be performed.
EXAMPLE 3-52:
The code returns to the main loop with the gray code in WREG.
3.13.8
C Language
3.13.8.1 BOTH
Like usual, the ‘C’ implementation is rather easy and much more readable.
A look-up table is achieved by creating an array and declaring it as a constant, so that
the compiler places it into program space and not data space.
A look-up table is achieved by creating an array and declaring it as a constant, so that
the compiler places it into program space and not data space.
;Using the Program Counter (PC) directly
movlw upper TableStart ; move upper part
movwf PCLATU
movlw high TableStart ; get high order part of the beginning of the table
movwf PCLATH
movlw low TableStart ; load starting address of table
rlcf temp, f ; multiply by 2 by shifting to the left (remember
that the PIC18 has 16bit program counter)
addwf temp,w ; add offset from ADC conversion
btfsc STATUS,C ; did it overflow?
incf PCLATH,f ; yes: increment PCLATH
movwf PCL ; modify PCL
movlw upper TableStart ; move upper part
movwf PCLATU
movlw high TableStart ; get high order part of the beginning of the table
movwf PCLATH
movlw low TableStart ; load starting address of table
rlcf temp, f ; multiply by 2 by shifting to the left (remember
that the PIC18 has 16bit program counter)
addwf temp,w ; add offset from ADC conversion
btfsc STATUS,C ; did it overflow?
incf PCLATH,f ; yes: increment PCLATH
movwf PCL ; modify PCL
movlw upper TableStart ; Load TBLPTR with the base
movwf TBLPTRU ; address of the word
movlw high TableStart
movwf TBLPTRH
movlw low TableStart
rlcf temp, f ;multiply by 2 by shifting to the left
addwf temp, w
btfsc STATUS, C
incf TBLPTRH , f
movwf TBLPTRL
movwf TBLPTRU ; address of the word
movlw high TableStart
movwf TBLPTRH
movlw low TableStart
rlcf temp, f ;multiply by 2 by shifting to the left
addwf temp, w
btfsc STATUS, C
incf TBLPTRH , f
movwf TBLPTRL
READ_WORD:
tblrd* ; read into TABLAT
movf TABLAT, w ; get data
return
tblrd* ; read into TABLAT
movf TABLAT, w ; get data
return