M97 CNC Code Explained: An Easy Intro for Beginners [Subprogram Call by Line]

What does the M97 code do?

The M97 CNC code is a subprogram call by line number.

This means that the machine will run a subprogram contained within the main program.

The machine will jump to the line number in the current program that is called out with the M97 code.

At the end of the subprogram, the M99 code will be used. This signifies the end of the subprogram.

Once the machine reads the M99 code, it will return to the line in the program after the initial M97 code.

illustration that shows the flow of a cnc program when using the m97 command to call a subprogram

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 M97 code

Format for using an M97 code

Here is an example of an M97 code in use:

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 code it will return to the line after the example shown above.

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.

The main CNC program ends with either M30 code (for most machines) or M02 (for older machines). No further code will be run after either of these codes.

A subprogram ends with the M99 command

When using the M97 command, M99 returns the machine to the line which called the subprogram. The machine will continue to run code in the program from that point.

Let’s look at that program flow again to see how the machine moves from the subprogram call (M97) to the subprogram and back to the main program after the M99 code.

illustration that shows the flow of a cnc program when using the m97 command to call a subprogram

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. Set the correct modes when you need them.

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.

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.

M98 Code Flow

Frequently asked questions about the M97 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.

Want to learn more about CNC G Code?

Leave a Comment