CNC Subprogram

Subprograms are a separate CNC program chosen to be run from within another program. it is also called subroutine.
Subprograms can be run (called up) from the main CNC program or from within another subprogram. They are used to perform repetitive machining operations or sequences such as drilling, counterboring and countersinking a hole.

Subprogram Call

Sub-program is called by the use of an M98 command followed by the sub-program number preceded with a letter P.

N10 M98 P1004

In the above cnc program line the sub-program 1004 will be called, which is stored in the control memory as O1004

Subprogram End

To return to the last program (main-program) position for the program to continue, an M99 command on the last line of sub-program is used
N100 M99

Subprogram Nesting

M99 Command
Fanuc control has the ability to jump to a specific program line number on its return to the
main program using the M99 command as:
M99 P100
This command above will move the control to line number N100 in the main program.
M99 can also be written at the end of a main program, and would result in a continuous
program loop

Subprogram Repeats

The control also has the ability to contain a repeat command as part of the M98 program line.

When the program line is written with the M98 P1004 command the control actually reads the line of information as M98 P00001004 , the first 4 digits after the P word being the repeat amount. To repeat a sub-program (O1004) 33 times, the program line would read as follows:

M98 P331004

Above cnc program code will call O1004 program 33 times then will return to main program.

Example of a CNC subprogram

In the example provided, the CNC subprogram is set to incremental mode G91.

O7000 (Main Program)
G91 G28 Z0
M06 T01
M03 S1200
G90 G54 G43 H1
G00 X50 Y110Z2
G01 Z0 F200
M98 P037010 (call subprog O7010 three times)
G28 Z0
O7010 (Subprogram)
G01 Z-9 F200 (relative movement of -9 on Z axis at each execution of the subprogram)
G90 Y-110

In this example, the main program (O7000) calls the subprogram (O7010) three times with the M98 P037010 instruction. The subprogram defines a sequence of operations that is repeated each time it is called.

Sub Program Commands – Notes

Note 1: A sub program must be saved to memory using a four digit number.
Note 2: If cutter compensation is required on a tool and the co-ordinates for the tool are within the sub program, the cutter compensation must be applied and cancelled within the sub program.
Note 3: To call a sub program the M98 code is used followed by P0000 (the number of the sub program required). For example, M98 P2000 This command is read call program number 2000.
Note 4: A sub program call command (M98 P0000) can be specified along with a move command in the same block. For example : G01 X42.5 M98 P1000;
Note 5: At the end of a sub program, the M99 code is entered. This returns control to the main program. The M99 code will return control to the next block after the M98 sub program call block in the main program. If the code M99 P0000 is entered, control will pass to the main program at a block with the N number equal to that of the P number stated after the M99 code. For example : M99 P0160 This command is read return to the main program at block number N0160.

Leave a Reply