146
FUNCTIONS
96-8000
June 1999
4.4 SUBROUTINES
One of the more important programming features of a CNC is called subroutines. 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. A subroutine call is done with M97 or M98 and a Pnnnn.
The P code is the same as the O number of the subroutine to be called.
It is important to note that there is little difference between the main program and the subroutines. In the LIST
PROG display, they all appear as numbered programs. When starting execution of a program, the LIST PROG
display is used to select the MAIN program and any subroutines used are called from within the main program.
Local subroutines can be used with the M97. This can be even easier to use than the M98 because the
subroutine is part of a single main program without the need to define a different Onnnnn program. With local
subroutines, you can code an M30 for the end of your main program followed by a line number and a subroutine
that ends with an M99.
The subroutine call causes the blocks in the subroutine to be executed just as if they were included in the
main program. In order to return control to the main program, subroutines must end with an M99.
Another very important feature of subroutines is that the M98 call block may also include an L or repeat
count. If there is an L, the subroutine call 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 must be first center drilled,
peck drilled, tapped, and chamfered. If a subroutine is defined that consists only of the X-Y position of the
holes, the main program can call that subroutine after defining a canned cycle to do each of the operations.
Thus, the X-Y positions can be used several times and need not be repeated for each tool. An example follows:
O0100 (MAIN PROGRAM FOR EXAMPLE OF SUBROUTINES) ;
G54 G00 G90 X0. Y0. ;
T01 M06 (CENTER DRILL) ;
G81 R0.2 Z-0.1 F20. L0 (NO OPERATION HERE, JUST DEFINE CANNED CYCLE) ;
S2000 M03 ;
M98 P0200 (CENTER DRILL EACH HOLE) ;
T02 M06 (PECK DRILL) ;
G83 R0.2 Z-1. F10. L0 (NO OPERATION HERE, JUST DEFINE CANNED CYCLE) ;
S1000 M03 ;
M98 P0200 (PECK DRILL EACH HOLE) ;
T03 M06 (TAP IN FLOATING HOLDER OR HARD TAP) ;
G84 R0.2 Z-1. F10. L0 (NO OPERATION HERE, JUST DEFINE CANNED CYCLE) ;
S200 (1/4-20) ;
M98 P0200 (TAP EACH HOLE) ;
T04 M06 (CHAMFER) ;
G81 R0.2 Z-0.1 F20. L0 (NO OPERATION HERE, JUST DEFINE CANNED CYCLE) ;
S2000 M03 ;
M98 P0200 (CHAMFER EACH HOLE) ;
G28 M30 (END OF MAIN PROGRAM) ;
O0200 (SUBROUTINE EXAMPLE LISTING ALL HOLE POSITIONS) ;