How to Use Subprograms (M98/M99) in CNC G-Code: Loops, Calls, and Counters
Meta Description: Learn to use M98 and M99 for calling subprograms in CNC machining. Includes real G-code examples, loop logic, counters, and best practices to improve productivity and automation.
🔍 What Are Subprograms in CNC?
Subprograms are reusable blocks of G-code stored separately from the main program. They simplify code, reduce repetition, and allow for flexible changes without rewriting.
- M98: Calls a subprogram
- M99: Returns to main program or loops
📘 Basic Subprogram Call Syntax
Main Program: O0001 M98 P1000 Subprogram: O1000 G0 X10 Y10 G1 Z-5.0 F200. G1 X50. M99
This example runs subprogram O1000 once, executing the specified moves, and then returns to the main program at M99.
🔁 Using M98 with Repeat Count
M98 Pxxxx Lnn runs the subprogram multiple times. The L value defines the number of repetitions.
M98 P1000 L5
This runs subprogram O1000 five times.
📘 Subprogram Nesting and Multi-Level Calls
You can call a subprogram from another subprogram. Most controllers allow 2–4 levels of nesting.
Main Program: O0001 M98 P2000 Subprogram O2000: M98 P3000 M99 Subprogram O3000: G0 X0 Y0 G1 X50 Y50 M99
⚠️ Be cautious: excessive nesting can cause memory overflows or logic errors.
📊 Subprogram Use Case Table
| Function | G-Code | Used For | Note |
|---|---|---|---|
| Call Subprogram | M98 Pxxxx | Call external code | Use P for subprogram ID |
| Repeat Call | M98 Pxxxx Lnn | Loops | Efficient for bolt circles, hole patterns |
| Return from Subprogram | M99 | Return or loop | In subprogram, returns to main |
📘 Example: Bolt Hole Pattern with Subprogram
O0001 (Main Program) G0 X0 Y0 Z5 M98 P2000 L6 M30 O2000 (Subprogram) G81 R1.0 Z-10.0 F100. X30 Y30 M99
💡 This drills the same hole at 6 positions by calling subprogram O2000 six times.
🧠 Looping with M99 (Infinite Loops)
Sometimes, M99 is used without a main program call to loop the same subprogram repeatedly.
O9000 G0 X0 Y0 G1 X100 Y100 M99
This loops endlessly unless stopped manually. It’s useful in automation or until external sensor signals.
🧩 Best Practices for Subprogram Design
- Use clear and unique subprogram numbers (e.g., O1000, O2000)
- Include comments for traceability
- Keep subprograms short and reusable
- Avoid deep nesting unless necessary
📈 How Subprograms Improve CNC Productivity
- Reduce program size and memory usage
- Enhance repeatability of machining tasks
- Speed up program editing and debugging
- Support for automation and job scheduling
🔧 Sample Workflow with Subprograms
- Define the core motion in a subprogram
- Use
M98with repeat count to run it as needed - Keep spindle and coolant commands in the main program
O0001 G90 G40 G21 M6 T3 M3 S1200 M8 M98 P5000 L10 M5 M9 M30 O5000 G0 X10 Y10 G1 Z-2.0 F150. G1 X60 G0 Z5.0 M99
📌 Summary
Using M98 and M99 efficiently lets you modularize your G-code, reduce redundancy, and increase flexibility. In modern smart factories and with CAM post-processors, subprograms remain highly valuable for scalable, automated, and error-resilient machining operations.
Leave a comment