Introduction
A constant, such as a pin name, cannot be passed to a subroutine or a function. This is a constraint of GCBASIC.
A macro can be used to implement a method of passing a constant to a reusable code section.
The example shown below implements a button-press routine that takes an input port constant and prints the result on a serial terminal.
Note: A macro will use more program memory, as the macro is compiled as inline code. Therefore, every use of the macro will use additional program memory - the same amount of program memory for each call to the macro.
Demonstration Program:
#chip 16F877a, 16
#define Button PORTC.1 ' Switch on PIN 14 via 10K pullup resistor
DIR Button In
wait 1 sec
'USART settings
#define USART_BAUD_RATE 9600
#define USART_TX_BLOCKING
;======== MAIN PROGRAM LOOP ================
HSerPrint "Button Test"
HSerPrintCRLF 2
Do
Test_button ( button ) ' <<< passing a pin constant into a macro, not possible with a Sub or Function
Loop
;==========================================
Macro Test_button (Button)
if Button = ON then
wait 10 ms 'debounce
ButtonCount = 0
Do While Button = On
Wait 10 ms
ButtonCount += 1
Loop
if ButtonCount > 5 then
if ButtonCount > 50 then 'Long push
hserprint "Long push"
else 'Short push
hserprint "Short push"
end if
HSerPrintCRLF
end if
wait 1 s
end if
End MacroKey line: Test_button ( button ) — passes the port.pin constant Button (defined as PORTC.1) directly into the macro; because macro parameters substitute like constants rather than copying into a variable, the macro
body can compare Button = ON against the real pin, something a Sub or Function parameter could not do.
See Also:

