1. Introduction to Subprogram in CNC Turning
In CNC turning, a subprogram is a small program
used inside the main program. It is also called a subroutine. I use a
subprogram when the same machining operation needs to be performed multiple
times. Instead of rewriting the same code, I write it once and call it whenever
required.
In turn, this is very useful for operations
like:
• Repeated grooves
• Multiple shoulders
• Step turning
• Repeated drilling
2.
Purpose of Subprogram in Turning
The main purpose is to reduce repetition.
For example, if I need to make 3 identical grooves on a shaft, I do not write the groove code 3 times. I write it once and call it 3 times. It also reduces mistakes. If I change one value, it updates everywhere. It also makes the program easy to read and simple.
3. Basic Format in CNC Turning
Main Program
O1104;
G21 G90 G54;
G00 X50.0 Z5.0;
M98 P2000 L3;
M30;
Subprogram
O2000;
G01 X30.0 Z-10.0 F0.2;
G00 X50.0;
M99;
Explanation (Turning View)
- Tool moves to cut diameter
- Same cut repeats 3 times
- Used for repeated turning operations
- M98
→ Call subprogram
- P2000
→ Subprogram number
- L3
→ Repeat 3 times
- M99
→ Return to main program
- M30
→ End program
Steps I follow:
- I identify repeated operations (like groove or step)
- I write it as a subprogram
- I give a number (O1104)
- I call it using M98
- I changed position in the main program
Example: Multiple Step Turning
Main
Program
O1000;
G21 G90;
G00 X40.0 Z5.0;
G00 Z0.0;
M98 P2000;
G00 Z-20.0;
M98 P2000;
G00 Z-40.0;
M98 P2000;
M30;
Subprogram
O2000;
G01 X30.0 F0.25;
G00 X40.0;
M99;
Explanation:
- Main program changes Z position
- Subprogram performs the same turning cut
- Used for step turning
Example: Groove Cutting Using Subprogram
Main
Program
O1000;
G00 X50.0 Z5.0;
G00 Z-10.0;
M98 P3000;
G00 Z-20.0;
M98 P3000;
G00 Z-30.0;
M98 P3000;
M30;
Subprogram
O3000;
G01 X20.0 F0.15;
G00 X50.0;
M99;
Explanation:
- The same groove is cut at different positions
- Saves time and code length
M98
(External Subprogram)
Most common method
M98 P2000 L3;
M97
(Local Subprogram)
Used inside the same program
M97 P100 L2;
P100 is a line number
7. Real-Life Machining Scenario (Shaft Production)
In industries such as automotive or machine manufacturing, shafts often require
multiple identical steps or grooves at different positions.
For example:
A motor shaft of length 150 mm needs:
·
3
identical steps at Z = -30, -60, -90
·
Each step
reduces the diameter from 50 mm to 40 mm
Instead of writing the same cutting code 3
times, I:
·
Write one
subprogram for step cutting
·
Call it at
different Z positions
Benefit in industry:
• Reduces programming time
• Ensures all steps are identical
• Improves production consistency
• Easy to modify (change once, apply everywhere)
This is very common in mass production, where
hundreds of shafts are machined daily.
8. Common Mistake + Correction (Very Important
for Students)
Common Mistakes in Subprogram Usage
Mistake:
Not retracting the tool before returning from the subprogram
O2022;
G01 X30.0 F0.2;
M99;
Problem:
When the subprogram is called again at a new Z position, the tool is still at the
cutting diameter (X30).
This can cause:
• Tool crash
• Wrong machining
• Surface damage
Correction (Proper Way):
O2023;
G01 X30.0F0.2
G00 X50.0 (Tool retraction)
M99;
Why this works:
• Tool safely moves away from workpiece
• Next call starts safely
• Prevents collision
- Reduces program length
- Saves time
- Easy to edit
- Reduces errors
- Good for repeated cuts
10. Precautions in Turning
- Check tool position (X and Z)
- Use correct offsets
- Ensure safe return position
- Always use M99 in subprogram
- Run a dry run before machining
Example mistake:
If the tool is not retracted, it may crash in the next call
In production environments, subprograms are not just optional—they are essential for efficiency and consistency. In CNC turning, subprograms are very useful. I use them for repetitive operations such as step turning, grooving, and drilling. They make my program short and simple. They save time and reduce errors. With practice, I can easily use subprograms to improve machining efficiency.
Note: “I strongly
request that all the above programs be carefully checked and dry-run before
execution to prevent tool damage or machine errors.”
Important FAQs on Subprogram in CNC Turning
1. What is a subprogram in CNC turning?
A subprogram is a smaller program used inside a
main program to perform repeated machining operations.
2. Why do we use subprograms in CNC?
To avoid writing the same code multiple times,
reduce errors, and make programs shorter and easier to manage.
3. What is the difference between the main
program and the subprogram?
·
The main program controls overall machining
·
Subprogram
performs repeated specific operations
4. What does M98 do in CNC turning?
M98 is used to call a subprogram from the main program.
5. What is the use of P and L in M98?
·
P → Subprogram number
·
L → Number of repetitions
Example: M98 P2000 L3 → Calls subprogram 3
times
6. What does M99 do?
M99 returns control from the subprogram back to
the main program.
7. What happens if M99 is not used?
The program may not return properly and can
cause errors or unexpected looping.
8. What is the difference between M98 and M97?
·
M98 → Calls external subprogram
·
M97 → Calls local subprogram within same program
9. Where are subprograms commonly used in
turning?
·
Step
turning
·
Grooving
·
Threading
cycles
·
Repeated
drilling operations
10. What precautions should be taken while
using subprograms?
·
Ensure
safe tool position before call
·
Use
correct offsets
·
Always
retract the tool properly
·
Perform a dry
run before machining
