Subprograms and Macros in CNC: Mastering M98, M99, and Custom Logic
In modern CNC programming, subprograms and macros allow for efficient, modular, and smart code. They reduce duplication, simplify complex operations, and enable adaptive control logic within the G-code.
🔁 What Is a Subprogram?
A subprogram is a reusable block of code that can be called multiple times from the main program.
Benefits:
- Eliminate repetitive code
- Easier debugging and updates
- Looping capabilities
- Logical structure in part families
🔢 M98 and M99: Core of Subprogram Control
| Code | Function |
|---|---|
| M98 | Call subprogram |
| M99 | Return from subprogram / Repeat loop |
Syntax:
M98 P#### L##
P####: Subprogram number (O####)L##: Optional, repeat count
📘 Example: Drilling Multiple Holes with a Subprogram
Main Program (O0001):
O0001
G90 G54 G0 X0 Y0 Z5
M98 P1000 L4
M30
Subprogram (O1000):
O1000
G81 R2 Z-10 F200
X10 Y0
X10 Y10
X0 Y10
X0 Y0
G80
M99
✅ This drills 4 hole patterns using the same code.
🔄 Subprogram Nesting
Subprograms can call other subprograms, allowing nested logic:
O0001
M98 P1000
M30
O1000
M98 P2000
M99
O2000
G1 X10 Y10
M99
Be careful to avoid infinite loops!
🧠 Macro Programming Basics
Macros allow parametric control using variables, logic, and conditional branches.
Common system variables:
#1to#33: Local variables#100+: General-purpose user variables#500+: Persistent variables
🔧 Example: Parametric Peck Drilling
#101=5 (Start X)
#102=10 (Increment)
#103=4 (Repeats)
O0002
N10 G0 X[#101] Y0 Z5
G83 Z-15 R2 Q3 F150
#101=[#101+#102]
IF [#103 GT 1] THEN #103=[#103-1] (Decrement loop)
IF [#103 GT 0] GOTO10
M30
This creates a looped parametric drilling pattern with incremental positions.
🛠️ Conditional Logic Operators
| Operator | Meaning |
|---|---|
| EQ | Equal |
| NE | Not equal |
| GT | Greater than |
| LT | Less than |
| GE | ≥ |
| LE | ≤ |
Use IF and GOTO to direct program flow:
IF [#1 GT 5] GOTO100
📦 Real-World Use Case: Custom Tool Probing Cycle
Create a macro that:
- Lowers the tool
- Checks contact
- Stores Z-zero offset automatically
O9001 (Tool Probe Macro)
G91 G31 Z-50 F100 ; Probe Z down
#500 = #5063 ; Save contact Z in persistent var
G90
M99
Called via:
M98 P9001
🔄 Looping with M99 in Main Program
Using M99 in the main program (without M30) loops the entire program:
O0003
...
M99
Used for continuous unattended cycles like part conveyors or bar feeders.
🧪 Use Case: Part Family Machining
Use subprograms with parameters to make part families:
#100=20 (Slot length)
#101=5 (Depth)
M98 P2000
M30
O2000
G1 X[#100] Z-[#101] F100
M99
Just change #100 and #101 in the main program for different parts — no CAM work needed.
🧠 Best Practices
- Use clear subprogram names (O-codes)
- Reset local variables at the end of subs
- Always use
M99to return, notM30 - Test macros in simulation first
- Comment all macros thoroughly
🧭 Summary
Subprograms and macros are powerful tools in advanced CNC programming. They enable:
- Code reusability
- Logic-based decisions
- Flexible part families
- Efficient automation
By mastering M98, M99, variable handling, and conditional logic, CNC programmers gain greater control and scalability over their code — saving time, reducing errors, and automating complex machining sequences.
Leave a comment