Skip to main content
CNC.wiki

Guide to Custom Macro B Programming: Fanuc CNC Optimization

Master Fanuc Custom Macro B programming. Learn syntax, parameters 6001#6 and 6001#7, and resolve alarms 112 and 118 to prevent turret and chuck crashes.

Hakan Gündoğdu
Hakan Gündoğdu

CNC CARE Co-founder

Introduction

An uncommanded motion driving the turret or spindle out of bounds and plunging the tool directly into a vise jaw, a secured clamp, or the chuck is the immediate hazard of neglecting variable persistence after a machine reset. When a CNC operator interrupts a Custom Macro B execution and presses reset, they assume the memory clears. However, if parameter settings retain stale local or common variables rather than purging them to vacant, restarting the cycle causes the CNC interpreter to run calculations using corrupted data. The operator sees the axis deviate off-path, resulting in a hard collision, a scrap part, and severe mechanical damage. Preventing these failures requires a precise understanding of variable persistence parameters, arithmetic syntax constraints, and proper error handling on Fanuc controls.

Technical Summary

Technical SpecificationDetails
Command CodesG65, G66, G66.1, G67
Modal Group / ModalityModal / Non-modal (G65 is non-modal, G66/G66.1 are modal, G67 cancels)
Brands CoveredFanuc
Critical Parameters6000#5 (SBM), 6000#3 (V15), 6001#5 (TCS), 6001#6 (CCV), 6001#7 (CLV)
Main ConstraintArithmetic precision of 8 decimal digits; bracket nesting max 5 levels; loop depth max 3 levels; variable range ±10-29 to ±1047.

Quick Read

  • Reset Memory Purging: Set parameters 6001#6 (CCV) and 6001#7 (CLV) to 0 to automatically clear local and common variables to vacant upon machine reset, preventing coordinate corruption.
  • Execution Stop Control: Set parameter 6000#5 (SBM) to 1 to enable single block stops during macro statement execution, allowing safe step-by-step program verification.
  • T-Code Automation: Configure parameter 6001#5 (TCS) to 1 to automatically call macro O9000 with a T-code, passing the tool number into variable #149.
  • Nesting and Loop Limits: Limit loop nesting depth (DO-END) to 3 levels and bracket nesting to a maximum of 5 levels to avoid triggering immediate format alarms.
  • Offset Backward Compatibility: Set parameter 6000#3 (V15) to 1 to use legacy Series 15 tool offset variable addresses on modern Series 16/18 controls.
  • Division Safety Checks: Ensure all arithmetic division routines verify that the divisor is non-zero to prevent zero-divide alarms (PS0112) from halting operations mid-cycle.

Basic Concepts

The practical programming effect of Fanuc Custom Macro B is the transformation of static G-code into a highly dynamic, mathematical programming language. Rather than writing a unique program for every single part dimension, programmers can create parametric routines where variables dictate the X, Y, and Z coordinates. The operator simply inputs the desired dimensions into a single G65 macro call block, and the CNC calculates the required toolpaths on the fly. Furthermore, because macros can read System Variables (such as #5021 for machine coordinates or #5061 for skip signal positions), the machine can perform logic-based decisions, inspect its own probing results, and automatically recalculate tool wear offsets without any human intervention.

This parametric control is similar to standard subprogram calls but with the added power of math and logical operations. While basic machine shops might rely on writing and calling subprograms to repeat static paths, Custom Macro B allows variables to dynamically calculate coordinates. This represents a major leap in automation beyond traditional canned cycles such as g84 g74 rigid tapping or m98 nested subprograms, as variables can change feedrates, depths, and counts on the fly.

Command Structure

The command structure of Custom Macro B relies on a systematic syntax to pass data from a parent program into a macro subprogram. When a simple macro call is executed using G65, the control opens the target program and populates local variables (typically #1 through #33) with arguments passed in the calling block. The syntax maps specific alphabetical addresses to their corresponding local variables. For instance, address A maps to variable #1, B maps to #2, and C maps to #3, allowing the programmer to transfer workpiece dimensions or machining tolerances directly. Once inside the macro, the interpreter executes the mathematical logic and coordinate shifts before returning control.

For repetitive or continuous operations, a modal macro call can be initialized using G66 or G66.1. Under G66, the control executes the macro program immediately after every motion command block, while G66.1 triggers the macro after every single program block. This modal behavior remains active, passing updated variables, until the control reads a G67 cancellation command. Aliasing G-codes and M-codes via parameter mappings allows these macros to run invisibly. The CNC interpreter translates a standard code to execute O9000-series subprograms while passing variables in the background.

Command Syntax Addresses:

  • Simple Macro Call: G65 P_ L_ <arguments>
  • Modal Macro Call (after motion): G66 P_ L_ <arguments>
  • Modal Macro Call (every block): G66.1 P_ L_ <arguments>
  • Cancel Modal Macro Call: G67
  • Conditional Branch: IF [<conditional expression>] GOTO n
  • Conditional Loop: WHILE [<conditional expression>] DO n ... END n
Parameter / VariableDescriptionValue / Range
#1 to #33Local variables mapped to alphabetical addresses (e.g. A maps to #1, B to #2). Cleared on reset based on parameter 6001#7.±10-29 to ±1047
#100 to #149Common variables. Cleared or retained on reset based on parameter 6001#6.±10-29 to ±1047
#500 to #531Common variables. Always retained on reset.±10-29 to ±1047
Parameter 6000#5 (SBM)Single block stop during macro statement. 1: Valid, 0: Invalid.0 or 1
Parameter 6000#3 (V15)Tool compensation variables mapping. 1: legacy FS15 spec, 0: standard FS16 spec.0 or 1
Parameter 6001#5 (TCS)Call custom macro O9000 by T-code. T-code value passed to variable #149. 1: Enabled, 0: Disabled.0 or 1
Parameter 6001#6 (CCV)Clearing of common variables #100 to #149 on reset. 0: Cleared to vacant, 1: Retained.0 or 1
Parameter 6001#7 (CLV)Clearing of local variables #1 to #33 on reset. 0: Cleared to vacant, 1: Retained.0 or 1

Brand Applications

Fanuc

Fanuc profoundly distinguishes its macro architecture from other control brands through its seamless aliasing integrations and embedded diagnostic feedback. First, Fanuc allows machine builders and programmers to alias standard G, M, S, and T codes to point directly to custom macros. By simply setting a parameter (like parameter 6050 for G-codes or enabling TCS for T-codes), an operator can command a standard turret index, and the control will silently execute a complex O9000-series macro in the background while automatically passing the tool number into variable #149. Second, Fanuc natively integrates custom macro generation of alarms and messages. By programming a value to system variable #3000, the macro can intentionally halt the machine and project a highly specific, customized text string directly onto the CNC's alarm screen to alert the operator of a part-sizing error. Finally, Fanuc provides extreme backward compatibility via parameter V15; operators can instruct modern controls to map variables identically to decades-old Series 15 architectures, ensuring legacy macros run flawlessly on new machinery.

Brand Comparison

Feature / VersionFanuc Series 15 / Legacy ControlsFanuc Series 16 / 18 / 21 / 0i (Modern)
Tool Compensation System VariablesUses legacy tool offset variable mapping.Uses modern standard variable mapping: #2001–#2200 for machining centers, but can toggle legacy emulation using parameter 6000#3 (V15) set to 1.
Lathe vs. Machining Center MappingRequires legacy specific addresses for compensation.M-series uses contiguous variables #2001–#2200. T-series splits offsets into #2001–#2064 (X wear), #2101–#2164 (Z wear), and #2201–#2264 (tool nose wear).
Variable Reset BehaviorVolatile variables cleared automatically on machine reset.Enables variable retention config. Parameters 6001#6 (CCV) and 6001#7 (CLV) dictate if #100–#149 and #1–#33 are cleared to vacant (0) or retained (1) on reset.

Technical Analysis

An analysis of Fanuc's Custom Macro B architecture reveals a design focused on backward compatibility and safety configuration. The primary difference between Lathe (T-series) and Machining Center (M-series) applications lies in their system variable offset mapping. The M-series utilizes a unified, contiguous array from #2001 to #2200. The T-series segregates offsets to accommodate multi-axis lathe geometries. It separates X wear (#2001–#2064), Z wear (#2101–#2164), and tool nose radius wear (#2201–#2264). This segregation prevents coordinate conflicts during turning operations but requires programmers to write axis-specific macros depending on the machine type.

Backward compatibility is maintained through parameter 6000#3 (V15). Toggling this parameter to 1 instructs a modern Series 16 or 18 controller to translate variable addresses to match legacy Series 15 hardware. This allows machine shops to run decades-old macro programs without rewrite.

Furthermore, memory clearing behaviors are parameter-driven. In standard configurations, local and common variables purge on reset to prevent stale data execution. However, setting parameter 6001#6 (CCV) to 1 retains common variables. Similarly, setting parameter 6001#7 (CLV) to 1 retains local variables, which can lead to coordinate shifts if a cycle is restarted mid-program.

Program Examples

Fanuc G-Code Example

O9010 (FANUC CUSTOM MACRO B EXAMPLE) ;
#100 = #1 + #2 ;
IF [#100 GT 10.0] GOTO 5 ;
#1 = 1.0 ;
WHILE [#1 LT 5.0] DO 1 ;
G91 G01 Z-2.0 F150 ;
G90 G01 X#100 F300 ;
G00 Z2.0 ;
G00 X0 ;
#1 = #1 + 1.0 ;
END 1 ;
GOTO 10 ;
N5 #3000 = 1 (SUM EXCEEDS LIMIT) ;
N10 M99 ;

Dry Run Execution

During a dry run of the G65 macro program, the controller receives the command G65 P9010 A1.0 B2.0. Address A maps to local variable #1 (1.0) and B maps to #2 (2.0). The interpreter calculates the sum of #1 and #2, storing 3.0 in common variable #100. It checks the condition IF [#100 GT 10.0]. Since 3.0 is not greater than 10.0, the branch to block N5 is bypassed. Variable #1 is then initialized to 1.0 as the loop counter. The WHILE [#1 LT 5.0] DO 1 loop begins. The tool executes an incremental plunge of Z by 2.0 mm, moves to absolute position X3.0, retracts, and returns to X0. Variable #1 increments by 1.0. This loop repeats four times (for #1 = 1.0, 2.0, 3.0, and 4.0) until #1 reaches 5.0. At that point, the loop terminates. The interpreter jumps to block N10 via GOTO 10, executing M99 to return control to the main program.

Error Analysis

Brand LabelAlarm CodeTrigger ConditionOperator SymptomRoot Cause / Fix
Fanuc112 (PS0112)ZERO DIVIDE: Division by zero was specified within a macro arithmetic statement.The CNC controller halts instantly mid-cycle and displays a "ZERO DIVIDE" alarm screen.Ensure the divisor variable is not zero. Add a conditional check before division.
Fanuc114 (PS0114)FORMAT ERROR IN MACRO: Undefined H code in a G65 block, or expression contains an illegal format.The interpreter halts execution and projects a "FORMAT ERROR IN MACRO" message.Check mathematical expression formatting, verify brackets match, and ensure G65 address codes are valid.
Fanuc116 (PS0116)WRITE PROTECTED VARIABLE: A read-only variable is placed on the left side of a substitution statement.The machine locks up, preventing axis motion, and displays a "WRITE PROTECTED VARIABLE" error.Do not attempt to write data to read-only system variables (such as coordinate status or skip signals).
Fanuc118 (PS0118)TOO MANY BRACKET NESTING: Bracket nesting [] exceeds the quintuple limit (5 levels deep).The controller fails to parse the block, triggering "TOO MANY BRACKET NESTING" and stopping.Reduce the depth of nested brackets in mathematical expressions to 5 levels or fewer.
Fanuc124 (PS0124)MISSING END STATEMENT: A DO command is programmed without a corresponding END command.The NC execution halts with a "MISSING END STATEMENT" alarm before starting the loop.Ensure every DO block has a matching END statement with a matching index (e.g. DO 1 and END 1).

Application Note

A catastrophic hard collision that destroys a carbide tool, scraps the workpiece, and damages the spindle is the direct mechanical consequence of leaving parameter 6001#6 (CCV) or parameter 6001#7 (CLV) configured to retain variable data on a Fanuc control. If these parameters are set to 1, local and common variables do not clear to vacant when the operator presses the reset button. When a macro program is interrupted mid-cut and restarted, the CNC interpreter reads the stale, uncleared coordinates. This corrupted mathematical state causes the machine to calculate an incorrect coordinate shift. The resulting uncommanded motion drives the turret or spindle out of bounds, plunging the tool directly into a secured clamp, a vise jaw, or the chuck. Operators must establish a strict policy of setting variable retention parameters to 0 to enforce automatic memory purging on reset.

Related Command Network

  • M98 (Subprogram Call): Executes standard static subprograms without passing local variables or evaluating arithmetic logic, providing a simpler alternative to G65 calls. Refer to our guide on m98 nested subprograms for details.
  • M198 (External Device Subprogram Call): Calls subprograms stored on external media, such as CF cards or server paths, allowing machines to run larger files than can fit in internal memory. See our guide on writing and calling subprograms for subprogram workflows.
  • BPRNT / DPRNT (External Data Output): Outputs variable values and formatting text to external computers or serial devices, allowing programmers to log probing data and cycle metrics.

Conclusion

Implementing a rigorous parameter audit is the most reliable method for ensuring safe, crash-free operation of Custom Macro B on Fanuc CNC machinery. Keeping parameters 6001#6 (CCV) and 6001#7 (CLV) set to 0 ensures that all volatile memory registers purge to vacant upon reset, preventing coordinate bleeding. Combining these parameters with single block stop validation (parameter 6000#5) and structured variable safety boundaries protects the physical machine components, scrap rates, and tooling during parametric production runs.

Frequently Asked Questions

How do parameters 6001#6 and 6001#7 affect safety during custom macro execution?

These parameters control the persistence of local and common variables when a reset is triggered. If they are configured to retain variables rather than clearing them, an interrupted macro cycle will retain stale mathematical coordinates that can miscalculate the next coordinate shift on restart, causing collisions. Practical Action: Access parameter settings on the Fanuc control and set 6001#6 (CCV) and 6001#7 (CLV) to 0 to force variables to clear to vacant upon reset.

Why does the Fanuc control trigger Alarm 118 (TOO MANY BRACKET NESTING) in a macro statement?

Fanuc Custom Macro B restricts bracket nesting [] in mathematical and logical expressions to a maximum depth of 5 levels. Exceeding this limit prevents the interpreter from parsing the expression and halts execution. Practical Action: Restructure complex equations by breaking them down into separate, sequential steps, storing intermediate calculations in temporary variables like #100 or #101.

How can we call a custom macro program automatically when performing a tool change?

Setting parameter 6001#5 (TCS) to 1 enables the T-code macro call feature, which triggers macro O9000 whenever the control reads a T-code. The tool number is passed automatically to common variable #149 as an argument. Practical Action: Enable TCS and map the tool-change sequence (e.g. indexing the turret or changing tool offset) inside program O9000, calling the physical turret rotation while using variable #149.

Still not resolved?

Ask our AI assistant about this topic in natural language. Grounded in verified sources, no hallucinations.

Ask AI Assistant
Hakan Gündoğdu
Hakan Gündoğdu
  • CNC CARE Co-Founder (May 2025 - Present)
  • Mitsubishi Electric NC Sales & Service Section Manager (2008 - 2025)
  • Reis CNC Service Engineer (2003 - 2005)
  • Ören Kalıp CNC Mold Line Team Leader (1999 - 2002)

With over 25 years of experience working in all areas of the CNC machine industry, I continue my activities as a co-founder of CNC CARE, where we offer brand-independent consulting, engineering, and original spare parts services.

Related Articles

Other articles on this topic