Файл: 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 225

Note that this macro instruction will alter the C flag according to the outcome of the last instruction.

Example 8.2

The PIC18CXXX family have an instruction bnc (Branch if No Carry) which transfers the program execution to the specified destination if the Carry flag is zero. Devise a macro instruction to simulate this for the 12and 14-bit core families.

Solution

The code fragment below follows the macro on page 208 but with the C flag replacing the Z flag.

Bnc macro destination btfss STATUS,C

goto destination endm

Example 8.3

Macros may be nested, that is a macro may use other macros in its definition. For example, consider a macro to create a countdown process that initialises a given GPR and then decrements to zero. Assuming that the macro Movlf has already been defined:

Movlf macro

literal,destination

movlw

literal

;

Put the literal in W

movwf

destination

;

& out to the dest file

endm

write a suitable macro definition.

Solution

One possible solution is:

Countdown

macro

literal,count_file

local

C_LOOP

; A macro label

Movlf

literal,count_file

; Initialize counter

C_LOOP

decfsz

count_file,f

; Decrement

goto

C_LOOP

; REPEAT UNTIL zero

endm

The specified file register is firstly initialized to literal using the macro Movlf. The actual countdown uses the decfsz instruction to both decrement the contents of count_file and break out of the loop on zero.


226 The Quintessential PIC Microcontroller

Thus the invocation Countdown d’100’,40h will initialize File 40h to decimal 100 and decrement to zero.

Note that both the Working register and STATUS are altered by this macro as well as the target GPR. Side e ects are a hazard in using macro instructions, especially if the macro has been designed by someone else and hidden in an include file. At the very least assume that W and STATUS are altered unless known otherwise. Altering banks in a macro is also potentially hazardous.

The macro label is qualified with the local directive to ensure that each time a macro is used it does not inject C_LOOP into the assembler’s symbol table. If not so qualified then an Address label duplicated assembler error will occur.

Example 8.4

The PIC16F84 is unusual in that its GPRs are mirrored across both data banks – see Fig. 4.6 on page 92. More commonly, di erent banks hold unique GPRs. For example, the PIC16C74 has 96 GPRs in Bank 0 spanning File 20h– 7Fh and another 96 in Bank 1 spanning File 80h– FFh – see Appendix B. These two banks are not images; thus for instance, File 60h in Bank 0 is not the same as File E0h.

In order to select a file register in Bank 1, the RP0 bit must be set to 1 as described on page 93. For example; to copy W into File E0h we have:

bsf

STATUS,RP0

; Change

to Bank 1

movwf

0E0h

;

Copy W

to File E0h

bcf

STATUS,RP0

;

and move back to

Bank 0

When using a relocatable assembler, the programmer will not necessarily know which bank the linker has placed a variable. Furthermore, as the suite and mix of component source files changes, the bank may switch back and forth as di erent phases of the project evolve!

To get around this problem, Microchip-compatible assemblers provide the banksel directive. This automatically keeps track of the location of the named variable and issues code to make the appropriate change-over. Show how this directive should be used when storing the decimal literals 1, 10, 100 in three GPRs called var_0, var_1 and var_2 respectively.

Solution

A possible sequence of instructions is shown below. The directive issues either bsf STATUS,RP0 or bcf STATUS,RP0 instructions as appropriate before the following instruction. For PICs with four File register banks,11 both RP1:RP0 bits will be altered by this directive.

1116-bit core PICs can have 16 Data banks of 256-bytes each.


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.