Haas CNC | Subprograms

In this article, we describe how to use M97 and M98 codes to call subprograms for Haas CNC machines and controllers with all details and examples.

0
548

Introduction

Subprograms (called with M97 or M98);

  • Are usually a series of commands that are repeated several times in a program.
  • Are written in a separate program, instead of repeating the commands many times
    in the main program.
  • Are called in the main program with an M97 or M98 and a P code.
  • Can include an L for repeat count. The subprogram call repeats L times before the
    main program continues with the next block.

When You Use M97

  • The P code (nnnnn) is the same as the block number (Nnnnnn) of the local subprogram.
  • The subprogram must be within the main program

When You Use M98

  • The P code (nnnnn) is the same as the program number (Onnnnn) of the subprogram.
  • The subprogram must reside in the active directory, or in a location specified in Settings 251/252.
  • Canned Cycles are the most common use of subprograms. For example, you might put the X and Y locations of a series of holes in a separate program. Then you can call that program as a subprogram with a canned cycle. Instead of writing the locations once for each tool, you write the locations only once for any number of tools.

External Subprogram (M98)

An external subprogram is a separate program that the main program references. Use M98 to command (call) an external subprogram, with Pnnnnn to refer to the program number you want to call.

You may be interested also:
“Haas CNC | G65 Code | Macro Subprogram Call Option”

When your program calls an M98 subprogram, the control looks for the subprogram in the main program’s directory. If the control cannot find the subprogram in the main program’s directory, it then looks in the location specified in Setting 251. Refer to page 396 for more information. An alarm occurs if the control cannot find the subprogram.

External Subprogram Example

In this example, the subprogram (program O40008) specifies (8) positions. It also includes a G98 command at the move between positions 4 and 5. This causes the Z Axis to return to the initial starting point instead of the R plane, so the tool passes over the workholding.

The main program (Program O40007) specifies (3) different canned cycles:

1. G81 Spot drill at each position
2. G83 Peck drill at each position
3. G84 Tap at each position

Each canned cycle calls the subprogram and does the operation at each position.

Main Program

%
O40007 (External subprogram ex-prog) ;
(G54 X0 Y0 is center left of part) ;
(Z0 is on top of the part) ;
(T1 is a spot drill) ;
(T2 is a drill) ;
(T3 is a tap) ;
(BEGIN PREPARATION BLOCKS) ;
T1 M06 (Select tool 1) ;
G00 G90 G40 G49 G54 (Safe startup) ;
G00 G54 X1.5 Y-0.5 (Rapid to 1st position) ;
S1000 M03 (Spindle on CW) ;
G43 H01 Z1. (Tool offset 1 on) ;
M08 (Coolant on) ;
(BEGIN CUTTING BLOCKS) ;
G81 G99 Z-0.14 R0.1 F7. (Begin G81) ;
M98 P40008 (Call external subprogram) ;
(BEGIN COMPLETION BLOCKS) ;
G00 Z1. M09 (Rapid retract, Coolant off) ;
G53 G49 Z0 M05 (Z home, Spindle off) ;
M01 (Optional stop) ;
(BEGIN PREPARATION BLOCKS) ;
T2 M06 (Select tool 2) ;
G00 G90 G40 G49 G54 (Safe startup) ;
G00 G54 X1.5 Y-0.5 (Rapid to 1st position) ;
S2082 M03 (Spindle on CW) ;
G43 H02 Z1. (Tool offset 1 on) ;
M08 (Coolant on) ;
(BEGIN CUTTING BLOCKS) ;
G83 G99 Z-0.75 Q0.2 R0.1 F12.5 (Begin G83) ;
M98 P40008 (Call external subprogram) ;
(BEGIN COMPLETION BLOCKS) ;
G00 Z1. M09 (Rapid retract, Coolant off) ;
G53 G49 Z0 M05 (Z home, Spindle off) ;
M01 (Optional stop) ;
(BEGIN PREPARATION BLOCKS) ;
T3 M06 (Select tool 3) ;
G00 G90 G40 G49 G54 (Safe startup) ;
G00 G54 X1.5 Y-0.5 (Rapid to 1st position) ;
S750 M03 (Spindle on CW) ;
G43 H03 Z1. (Tool offset 3 on) ;
M08 (Coolant on) ;
(BEGIN CUTTING BLOCKS) ;
G84 G99 Z-0.6 R0.1 F37.5 (Begin G84) ;
M98 P40008 (Call external subprogram);
(BEGIN COMPLETION BLOCKS) ;
G00 Z1. M09 (Rapid retract, Coolant off) ;
G53 G49 Z0 M05 (Z home, Spindle off) ;
G53 Y0 (Y home) ;
M30 (End program) ;
%

F5.19: Subprogram Pattern

Sub Program

%
O40008 (Subprogram) ;
X0.5 Y-0.75 (2nd position) ;
Y-2.25 (3rd position) ;
G98 X1.5 Y-2.5 (4th position) ;
(Initial point return) ;
G99 X3.5 (5th position) ;
(R plane return) ;
X4.5 Y-2.25 (6th position);
Y-0.75 (7th position) ;
X3.5 Y-0.5 (8th position) ;
M99 (sub program return or loop) ;
%

Local Subprogram (M97)

A local subprogram is a block of code in the main program that is referenced several times by the main program. Local subprograms are commanded (called) using an M97 and Pnnnnn, which refers to the N line number of the local subprogram.

The local subprogram format is to end the main program with an M30 then enter the local subprograms after the M30. Each subprogram must have an N line number at the start and a M99 at the end that will send the program back to the next line in the main program.

Local Subprogram Example

%
O40009 (Local subprogram ex-prog) ;
(G54 X0 Y0 is at the top left corner of part) ;
(Z0 is on top of the part) ;
(T1 is a spot drill) ;
(T2 is a drill) ;
(T3 is a tap) ;
(BEGIN PREPARATION BLOCKS) ;
T1 M06 (Select tool 1) ;
G00 G90 G40 G49 G54(Safe startup) ;
X1.5 Y-0.5 (Rapid to 1st position) ;
S1406 M03 (Spindle on CW) ;
G43 H01 Z1.(Tool offset 1 on) ;
M08(Coolant on) ;
(BEGIN CUTTING BLOCKS) ;
G81 G99 Z-0.26 R0.1 F7. (Begin G81) ;
M97 P1000 (Call local subprogram) ;
(BEGIN COMPLETION BLOCKS) ;
G00 Z0.1 M09 (Rapid retract, Coolant off) ;
G53 G49 Z0 M05 (Z home, Spindle off) ;
M01 (Optional stop) ;
(BEGIN PREPARATION BLOCKS) ;
T2 M06 (Select tool 2) ;
G00 G90 G40 G49 (Safe startup) ;
G54 X1.5 Y-0.5 (Rapid back to 1st position) ;
S2082 M03 (Spindle on CW) ;
G43 H02 Z1. (Tool offset 2 on) ;
M08(Coolant on) ;
(BEGIN CUTTING BLOCKS) ;
G83 G99 Z-0.75 Q0.2 R0.1 F12.5 (Begin G83) ;
M97 P1000 (Call local subprogram) ;
(BEGIN COMPLETION BLOCKS) ;
G00 Z0.1 M09 (Rapid retract, Coolant off) ;
G53 G49 Z0 M05 (Z home, Spindle off) ;
M01 (Optional stop) ;
(BEGIN PREPARATION BLOCKS) ;
T3 M06 (Select tool 3) ;
G00 G90 G40 G49 (Safe startup) ;
G54 X1.5 Y-0.5 ;
(Rapid back to 1st position) ;
S750 M03 (Spindle on CW) ;
G43 H03 Z1.(Tool offset 3 on) ;
M08(Coolant on) ;
(BEGIN CUTTING BLOCKS) ;
G84 G99 Z-0.6 R0.1 F37.5 (Begin G84) ;
M97 P1000 (Call local subprogram) ;
(BEGIN COMPLETION BLOCKS) ;
G00 Z0.1 M09 (Rapid retract, Coolant off) ;
G53 G49 Z0 M05 (Z home, Spindle off) ;
G53 Y0 (Y home) ;
M30 (End program) ;
(LOCAL subprogram) ;
N1000 (Begin local subprogram) ;
X0.5 Y-0.75 (2nd position) ;
Y-2.25 (3rd position) ;
G98 X1.5 Y-2.5 (4th position) ;
(Initial point return) ;
G99 X3.5 (5th position) ;
(R-plane return) ;
X4.5 Y-2.25 (6th position) ;
Y-0.75 (7th position) ;
X3.5 Y-0.5 (8th position) ;
M99 ;
%


Need to More?

Our volunteers have worked together and carefully prepared the articles published here in their native language without using machine translation. You can search the entire site for more information on the subject. You can start a discussion on CNCarea.com forums and join us to get support, ask questions, improve a published article or give your opinion.


Previous articleMitsubishi CNC | M70-M80 Series | Program Restart
Next articleCNC Milling | G86 Cycle | Boring