|
THE MACRO-2
Page 35
INDEX
A MACRO is identical to a sub-routine. In fact it is a sub-routine. All the
timing associated with a normal sub-routine applies to a Macro.
A Macro is generally a short routine that is called from different
places in your program. There is no advantage in producing a MACRO rather than
a "sub-routine."
The terminology is simply to make programming more complex.
There is no need to create MACROS, but if you do, here are the requirements.
A Macro can be a Delay routine or a routine that sends out a pulse on a
particular line or any other operation - there is no limit to the
things it can do.
We have already explained how to produce a sub-routine such as Delay.
Here is the code for Delay as a sub-routine
and as a Macro:
Delay as a sub-routine:
Del1
Main
|
NOP
DECFSZ 20h,1
GOTO Del1
RETURN
BSF 06,3
CALL Del1
BCF 06,3
CALL Del1
GOTO Main |
;Create delay
;Turn on output line
;Turn off output line |
|
Delay as a MACRO:
Del1
Label
Main
|
MACRO
LOCAL Label
DECFSZ 20h,1
GOTO Label
ENDM
BSF 06,3
Del1
BCF 06,3
Del1
GOTO Main |
;Create delay
;Turn on output line
;Turn off output line
|
|
In the program above, the word Del1 in Main, is
converted to a CALL instruction by the assembler and the micro goes to the
address where Del1 Macro is located, when it executes the program. The assembler also converts the
instructions at Del1 to three lines of code. The instructions:
DECFSZ 20h,1
GOTO Label
ENDM are converted to:
DECFSZ 20h,1
GOTO Label and RETURN.
The instruction: LOCAL needs to be explained. In the example
below, the word LOCAL refers to a label called
Label. The next example uses a
label called Findus and Quell. We have used the words Findus and Quell to make
the explanation easier to understand.
The instruction LOCAL in a Macro means the next word will be used as a label in
the current macro. We have used the word "Quell" in the example below. Normally a label
cannot be used twice in a program as the assembler will not know which label to
use. But when it is used in a MACRO, the assembler assigns an address within
the Macro. This means "Quell" in another Macro will be assembled as a different
address.
Although this feature is available, always use a different name for each label.
Del1
Label
Del2
Label
Main
|
MACRO
LOCAL
Label
DECFSZ 20h,1
GOTO
Label
ENDM
MACRO
LOCAL
Label
DECFSZ 22h,1
GOTO
Label
ENDM
BSF 06,3
Del1
BCF 06,3
Del1
GOTO Main |
;Create delay
;Turn on output line
;Turn off output line |
|
Del1
Findus
Del2
Quell
Del3
Quell
|
MACRO
LOCAL Findus
DECFSZ 20h,1
GOTO Findus
ENDM
MACRO
LOCAL Quell
DECFSZ 22h,1
GOTO Quell
ENDM
MACRO
LOCAL Quell
DECFSZ 23h,1
GOTO
Quell
ENDM |
;Create delay
|
| Since a Macro operates
exactly like a sub-routine, you can "go to" another Macro from an existing
Macro. Refer to the program below. The instruction "Del1" is
performed as a CALL and the ENDM instruction at the end of the second Macro
operates as a RETURN (to Pulse MACRO - in this case).
Pulse
P1
Del1
Delay
|
MACRO
LOCAL P1
MOVLW 03
MOVWF 20h
BSF 06,3
Del1
BCF 06,3
Del1
DECFSZ 20h,1
GOTO P1
ENDM
MACRO
LOCAL Delay
DECFSZ 20h,1
GOTO Delay
ENDM |
;Pulse the output line
;Turn on output line
;Turn off output line
|
|
To exit from a Macro - use EXITM
TIMING
Timing (using a 4MHz oscillator):
Del1
Delay
Main
|
MACRO
LOCAL Delay
NOP
NOP
ENDM
BSF 06,3
Del1
BCF 06,3
Del1
GOTO Main |
1uS
1uS
1uS
1uS
1uS
1uS
1uS
1uS |
|
NEXT |