PROGRAMMING JANUARY 2002
SUBROUTINE
A subprogram is a separate program called up by another program. The use of
subprograms can significantly reduce the amount of programming on some
parts. Subroutines allow the CNC programmer to define a series of commands
which might be repeated several times in a program and, instead of repeating
them many times, they can be called up when needed. A subroutine call is
done with M97 or M98 and a Pnnnn. The P code command identifies the O pro-
gram number being used when executed with M98 or an N sequence number to
identify the block where a local subroutine starts when executed with M97.
Local subroutines are called with an M97. This can be easier to use than the
M98 because the subroutine is contained within the main program without the
need to define a separate Onnnn program. With local subroutines, you define an
M30 for the end of your main program portion. And after the M30 list all of your
subroutines starting with a N sequence number to define the beginning of a local
subroutine. And then end every subroutine with an M99 to send the control back
to the very next line in the main program after the subroutine call.
A subroutine call from the main program calls up program blocks in a subroutine
to be executed just as if they were included in the main program. Then to return
back to the main program, you end a subroutine with an M99 which sends it back
to the next line after the subroutine call in the main program.
Another important feature of a "subroutine call" is that the M97 and M98 block
may also include an L (loop) or repeat count. If there is an Ln with the subrou-
tine call it is repeated that number of times before the main program continues
with the next block.
The most common use of subroutines is in the definition of a series of holes
which may need to be center drilled, peck drilled, tapped, and/or chamfered. If a
subroutine is defined that consists only of the X-Y position of the holes, the main
program can define the canned cycles, and the hole locations can be called up
in the subroutine to do each of the tool operations. Thus, the X-Y positions can
be entered only once and used several times for each tool.
104
PROGRAMMING JANUARY 2002
M97 LOCAL SUB-ROUTINE CALL
This code is used to call a sub-routine, referenced by a line number N within the
same program. A Pnnnnn code is required and must match the N line number.
This is used for simple sub-routines within a program and does not require the
complication of having a separate program. A local sub-routine must still end
with an M99. If there is an L count on the M97 line, the sub-routine will be re-
peated that number of times.
Main program:
O04321 (Start of main program)
...
... (Part program)
...
M97 P123 (Jumps to line N123, after the M30, to execute a local sub-routine.)
... (The M99 at the end of the sub-routine will cause it to jump back here.)
...
... (Finish part program)
...
M30 (End of main program)
N123 (Identifies the start of the Local Sub-Routine called up by M97 P123)
...
... (Local sub-routine portion of part)
...
M99 (Jumps back to the line after the local sub-routine call in the main program)
124
JANUARY 2002 PROGRAMMING
M98 SUB-PROGRAM CALL
This M98 code is used to call a sub-program. The Pnnnn code is the sub-pro-
gram number being called; it must be in the same block as the M98. The sub-
program number being called must already be loaded into the control, and it
must contain an M99 at the end in order to return to the next line in the main
program. An L count can also be included on the line containing the M98, which
will cause the subroutine to be repeated L times before continuing to the next
block.
Main program:
O05432 (Start of main program)
...
... (Part program)
...
M98 P234 (Jumps to program O00234 to execute sub-program)
... (The M99 at the end of the sub-program will jump back here)
...
... (Finish part program)
...
M30 (End of main program)
Sub-program:
O00234 (Identifies the start of a separate sub-program)
...
... (Sub-program portion of part)
...
M99 (Jumps back to the line after the sub-program call in the main program)
125
PROGRAMMING JANUARY 2002
O11106 (PROGRAM CALLING SUB-PROGRAM WITH AN M98)
T1 M06 (90 DEG. 5/8 DIA. SPOT DRILL)
G90 G54 G00 X1.5 Y-0.5
S1406 M03
G43 H01 Z1. M08
G81 G99 Z-0.26 R0.1 F7. (G81 Drilling Canned Cycle)
M98 P11107 (Call Sub-Program O11107)
T2 M06 (27/64 DIA. DRILL)
G90 G54 G00 X1.5 Y-0.5
S2082 M03
G43 H02 Z1. M08
G83 G99 Z-1.15 Q0.2 R0.1 F12.5 (G83 Peck Drilling Canned Cycle)
M98 P11107 (Call Sub-Program O11107)
T3 M06 (1/2-20 TAP)
G90 G54 G00 X1.5 Y-0.5
S750 (G84 will turn on the spindle, so no M03 is needed)
G43 H03 Z1. M08
G84 G99 Z-1.2 R0.1 F37.5 (G84 Tapping Canned Cycle)
M98 P11107 (Call Sub-Program O11107)
G53 G49 Y0.
M30 (End Program)
%
'%
! (
'
"
"% ""%
# &
$ %
#'%
!
106
JANUARY 2002 PROGRAMMING
(Sub-program)
(Listing all the hole locations)
O11107
X.5 Y-.75 (2)
Y-2.25 (3)
G98 X1.5 Y-2.5 (4)
X3.5 R-.4 (5)
X4.5 Y-2.25 (6)
Y-.75 (7)
X3.5 Y-.5 (8)
G80 G00 Z1.0 M09
G53 G49 Z0. M05
M99 (An M99 ends a sub-program and
returns back to the next line in the main
program after the M98 sub-program call.)
%
'%
! (
'
"
"% ""%
# &
$ %
#'%
!
107
PROGRAMMING JANUARY 2002
O11109 (PROGRAM CALLING LOCAL SUB-PROGRAM WITH M97)
T1 M06 (90 DEG. 5/8 DIA. SPOT DRILL)
G90 G54 G00 X1.5 Y-0.5
S1406 M03
G43 H01 Z1. M08
G81 G99 Z-0.26 R0.1 F7. (G81 Drilling Canned Cycle)
M97 P10 (Call Local Sub-Routine N10)
T2 M06 (27/64 DIA. DRILL)
G90 G54 G00 X1.5 Y-0.5
S2082 M03
G43 H02 Z1. M08
G83 G99 Z-1.15 Q0.2 R0.1 F12.5 (G83 Peck Drilling Canned Cycle)
M97 P10 (Call Local Sub-Routine N10)
T3 M06 (1/2-20 TAP)
G90 G54 G00 X1.5 Y-0.5
S750 (G84 will turn on the spindle, so no M03 is needed)
G43 H03 Z1. M08
G84 G99 Z-1.2 R0.1 F37.5 (G84 Tapping Canned Cycle)
M97 P10 (Call Local Sub-Routine N10)
G53 G49 Y0.
M30 (End of main program)
(Local Sub-Program)
N10 (Local subroutine example listing all the hole locations)
X.5 Y-.75 (2)
Y-2.25 (3)
G98 X1.5 Y-2.5 (4)
X3.5 R-.4 (5)
X4.5 Y-2.25 (6)
Y-.75 (7)
X3.5 Y-.5 (8)
G80 G00 Z1.0 M09
G53 G49 Z0. M05
M99 (An M99 ends a sub-routine and returns back to the next line in
the main program after the M97 sub-routine call.)
108