Specifications
mikroElektronika | Free Online Book | PIC Microcontrollers | Appendix A: Programming a Microcontroller
bank1 macro ; Macro bank1
bsf STATUS, RP0 ; Set RP0 bit
bcf STATUS, RP1 ; Reset RP1 bit
endm ; End of macro
enableint macro ; Global interrupt enable
bsf INTCON,7 ; Set bit
endm ; End of macro
disableint macro ; Global interrupt disable
bcf INTCON,7 ; Reset bit
endm ; End of macro
Macros defined in this way are saved in a particular data file with extension INC which stands for INCLUDE data file. As
seen, these four macros do not have arguments. However, macros may include arguments if needed.
The following example shows macros with arguments. Pin is configured as input if the corresponding bit of the TRIS
register is set to logic one (bank1). Otherwise, it is configured as output.
input macro arg1,arg2 ;Macro Input
bank1 ;Bank containing TRIS registers
bsf arg1,arg2 ;Set the specified bit (1=Input)
bank0 ;Macro for bank 0 selection
endm ;End of macro
output macro arg1,arg2 ;Macro Output
bank1 ;Bank containing TRIS registers
bcf arg1,arg2 ;Clear the specified bit (0=Output)
bank0 ;Macro for bank 0 selection
endm ;End of macro
Macro with arguments may be called in the following way:
...
output TRISB,7 ;Pin RB7 is configured as output
...
When calling this macro, the first specified argument TRISB is replaced by the first argument arg1 in macro definition.
Similarly, number 7 is replaced by the argument arg2, and the following code is generated:
...
bsf STATUS, RP0 ;Set RP0 bit = BANK1
bcf STATUS, RP1 ;Reset RP0 bit = BANK1
bcf TRISB,7 ;Configure RB7 as output
bcf STATUS,RP0 ;Clear RP0 bit = BANK0
bcf STATUS,RP1 ;Clear RP1 bit = BANK0
...
It is clear at first sight that the program becomes more legible and flexible by using macros. The main disadvantage of
macro is that it occupies a lot of memory space because every macro name in a program is replaced by its predefined
code. Owing to the fact that programs often use macro, everything is more complicated if it is long.
callc macro label ;Macro callc
local Exit ;Define local Label within macro
bnc Exit ;If C=0 jump to Exit
call label ;If C=1 call subroutine at address Label(out of macro)
Exit ;Local Label within macro
endm ;End of macro
http://www.mikroe.com/en/books/picmcubook/appa/ (8 of 21)5/3/2009 11:35:35 AM