CNC | M98 and M99 Codes | Subprogram Functions

CNC Subprogram Introduction

The length of a CNC program is usually measured in the number of characters such program contains. This number is similar to the number of bytes, if the program is stored on a computer disk. Physical length of a program is usually not an issue for most jobs. Program length will vary, depending on the complexity of work, number of tools used, method of programming and other factors. Generally, the shorter the program, the less time is needed to write it, and the less space it will occupy in CNC memory. Short programs also reduce the possibility of a human error, because they are easily checked, modified and optimized.

Virtually all CNC systems offer features designed to shorten the length of a program to some extent and make the programming process easier,more efficient and less prone to errors. Typical examples of this type of programming are fixed cycles, multiple repetitive cycles and custom macros. This article describes the structure, development and applications of another method of efficient program preparation – the use of subprograms.

Main Program and Subprograms

A CNC program is a series of instructions, assigned to different tools and operations. 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.

Each program must have its own program number and is stored in the control memory. Programmer uses special M-function to call one program from another. The first program that calls another program is called the main program, all other programs are called subprograms. Main program is never called by a subprogram – it becomes the top level of all programs. Subprograms can also be called from other subprograms, up to a certain number of nesting levels. When a program containing subprograms is used, always select the main program, never the subprogram. The only time a subprogram is selected at the control is for editing purposes. In some reference materials, subprograms are also called subroutines or macros, but the term subprogram is used most often and the word macro could have a different meaning altogether.

Subprogram Benefits

Any frequently programmed order of instructions or unchanging block sequences can benefit from becoming a subprogram. Typical applications for subprogram applications in CNC programming are:

  • Repetitive machining motions
  • Functions relating to tool change
  • Hole patterns
  • Grooves and threads
  • Machine warm-up routines
  • Pallet changing
  • Special functions
  • And others..

Structurally, subprograms are similar to standard programs. They use the same syntax rules and look and feel the same. Often, it may not be easy to see the difference between a regular program and a subprogram at a casual glance. A subprogram can use 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

Not every subprogram will provide all benefits, but even one benefit should be a good reason to use subprograms.

Identification of Subprograms

The first step towards a successful application of subprograms is the identification and isolation of repetitive programming sequences. For example, the next six program blocks represent a machine zero return for a typical horizontal machining center, at the start of program:

N1 G20
N7 …

These blocks represent a typical sequence of commands that will be repeated every time a new program for that machine is written. Such a program may be written many times a week, each time repeating the same sequence of instructions. To eliminate any possibility of an error, frequently used order of blocks can be stored as a separate program and identified by a unique program number. Then, it can be called up at the top of any main program. This stored programming sequence will become a subprogram – a branch, or an extension, of the main program.

M98 and M99 Codes Format

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

Subprogram call function M98 must always be followed by the subprogram number P–. Subprogram end function M99 terminates the subprogram and transfers processing back to program it originated from (a main program or a subprogram). Although M99 is mostly used to end a subprogram, it may also be rarely used in the main program, replacing the M30 function. In this case, the program will run ‘forever’, or until the Reset key is pressed.

M98 Subprogram Call Function

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 addresses to become effective:

  • Address P identifies the selected subprogram number
  • Address L or K identifies the number of subprogram repetitions ( L1 or K1 is the default )

For example, a typical subprogram call block includes the M98 function and the subprogram number:

N167 M98 P3951

In block N167, subprogram O3951 is called from CNC memory, to be repeated once – L1 (K1) counter is the default, depending on the control. Sub program must be stored in the control before being called by another program.

M98 blocks that call sub programs may also include additional instructions, such as rapid tool motions, spindle speed, feedrate, cutter radius offset number, etc. On most controls, if included in the same block as subprogram call, the additional data will be passed to the contents of the subprogram. The following subprogram call block also contains a tool motion in two axes:

N460 G00 X28.373 Y13.4193 M98 P3951

This block executes the rapid motion first, then it calls the subprogram. The order of words in a block makes no difference to the block execution:

N460 M98 P3951 G00 X28.373 Y13.4193

results in the same machining order as if the tool motion preceded the subprogram call, but looks illogical.

M99 Subprogram End Function

When the main program and subprogram coexist in the control, they must differ by their program numbers. During processing, they will be treated as one continuous program, so a distinction must be made for the program end function as well. The end of program function is M30 or, less frequently, M02. Subprogram must be terminated by a different function. Fanuc uses M99 for that purpose:

O3951 (SUB-1) Subprogram start

M99 Subprogram end

When a subprogram terminates, control returns program processing to the program of origin – it will not terminate the main program – that is the exclusive function of M30. Additional parameters may also be added to the M99 subprogram end, for example a block skip code [/], a block number to return to upon exit, etc. Note that the stop code symbol (the % sign) is used in the same manner for a subprogram, as for any main program. Subprogram termination is important and must always be done right. It sends two very important instructions to the control system:

  • To terminate the subprogram
  • To return to the block following subprogram call

Never use the program end function M30 (M02) to terminate a subprogram – it will immediately cancel all program processing and reset the control. Program end function does not allow program execution of any blocks beyond the block that contains it.
Normally, subprogram end M99 returns the processing to the block immediately following subprogram call M98. This concept is illustrated in Figure 39-2 (without block numbers).

Figure 39-2
Flow of a program processing with a single subprogram