Advanced CNC Subprograms and Loops: G22, G23, M98, M99, and Conditional Logic with Real Macros
CNC machines aren’t just motion devices — they’re programmable computers capable of conditional logic, looping, and modular routines.
By mastering subprograms and logic commands like G22, G23, M98, M99, and macro conditions (IF/WHILE), you can automate complex repetitive machining with clean, reusable code.
This guide provides real-world examples from Fanuc, Haas, Siemens, and Heidenhain systems.
📌 1. What Are CNC Subprograms?
Subprograms are reusable blocks of G-code stored in the control’s memory, usually named as:
O9000 – O9999
They are called using:
M98 Pxxxx Lnn
and ended with:
M99
| Command | Function |
|---|---|
| M98 | Call a subprogram |
| Pxxxx | Subprogram number |
| Lnn | Number of repetitions |
| M99 | Return to main program or loop back |
📌 2. Basic Fanuc Example — Subprogram Call
Main Program (O1000)
%
O1000 (MAIN PROGRAM)
G90 G17 G21
M98 P2000 L3
M30
%
Subprogram (O2000)
%
O2000 (DRILL CYCLE)
G81 Z-15. R2. F100
G80
M99
%
Executes subprogram O2000 three times using
L3.
📌 3. Haas Example — Multi-Hole Pattern with M98/M99
%
O3000 (HAAS MAIN)
T01 M06
M98 P4000 L5
M30
%
%
O4000 (SUBPROGRAM)
G81 X[#100] Y[#101] Z-20. R2. F150
#100 = [#100 + 20]
M99
%
Drills five holes 20 mm apart in X using a loop variable.
📌 4. Nested Subprogram Calls (Fanuc)
Subprograms can call other subprograms:
M98 P2000
...
O2000
M98 P3000
M99
O3000
(SECONDARY SUBROUTINE)
M99
Enables modular machining — for example, one subprogram for drilling, another for tapping.
📌 5. M99 — Return or Loop Back
| Context | Behavior |
|---|---|
| Inside subprogram | Returns to caller |
| Inside main program | Loops to start |
Example:
O5000
G01 X100.
M99
When executed directly,
M99loops infinitely (use with caution).
📌 6. G22 & G23 — Axis Limit Control
| Code | Function |
|---|---|
| G22 | Enable soft limit check |
| G23 | Disable soft limit check |
Example:
G22 X-100. X100.
G00 X150. (ALARM)
G23
G00 X150. (NOW OK)
G22 protects from exceeding the soft travel range — ideal for automation and unmanned cycles.
📌 7. WHILE / END Loop — Repetition Without M98
#100 = 0
WHILE [#100 LT 5] DO1
G81 X[#100*20] Y0 Z-10. R2. F150
#100 = [#100 + 1]
END1
M30
Repeats drilling operation 5 times without subprogram call — fully contained logic loop.
📌 8. IF / GOTO Conditional Logic
#500 = 45
IF [#500 GT 50] GOTO 100
G01 Z-10. F100
N100 G01 Z-20. F150
Executes different depths based on variable condition — foundational to CNC decision-making.
📌 9. Real Example — Automatic Pattern Generator
#100 = 0
#101 = 5 (HOLE COUNT)
#102 = 25 (SPACING)
WHILE [#100 LT #101] DO1
M98 P3000
#100 = [#100 + 1]
END1
M30
O3000
G81 X[#100 * #102] Y0 Z-20. R2. F120
G80
M99
Dynamically creates hole patterns based on variables — zero code duplication.
📌 10. Siemens Example — LBL CALL & GOTO
LBL 1
CYCLE83(DEPTH=-20, PECK=5)
LBL CALL 1 REP=5
LBL 0
Equivalent to M98 loop logic — Siemens uses
LBL(label) instead of O numbers.
📌 11. Heidenhain Example — CALL LBL
LBL 1
CYCL DEF 200 DRILLING Q201=-20 Q206=+150
LBL CALL 1 REP4
LBL 0
Executes subroutine 4 times — Heidenhain subprogram syntax is clean and structured.
📌 12. Mazak Example — Smooth Control Subprogram
M98 P8001 L10
O8001
G81 Z-10. R2. F100
M99
Standard ISO-compatible M98/M99 subroutine control in Mazak SmoothX systems.
📌 13. Conditional Tool Change Macro (Fanuc)
#500 = [#500 + 1]
IF [#500 GE 10] THEN #3006 = 1 (TOOL CHANGE REQUIRED)
After 10 cycles, display message to change the tool — perfect for unattended machining.
📌 14. Recursive Subprogram Example (Advanced)
O7000
#100 = [#100 + 1]
IF [#100 GT 5] GOTO 100
G01 X[#100*10] F200
M98 P7000
N100 M99
Calls itself recursively until the condition breaks — caution: ensure escape logic exists.
📌 15. Preventing Machine Overtravel with G22/G23
G22 X-100. X100. Z-200. Z0.
G00 X150. (OVERTRAVEL ALARM)
G23
G00 X150. (SAFE MOVE)
Enables software-defined movement limits to protect axes during automation.
📌 16. Example — Automatic Drill Array with Limit Check
#100 = 0
WHILE [#100 LT 6] DO1
IF [#100 GT 4] GOTO 200
G81 X[#100*20] Y0 Z-20. R2. F150
#100 = [#100 + 1]
END1
N200 M30
Automatically stops drilling when position exceeds limit — combines IF and WHILE logic.
📌 17. AI-Driven Macro Trends (2025–2030)
- Self-optimizing macros: AI adjusts cycle count based on tool wear sensors.
- Dynamic limit checking (G22++): Real-time collision prediction.
- Loop learning systems: Adaptive repetition based on load/temperature data.
- Cloud-linked subprograms: Shared machining templates across multiple CNCs.
- Predictive conditional logic: Machines make their own “if” decisions using vibration and torque analytics.
✅ Conclusion
By mastering subprograms, loops, and conditional logic, you elevate CNC programming from manual code repetition to smart, modular automation.
G22/G23 protect your hardware, M98/M99 organize your logic, and macros give your CNC the ability to think, adapt, and repeat perfectly — every single time.
Leave a comment