Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Subprogram

CNC program is a series of instructions. If such a program includes two


or more repetitive instructions, its structure should be changed from a
single long program to two or more separate programs. Each repetitive
instruction is written only once and called when required. This is the main
concept of subprograms.

Fig.1 shows a typical part layout repeated at different locations.

Fig.1: Example of a part requirement suitable to be used as a subprogram

Each program must have its own sequence number. The programmer uses
special M code function to call one program from another. The first
program that is called another program is the main program, all other
programs are called subprograms. The main program is never called a
1
subprogram. Subprograms can also be called from other subprograms, up
to a certain number of nesting levels. Subprograms are also called
subroutines or macros.
Structurally, subprograms are similar to standard programs. They use the
same syntax rules and look and feel the same. A subprogram can use the
absolute or incremental data input, as necessary. Subprograms are loaded
into the CNC system memory just like other programs. When properly
implemented, they offer several benefits:
 Program length reduction
 Program error reduction
 Programming time and effort reduction
 Quick and easy modifications
1.1 Subprogram Functions
A subprogram must be recognized by the control system as a unique type of
program, not as a main program. This distinction is accomplished with two
miscellaneous functions, normally applicable to subprograms only:

M98 Subprogram call function


M99 Subprogram end function

The subprogram call function M98 must always be followed by the


subprogram number P--. The function M98 calls up a previously stored
subprogram from another program. If used only by itself in a block, it will
result in an error. M98 is an incomplete function, it requires two additional
parameters to become complete, therefore effective:
 The address P identifies the selected subprogram number
 The address L or K identifies the number of subprogram repetitions
( L1 or K1 is the default)

2
For example, a typical subprogram call block includes the M98 function and
the subprogram number:
N--- M98 P3951
In block N---, the subprogram 03951 is called from the CNC memory, to be
repeated once, LI (Kl) counter is the default, depending on the control.
The subprogram end Junction M99 terminates the subprogram and transfers
the processing back to program it originated from (a main program or a
subprogram).
1.2 Calling Subprograms
The program end function M30 (M02) is never used to terminate a
subprogram, it will immediately cancel all program processing and reset the
control. The program end function does not allow program execution of any
blocks beyond the block that contains it.
Normally, the subprogram end M99 returns the processing to the block
immediately following the subprogram call M98. This concept is illustrated
in Fig.2 (without block numbers) and described next.

Fig.2: Flow of program processing with a single subprogram

3
The M99 function causes the subprogram to terminate and transfers its
execution to the next block of the program it originated from. For example,
N70 M98 P3452 (SUBPROGRAM CALL)
N71 . .. (BLOCK TO RETURN TO FROM
03452)
For some special applications, it may be necessary to specify a different
block number to return to, rather than using the next block default. In this
case the programmer must include the P address in the M99 block:
M99 p..
In this format, the P address represents the block number to return to from
the completed subprogram.

Note that the address P has a different meaning when used with M98 and
M99 functions

1.5 Tool Motion and Subprograms


This hole pattern is repeated at four specified locations of the part, as
illustrated in fig.4.

150

30 125

35

Fig.4: Hole pattern layout for program examples O3404 and O3405
(both using subprogram O3455)

4
O3404 (MAIN PROGRAM) (FOUR-CORNER PATTERN)
NI G71
N2 G17 G40 G80 (plane X-Y, cancel radius offset & cancel fixed cycle)
N3 G90 G00 G54 X35 Y30 (Absolute coordinates, rapid traverse, shift zero to
X=35 & Y=30)
N4 G43 Z25 S350 M03 H01 (run spindle 350 rpm clockwise, use the length
compensation given in register H01 & move Z rapid to 25mm)
N5 G81 R0.1 Z-0.269 F3.5 (LL HOLE 1)
N6 M98 P3455 (Call subprogram number P3455)
N7 G90 X150 Y35 (LR HOLE 1)
N8 M98 P3455 (Call subprogram number P3455)
N9 G90 X6.25 Y5.0 (UR HOLE 1)
N10 M98 P3455 (Call subprogram number P3455)
N11 G90 XI.88 Y5.0 (UL HOLE 1)
N12 M98 P3455 (Call subprogram number P3455)
N13 G80 G90 G28 Z1.0 M05
N14 G91 G28 X0 Y0
O3455 (SUBPROGRAM) (FOUR-CORNER LOCATIONS)
N551 G91 X0.75 L3
N552 Y0.6 L2
N553 X-0.75 L3
N554 Y-0.6
N555 M99
%

You might also like