What does the M99 code do?
The M99 CNC code ends the current subprogram.
Once the machine reads the M99 code, it will return to the line in the program after the subprogram was called up.
If the subprogram was started using M97 (subprogram call by line number) then the machine will return to the line in the current program just after the M97 code.
If the subprogram was started using M98 (subprogram call by program number) then the machine will return to the line in the original program just after the M98 code was used.
Learn CNC Programming – It’s Easier Than You Think!
Learning G Code doesn’t have to be difficult…
If you know what to focus on.
Join our simple, easy-to-follow course, “G Code Made Easy: CNC Programming for Beginners“. We walk you through all the important codes – with simple explanations and real-world examples.
Want to become a super-skilled CNC programmer? Join now to take the shortcut to becoming a G Code Master today!Make Learning G Code Easy
Other codes used with the M99 code
- P – Selects the subprogram number to run. When used with M97, the P code selects the line in the current program to run. When used with M98, the P code selects the program number to run.
- N – Identifies the line number
- O – Identifies the program number
- M97 – Jumps to a line number in the program selected with the P code
- M98 – Jumps to a subprogram selected with the P code
- L – Number of times to repeat the subprogram
M97 vs M98
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.
What are subprograms used for?
Subprograms are a separate CNC program selected to run from within another program.
They are used to perform repetitive machining operations or sequences such as drilling, counterboring and countersinking a hole.
They can also be used for machine related functions such as preparing for a tool change.
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.
Differences between a subprogram and the main program
The commands to end the main program and a subprogram are different.
A subprogram ends with the M99 command.
It doesn’t matter if you use the M97 or M98 command to call your subprogram, M99 will return the machine to the next line in the original program after the M97 or M98 code was used.
The machine will continue to run code in the program from that point.
If an M30 code is used to end a subprogram then the machine will stop running. It will not return to the line after the subprogram call like it would when using the M99 command.
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 to 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.
Frequently asked questions about the M99 code
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
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 which is aimed at beginners.