Файл: The quintessential PIC microcontroller (S. Katzen, 2000).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 15.06.2025
Просмотров: 5304
Скачиваний: 0
224 The Quintessential PIC Microcontroller
–extern: Publishes the named variables as defined outside the file, to be subsequently resolved by the linker.
–global: Publishes the named variables that have been defined (that is space reserved) in the file and that are to be made visible to the linker.
–macro - endm: Used to allow the specified enclosed sequence of instructions to be replaced by a new macro instruction; eg.
Addf macro N,file movf file,w addlw N movwf file endm
adds the literal N to the specified register file file. For example, to add five to File 20h the programmer can use the invocation
Addf 5,20h
–include: Used to include the specified file at this point; for example include "myfile.asm".
–end: Normally the last line of an assembly-level source file. Tells the assembler to ignore anything following.
Examples
Example 8.1
The following routine e ectively exchanges the byte contents of W and a file register F without needing an additional intermediate file register.
xorwf |
F,f |
; [file] |
<- |
WˆF |
|
xorwf |
F,w |
; |
W <- Wˆ(WˆF) = 0ˆF = F |
||
xorwf |
F,f |
; |
[file] |
<- |
FˆWˆF = 0ˆW = W |
where ˆ denotes eXclusive-OR.
Wrap the given code within a macro to generate a new instruction Exgwf F where F is the designated file register.
Solution
Exgwf macro |
file |
xorwf |
file,w |
xorwf |
file,f |
xorwf |
file,w |
endm |
|
8. Assembly language 227 |
|||
movlw |
1 |
; |
The first literal |
banksel |
var_0 ; |
Change to the appropriate bank |
|
movwf |
var_0 ; |
Do it |
|
movlw |
10 |
; |
Literal ten |
banksel |
var_1 ; |
Change to the appropriate bank |
|
movwf |
var_1 ; |
Do it |
|
movlw |
100 |
; |
Literal hundred |
banksel |
var_2 ; |
Change to the appropriate bank |
|
movwf |
var_2 ; |
Do it |
|
Where Indirect addressing is used for 4-bank PICs the IRP bit in STATUS must be 0 for Banks 0:1 and 0 for Banks 2:3 – see page 113. This can be implemented using the bankisel directive in a similar manner to banksel.
Self-assessment questions
8.1Design macros to simulate the PIC18XXX family relative conditional Branch instructions bc (Branch on Carry) and bz (Branch if Zero).
8.2Design a macro of the form Mul XPLIER,XCAND,PRODUCT that will implement the function PRODUCT:2 = VAR1 × VAR2. Hint: Check Program 6.5 on page 152. What do you think are the advantages and disadvantages of using a macro instead of a subroutine in a long implementation like this?
8.3The goto and call instruction op-codes use an 11-bit address suitable to transfer anywhere within a 2 Kbyte Program store; as illustrated in Fig. 5.4 on page 114. As shown in this diagram, the 13-bit Program Counter is overwritten by the instruction’s 11-bit address together with PCLATH[4:3] (PCLATch High byte) File 0Ah to give a 13bit destination address.
Some mid-range PICs have a 4 or 8 Kbyte Program store, such as the PIC16C74 and PIC16F876 respectively. These require 12or 13bit destination addresses for goto and call instructions making use of the PCLATH[4:3] bits (see page 115) and e ectively partitioning up the Program store into corresponding two or four pages. The programmer needs to manipulate these bits before the goto or call instructions to specify the page. For instance, for the PIC16C74 to call a subroutine beginning at FRED which is at address 0B00h (i.e. in Page 1) we have:
228 The Quintessential PIC Microcontroller
bsf |
PCLATH,3 |
; |
Change to Page1 |
call |
FRED |
; |
Go to it |
In a relocatable program the location of a label, such as FRED is uncertain and in a multi-page PIC may be placed by the linker in any page. To allow the assembler to alter the PCLATH[4:3] bits as appropriate, Microchip compatible assemblers have a directive pagesel which must precede any goto or call instruction; rather in the manner of the banksel directive of Example 8.4. Show how you could use this to support a series of calls to subroutines named SUB_0, SUB_1 and SUB_2.
8.4The banksel approach to selecting a bank is ine cient in that an extra instruction is issued even if the PIC is already in the correct bank. Consider how in a timeor space-critical subroutine this ine ciency can be avoided.
8.5To be safe, determine a maximum value that NUM_1 and NUM_2 should not exceed to guarantee correct working for our program to calculate the root mean square of the two variables.
8.6Rewrite the routine main.asm of Program 8.2 and the subroutine root2.asm of Program 8.4 to allow for all values of NUM_1 and NUM_2. This will require a 3-byte sum and square root function.
8.7The following routine based on the macro instruction Movlf of Example 8.3 does not work as intended. COUNT is altered seemingly at random and not consistently with the desired literal 32. Why is this?
movf |
COUNT,f |
; Test COUNT for zero |
btfsc |
STATUS,Z |
; IF not Zero THEN skip |
Movlf |
d’32’,COUNT |
; ELSE re-initialize it to 32 |
8.8 A programmer with expertise in the Motorola 68HC05 MCU has been converted to the PIC family and wishes to design macros to simulate, amongst others, the following 68HC05 instructions. Note that the Accumulator register in the 68HC05 family is the equivalent to the Working register of the PIC.
lda memory
LoaD Accumulator with data from memory.
lda #data
LoaD Accumulator with literal data. sta memory
STore Accumulator data into memory.