User`s manual

5.0 - Motion Programs
Page - 45
5.5 - Subroutines and Subprograms
It is possible to create subroutines and subprograms in PMAC motion programs to create well-structured modular programs
with re-usable subroutines. The GOSUBx command in a motion program causes a jump to line label Nx of the same motion
program. Program execution will jump back to the command immediately following the GOSUB when a RETURN
command is encountered. This creates a subroutine.
The CALLx command in a motion program causes a jump to PROG x, with a jump back to the command immediately
following the CALL when a RETURN command is encountered. If x is an integer, the jump is to the beginning of PROG x;
if there is a fractional component to x, the jump is to line label N(y*100,000), where y is the fractional part of x. This
structure permits the creation of special subprograms, either as a single subroutine, or as a collection of subroutines, that can
be called from other motion programs.
The PRELUDE command allows creating an automatic subprogram call before each move command or other letter-
number command in a motion program.
5.5.1 - Passing Arguments to Subroutines
These subprogram calls are made more powerful by use of the READ statement. The READ statement in the subprogram
can go back up to the calling line and pick off values (associated with other letters) to be used as arguments in the
subprogram. The value after an A would be placed in variable Q101 for the coordinate system executing the program, the
value after a B would be placed in Q102, and so on (Z value goes in Q126). Letters “N” or “O” cannot be passed.
This structure is particularly useful for creating machine-tool style programs, in which the syntax must consist solely of
"letter-number" combinations in the parts program. Since PMAC treats the G, M, T, and D codes as special subroutine
calls, the READ statement can be used to let the subroutine access values on the part-program line after the code.
The READ statement also provides the capability of seeing what arguments have actually been passed. The bits of Q100 for
the coordinate system are used to note whether arguments have been passed successfully; bit 0 is 1 if an A argument has
been passed, bit 1 is 1 if a B argument has been passed, and so on, with bit 25 set to 1 if a Z argument has been passed. The
corresponding bit for any argument not passed in the latest subroutine or subprogram call is set to 0.
Example:
close delete gather undefine all
#1->2000X
open prog1 clear
LINEAR INC TA100 TS0 F50 ;Mode and timing parameters
gosub 100 H10 ;Subroutine call passing parameter H with value 10
return ;End of the main program section (execution ends)
n100 ;Subroutines section. First subroutine labeled 100
read(h) ;Read the “H” parameter value passed
IF (Q100 & $80 > 0) ;If the “H” parameter has been passed …
X(Q108) ;Use the “H” parameter value contained in Q108
endif
return ;End of the subroutine labeled 100
close ;End of the motion program code
5.5.2 - G, M, T, and D-Codes (Machine-Tool Style Programs)
PMAC permits the execution of machine-tool style RS-274 ("G-Code") programs by treating G, M, T, and D codes as
subroutine calls. This permits the machine tool manufacturer to customize the codes for his own machine, but it requires the
manufacturer to do the actual implementation of the subroutines that will execute the desired actions. When PMAC
encounters the letter G with a value in a motion program, it treats the command as a CALL to motion program 10n0, where
n is the hundreds’ digit of the value. The value without the hundred’s digit (modulo 100 in mathematical terms) controls the
line label within program 10n0 to which operation will jump -- this value is multiplied by 1000 to specify the number of the
line label. When a return statement is encountered, it will jump back to the calling program.
For example, G17 will cause a jump to N17000 of PROG 1000; G117 will cause a jump to N17000 of PROG 1010;
G973.1 will cause a jump to N73100 of PROG 1090.
M-codes are the same, except they use PROG 10n1; T-codes use PROG 10n2; D-codes use PROG 10n3.
Most of the time, these codes have numbers within the range 0 to 99, so only PROGs 1000, 1001, 1002, and 1003 are
required to execute them. For those who want to extend code numbers past 100, PROGs 1010, 1011, etc. will be required to
execute them.