- Name:
-
#macro
- Syntax:
-
#MACRO label(param1, param2...)
(code goes here)
#ENDMACRO
- Description:
-
Allows a multi-line substitution macro to be defined.
Wherever the defined macro label name is found within the program source that name will be replaced by the contents of the macro (with parameters substituted with the values specified). Please see macros within part 2 of the PICAXE manual for more details.
As a macro is processed as a 'text substitution' before compilation it cannot contain any other directive.
- Applies To:
-
All
- See Also:
-
- Related Create:
-
- Share:
-
- Print:
-
Macro Expansion
A simple macro to display a number on line one of an LCD
- Code Example:
-
; Original source code -
symbol lcdpin = B.7
#macro lcdout( delay, number )
serout lcdpin, n2400, ( 254, 128, #number, " " )
pause delay
#endmacro
main:
lcdout( 100, w0 )
w0 = w0 + 1
pause 1000
goto main
; Equivalent to -
symbol lcdpin = B.7
main:
serout lcdpin, n2400, ( 254, 128, #w0, " " )
pause 100
w0 = w0 + 1
pause 1000
goto main
Copy Code
Submit an Example
Subroutine parameters
This is an example using #macro commands to allow a program to be more C-like. The program uses the PICAXE X2 Scratchpad and 'ptr' variables to pass variables into subroutines.
- Code Example:
-
#Picaxe 20X2
#Terminal 9600
; void Sub(byte b11, byte b12) {
; byte b13 = b11 - b12;
; printf("%d\n",b13);
; }
;
; void main() {
; byte b0 = 123;
; while (1) {
; Sub(b0,23);
; b0 = b0 + 1;
; }
; }
#Macro PushByte(bArg)
@ptrInc = bArg
#EndMacro
#Macro PopByte(bVar)
ptr = ptr - 1
bVar = @ptr
#EndMacro
#Macro Define_Main
Label_Main:
#EndMacro
#Macro Define_Sub(bArg1,bArg2)
Label_Sub:
PopByte(bArg2)
PopByte(bArg1)
#EndMacro
#Macro Sub(bArg1,bArg2)
PushByte(bArg1)
PushByte(bArg2)
Gosub Label_Sub
#EndMacro
Define_Main
b0 = 123
Do
Sub(b0,23)
b0 = b0 + 1
Loop
Define_Sub(b11,b12)
b13 = b11 - b12
SerTxd( #b13, cr, lf )
Return
Copy Code
Submit an Example
Conditional #macro use
This example shows how to create a macro which can do two different things depending on whether 'PRINT_A' has been defined or not. Two separate macros are defined with different names which do the different things required, then the macro name which will be used in the main program is specified through one of the two #define commands, selected by a #ifdef.
- Code Example:
-
; Implements the equivalent of...
;
; #macro PrintNumber(n)
; #ifdef PRINT_A
; sertxd( "A", #n )
; #else
; sertxd( "X", #n )
; #endif
; #endmacro
#define PRINT_A
#ifdef PRINT_A
#define PrintNumber(n) PrintNumberA(n)
#else
#define PrintNumber(n) PrintNumberX(n)
#endif
#macro PrintNumberA(n)
sertxd( "A", #n )
#endmacro
#macro PrintNumberX(n)
sertxd( "X", #n )
#endmacro
do
for b0 = 1 to 10
PrintNumber(b0)
next
loop
Copy Code
Submit an Example
Submit Your Own Code!
You must be logged in to submit code examples. Login now.