Subprograms are a separate CNC program chosen to be run from within another program.
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.
CNC machines run the lines of code in a program in order.
Using subprograms allows the programmer to jump around to different sections of the current program or to run a different program and come back to the current program.
Want to learn more about CNC G Code?
What is the difference between a subprogram and the main program?
The commands to end the main program and a subprogram are different.
The main CNC program ends with either M30 (most machines) or M02 (older machines) code.
No further code will be run after either of these codes.
A subprogram ends with the M99 command.
The M99 code returns the machine to the program which called the subprogram. The machine will continue to run code in this program.
CNC codes used when working with subprograms
- P – Selects the subprogram number to run. When used with M97, the P code selects the line in the current program to run.
- O – Identifies the program number.
- M97 – Jumps to a line number in the program selected with the P command.
- M98 – Jumps to a subprogram selected with the P code.
- M99 – Ends the subprogram, returns to the line after where subprogram was run.
- L – Number of times to repeat the subprogram.
How to call a subprogram
There are two ways to run subprograms: M97 and M98.
The difference between M97 and M98 is the program location they move to.
M97 will jump to a new line in the current program.
M98 will run an entirely different program.
Format for using M97
The format for using an M97 code is:
M97 P1234 L5
This line of code will tell the machine to move to line N1234 of the current program. The L code tells the CNC machine to run the subprogram five times.
The L code can be left out if the subprogram will only be run once.
Line 1234 will be after the program end command (M30). Once the machine reaches the M99 command it will return to the line after the example shown above.
Format for using M98
The format for using an M98 code is:
M98 P5678 L2
This line of code will tell the machine to go run program number 5678 two times. The L code tells the CNC machine to run the subprogram twice.
The L code can be left out if the subprogram will only be run once.
Program 5678 will perform any necessary machining functions and then end with an M99 command instead of M30. The M99 command will make the machine return to the line after the example shown above.
On the other hand, both commands (M98 and M97) use parameters K (or L) as the number of repetitions of a given subprogram. The use of the letter K or L depends on the CNC model. Always check the machine’s manual for further details.
G21 (metric mode)
G17 G40 G80 (select XY plane, cancel cutter compensation and canned cycles)
G91 G28 Z0 (home z axis)
M03 S1200 (turn spindle on)
G90 G54 G43 H1 (incremental mode, select work offset, turn on tool length compensation)
G00 X5.0 Y5.0 (rapid to hole #1 location)
Z2.0 (position tool above part)
M98 P5555 (run subprogram)
G28 Z0 (home z axis)
M30 (end program)
G91 (set incremental movement mode)
G81 Z-10.0 F200 (drill hole #1)
X5.0 (drill hole #2)
Y6.0 (drill hole #3)
X-5.0 (drill hole #4)
G00 Z20.0 (rapid to safe location above part)
M99 (return to main program)
In the example above, the machine will create a pattern of four holes. This pattern could be easily repeated by moving to the start of the pattern in a new location and running the subprogram again.
To do this the location of the repeated pattern would be listed after the subprogram call in the main program and the subprogram call of M98 P5555 would be listed again. The machine would then make the same pattern of holes in the new location.
What are subprograms used for?
Subprograms are used for repeating tasks.
This can be machining of a part or controlling the machine itself.
Part related subprograms
Most of these subprograms include canned cycles in their lines of code to perform repetitive machining operations such as drilling, pecking, tapping, threading, bearing, and boring.
They also help to run the same program in different parts of the workpiece, even if they don’t include canned cycles.
This can include rotating or changing the tool, contouring or finishing.
Machine related subprograms
Subprograms can also be used to control the machine.
Changing a tool in the CNC might consist of turning the coolant off, turning the spindle off and moving it to a safe location, setting or canceling a variety of modes, and finally switching the tool.
A subprogram can be created to automate the tasks and make sure it is executed the same way each time.
This makes the operation less prone to errors or crashes.
This benefit only gets better as the amount of operations performed gets longer.
Benefits of using a subprogram
The greatest benefit of using subprograms is reducing the lines of code which makes the program easier to read and edit.
Subprograms can also reduce the number of errors in the program. Less lines of code means less possible sources of problems.
Drawbacks of using a subprogram
Subprograms are meant to make the program easier to work with for both the programmer and anyone using it. If not done correctly, they can have the opposite effect.
Programmers should remember that part of their job is making sure that the program is easy to use for the operator.
It can also be easy to have the wrong modal commands or offsets chosen when starting or ending a subprogram. Following a good program format that uses safety lines or blocks of code can protect from this happening.
If it is important that a mode or offset is needed in a specific section of your program, it is best to use the necessary code to make sure things are set correctly. Assuming your machine is in the correct mode already is dangerous.
Making subprograms inside your subprograms (nesting) also has the potential to cause confusion. Repeatedly switching between programs can be confusing for both programmers and operators.
For a more in-depth description of subprogram nesting, see additional info on the topic further down in this post.
Differences between subprograms and canned cycles
Subprograms are small blocks of code used to perform repeatable machining operations or functions.
Canned cycles are commands that give the machine instructions for a pattern of movements used to simplify code.
Canned cycles are used for operations that the majority of CNC users will need such as drilling or counterboring a hole. Subprograms can be made custom to the needs of the individual machine user.
In a way, canned cycles are like mini subprograms that can be used easily in your program without needing to create a separate subprogram.
There are often times where canned cycles are used together with subprograms to increase the efficiency of the program even further.
How do modal commands work in subprograms?
Modal commands work the same way as they do in the main program. This means they stay on until changed or turned off.
If a modal command is on when starting the subprogram, it will stay on while running the subprogram. The same is true when switching from the subprogram to the main program.
The safest thing to do is make sure your safety blocks cover the required codes for each section of code.
What is nesting in CNC programming?
So far we have concentrated on running a subprogram from our main program, but did you know you can also run a subprogram from within a subprogram?
This is called nesting.
Nesting can be a powerful tool but can quickly get out of control.
Most CNC controllers will allow up to four level deep nesting.
This means calling a subprogram in a subprogram in a subprogram in a subprogram in your main program.
Confusing right? That is why in most cases it isn’t a good idea to nest that deep. For most applications, one level deep is enough.
Even two level nesting can be hard to follow. Three and four levels are definitely not advised if you are reading this post.
The bulleted list below shows how subprogram structure works.
Tips for numbering your subprograms
Create a system for numbering your programs and subprograms to avoid confusion.
Some choose to set aside blocks of numbers for each type of program. For example, O0001-O4999 for main programs and O5000-O9999 for subprograms.
Others make their subprograms closely follow their main program numbers. If the main program is O1000 then the subprograms will be O1001, O1002 and so on.