0
0
Ev-technologyHow-ToBeginner · 3 min read

How to Use Subprogram in CNC: Syntax and Examples

In CNC programming, a subprogram is a separate block of code that can be called from the main program using M98 and returned with M99. This helps reuse code and organize complex tasks efficiently.
📐

Syntax

The basic syntax to call a subprogram in CNC is using M98 P[subprogram_number] where P specifies the subprogram number. The subprogram itself starts with O[subprogram_number] and ends with M99 to return control to the main program.

  • M98: Call subprogram
  • P[subprogram_number]: Subprogram identifier
  • O[subprogram_number]: Subprogram start label
  • M99: Return from subprogram
cnc
M98 P1000 ; Call subprogram 1000
...
O1000 ; Subprogram start
G01 X10 Y10 F100
M99 ; Return to main program
💻

Example

This example shows a main program calling a subprogram to move the tool to a position. The subprogram moves the tool and then returns control back to the main program.

cnc
O0001 (Main Program)
G21 (Set units to mm)
G90 (Absolute positioning)
M98 P1000 (Call subprogram 1000)
G00 X0 Y0 (Return to origin)
M30 (End program)

O1000 (Subprogram 1000)
G01 X50 Y50 F200 (Move to X50 Y50 at feed 200)
M99 (Return to main program)
Output
Tool moves to X50 Y50, then returns to origin (X0 Y0), program ends.
⚠️

Common Pitfalls

Common mistakes when using subprograms include:

  • Forgetting M99 at the end of the subprogram, causing the machine to not return to the main program.
  • Using incorrect subprogram numbers or labels, leading to errors or unexpected behavior.
  • Not setting proper positioning modes (absolute/relative) consistently between main and subprogram.

Always verify subprogram calls and returns to avoid machine stops or crashes.

cnc
O0001
M98 P1000 ; Call subprogram
M30 ; End

O1000
G01 X50 Y50 F200
; Missing M99 here causes no return to main program
📊

Quick Reference

CommandDescription
M98 P[subprogram_number]Call the subprogram with the given number
O[subprogram_number]Start label of the subprogram
M99Return from subprogram to main program
M30End of main program

Key Takeaways

Use M98 with P parameter to call a subprogram by its number.
End every subprogram with M99 to return control to the main program.
Label subprograms clearly with O followed by their number.
Check positioning modes to avoid unexpected tool movements.
Test subprogram calls carefully to prevent machine errors.