Flexible support from samples to mass production

phone +86 13163919000email info@xinmeiintelligent.com

logo

Subprograms and Macro Programming

Advanced CNC Programming Techniques for Efficiency and Flexibility

Advanced CNC systems allow modular programming using subprogram calls, macro variables, conditional logic, and looping functions. In FANUC-based systems, macro programming significantly improves repeatability and reduces code redundancy, making it particularly effective for batch production or families of similar parts.

1Subprogram Calls (M98 / M99)

Subprograms enable programmers to write commonly used code sequences once and call them from any main program. This modular approach reduces program length, simplifies maintenance, and ensures consistency across multiple parts.

Calling a Subprogram (M98)

M98 calls a subprogram by its program number. The format M98 P1000 L3 calls program O1000 three times. The L parameter specifies the number of repetitions, making it ideal for repetitive operations like drilling hole patterns.

Returning from a Subprogram (M99)

M99 at the end of a subprogram returns execution to the main program at the line following the M98 call. Without M99, the controller would not know where to continue execution.

Nesting Subprograms

Subprograms can call other subprograms, creating nested structures. Most controllers support 4-10 levels of nesting. Deep nesting should be used carefully to maintain program readability and debugging capability.

Benefits of Subprogram Use

Subprograms reduce total program size, minimize the risk of transcription errors, and allow changes to be made in one location rather than updating every instance of repeated code throughout the main program.

2Macro Variables

Macro variables (also called custom variables or user variables) store numerical values that can be referenced and modified during program execution. They transform static CNC programs into dynamic, parametric programs.

Local Variables (#1 - #33)

Local variables are temporary and reset when the macro program ends. They are typically used for arguments passed to the macro (e.g., #1 = X value, #2 = Y value) and for intermediate calculations within the macro.

Common Variables (#100 - #199, #500 - #999)

Common variables retain their values across program calls. Variables #100-#199 are cleared at power-off, while #500-#999 are persistent and retain values even after the machine is turned off, making them useful for storing tool life data or production counters.

System Variables (#1000+)

System variables provide read/write access to machine parameters, tool offsets, work coordinates, and controller status. For example, #5021-#5023 read the current machine position in X, Y, Z coordinates.

3Conditional Logic (IF Statements)

Conditional logic allows CNC programs to make decisions during execution. This capability transforms linear programs into intelligent processes that can adapt to different conditions.

1

IF-THEN Structure

The basic IF-THEN structure evaluates a condition and executes a command if true. For example: IF [#1 GT 10] THEN #2 = 5 sets variable #2 to 5 only if #1 is greater than 10.

2

IF-GOTO Structure

IF-GOTO redirects program execution to a specific line number when a condition is met. For example: IF [#1 EQ 0] GOTO 100 jumps to block N100 if #1 equals zero. This enables branching logic for different machining scenarios.

3

Comparison Operators

FANUC macro programming supports EQ (equal), NE (not equal), GT (greater than), LT (less than), GE (greater than or equal), and LE (less than or equal) for condition evaluation.

4Looping Functions (WHILE-DO)

Looping functions allow a block of code to be executed repeatedly until a condition is met. This is essential for operations that require iteration, such as peck drilling to depth, spiral milling, or processing arrays of features.

WHILE-DO Loop

The WHILE-DO-END structure repeats a code section as long as the condition remains true. For example: WHILE [#1 LT 100] DO 1 ... #1 = #1 + 10 ... END 1 increments #1 by 10 each iteration until it reaches 100.

Counter-Based Loops

Using a variable as a counter, loops can execute a specific number of times. Initialize the counter, test it in the WHILE condition, and increment it within the loop body to control iteration count precisely.

Nested Loops

Multiple WHILE-DO loops can be nested using different DO-END identifiers (DO 1/END 1, DO 2/END 2, etc.). Nested loops are powerful for grid patterns, multi-layer machining, and complex repetitive geometries.

5Practical Applications

Macro programming unlocks significant productivity gains in real-world manufacturing scenarios where families of similar parts or complex repetitive operations are common.

  • Parametric programs for families of parts with varying dimensions — change a few variables to produce different sizes
  • Automatic tool wear compensation by reading tool offset variables and adjusting cutting parameters
  • Custom canned cycles tailored to specific machining operations not available in standard G-code
  • Production counting and tool life management using persistent common variables
  • Probing routines that measure features and automatically update work offsets or tool compensations

Conclusion

Subprograms and macro programming represent the advanced tier of CNC programming capability. By leveraging modular code structure, parametric variables, conditional logic, and looping functions, manufacturers can dramatically reduce programming time, improve consistency, and create intelligent machining processes that adapt to varying conditions. These techniques are essential for competitive manufacturing operations.