The macro language is a programming language that gives the CNC programmer the ability to write very flexible programs. This is done through the use of variables, mathematical expressions and program flow control statements. The macro language combined with standard G-code programming can create reusable programs much like canned cycles. These programs can do many useful things like custom pocketing or automatic tool measurement.
Macro variables
Variables are designated with the “#” symbol and are followed by a number or expression that designates the variable number. Variables can used as the value following any letter address except “N”.
Variables are classified into four different types.
Variable number | Type of variable | Function |
---|---|---|
#0 | Always null | This variable is always null. No value can be assigned to this variable. It is not a value, it is nothing/empty/null. |
#1 – #33 | Local variables | Local variables can only be used within a macro to hold data such as the results of operations. When the power is turned off, local variables are initialized to null. When a macro is called, arguments are assigned to local variables. These should only be used to pass values, not for calculations |
#100 – #149 (#199) #500 – #531 (#999) | Common Variables | Common variables can be shared among different macro programs. When the power is turned off, variables #100 to #149 are initialized to null. Variables #500 to #531 hold data even when the power is turned off. As an option, common variables #150 to #199 and #532 to #999 are also available. |
#1000 + | System variables | System variables are used to read and write a variety of NC data items such as the current position and tool compensation values. |
Macro operations
The operations listed in the table below can be performed on variables. The expression to the right of the operator can contain constants and/or variables combined by a function or operator. Variables #j and #K in an expression can be replaced with a constant. Variables on the left can also be replaced with an expression.
operation | Format | Remarks |
---|---|---|
Definition | #i=#j | |
Sum | #i=#j+#k; | |
Difference | #i=#j–#k; | |
Multiply | #i=#j*#k; | |
Divide | #i=#j/#k; | |
OR | #i=#j OR #k; | A logical operation is performed on binary numbers bit by bit. |
XOR | #i=#j XOR #k; | |
AND | #i=#j AND #k; | |
Conversion from BCD to BIN | #i=BIN[#j]; | Used for signal exchange to and from the PMC |
Conversion from BIN to BCD | #i=BCD[#j]; |
Operation Descriptions
Definition – #i=#j
This is what’s used to transfer data from one variable to another. The left variable is where the result is.
So if #1=10 and #2=12
#1=#2
Both variables now equal 12.
Sum – #i=#j+#k
This is what’s used to add variables, or values on their own together.
So if #2=12 #1=#2+10
The value of #1 is now 22.
Difference – #i=#j-#k
This is what’s used to subtract variables, or values on their own together.
So if #2=12
#1=#2-10
The value of #1 is now 2.
Multiply – #i=#j*#k
This is what’s used to multiply variables, or values on their own together.
So if #2=12
#1=#2*10
The value of #1 is now 120.
Divide – #i=#j/#k
This is what’s used to divide variables, or values on their own together.
So if #2=20
#1=#2/10
The value of #1 is now 2.
All of the above can be put together using brackets to perform larger calculations. So if #1=2 and #2=5
#100=#1*[#2-3]
The value of #100 is now 4, because 2 x (5 – 3) = 4
Macro Programming functions
function | Format | Remarks |
---|---|---|
Sine | #i=SIN[#j]; | An angle is specified in degrees. 90 degrees and 30 minutes is represented as 90.5 degrees. |
Arcsine | #i=ASIN[#j]; | |
Cosine | #i=COS[#j]; | |
Arccosine | #i=ACOS[#j]; | |
Tangent | #i=TAN[#j]; | |
Arctangent | #i=ATAN[#j]/[#k]; | |
Square root | #i=SQRT[#j]; | |
Absolute value | #i=ABS[#j]; | |
Rounding off | #i=ROUND[#j]; | |
Rounding down | #i=FIX[#j]; | |
Rounding up | #i=FUP[#j]; | |
Natural logarithm | #i=LN[#j]; | |
Exponential function | #i=EXP[#j]; |
Macro Programming Statements
In addition to variables and expressions, the macro language uses a few macro statements that can control the flow of the program.
Here is a list of the macro statements:
- IF – GOTO
- IF – THEN
- GOTO
- WHILE – DO
- DO- END
You'll notice that a few of the statements are grouped together. This is because the statements work together to determine the exact function performed.
Macro Programming Examples
Macro programming is very flexible. The examples given here are not necessarily the right way or the only way to do something. This section is simply to help you understand how the macro language works.
Example 1 : Frame Hole Pattern

(MAIN PROGRAM) | |
O0023 | |
N1 G21 | Metric mode |
N2 G90 G00 G54 X0 Y0 S800 M03 | Any X and/or Y motion may be included |
N3 G43 Z25.0 H01 M08 | Tool length offset + clearance above |
N4 G99 G81 R2.5 Z-14.7 F150.0 L0 | No machining but cycle data memorized |
N5 G65 P8103 X10.0 Y9.5 U6 V5 I16.0 J14.0 | Macro call with assignments |
N6 G80 Z25.0 M09 | G90 omitted on purpose (see macro) |
N7 G28 Z25.0 M05 | Return to machine zero |
N8 M01 | End of current tool |
(FRAME HOLE PATTERN MACRO) | |
O8103 | |
#10 = #4003 | Store current setting of G90 or G91 |
IF[#4 LE 0] GOTO9101 | Alarm if I not defined (spacing between holes in X) |
IF[#5 LE 0] GOTO9101 | Alarm if J not defined (spacing between holes in Y) |
IF[#21 LT 2] GOTO9102 | Alarm if U less than 2 (minimum of 2 horizontal holes) |
IF[#21 NE FUP[#21]] GOTO9103 | Alarm if U uses a decimal point (number of horizontal holes) |
IF[#22 LT 2] GOTO9102 | Alarm if V less than 2 (minimum of 2 vertical holes) |
IF[#22 NE FUP[#22]] GOTO9103 | Alarm if V uses a decimal point (number of vertical holes) |
G90 X#24 Y#25 | Lower left corner hole = the first hole of the pattern |
#33 = #21-1 | Number of spaces horizontally (positive) |
WHILE [#33 GT 0] DO1 | Start the loop for positive horizontal holes |
G91 X#4 | Incremental + complete bottom row (to the right in X+) |
#33 = #33-1 | Update counter |
END1 | End of loop |
#33 = #22-1 | Number of space vertically (positive) |
WHILE [#33 GT 0] DO1 | Start the loop for positive vertical holes |
Y#5 | Complete right column (up in Y positive) |
#33 = #33-1 | Update counter |
END1 | End of loop |
#33 = #21-1 | Number of spaces horizontally (negative) |
WHILE [#33 GT 0] DO1 | Start the loop for negative horizontal holes |
X-#4 | Complete bottom row (to the left in X negative) |
#33 = #33-1 | Update counter |
END1 | End of loop |
#33 = #22-1 | Number of spaces vertically (negative) |
WHILE [#33 GT 1] DO1 | Loop for vert. neg. holes – see condition – no first hole! |
Y-#5 | Complete left column (down in Y negative) |
#33 = #33-1 | Update counter |
END1 | End of loop |
GOTO9999 | Bypass all alarms if data input in G65 macro call is correct |
N9101 #3000 = 101 (HOLE SPACING TOO SMALL) | Generates alarm number 101 or 3101 |
N9102 #3000 = 102 (TWO HOLES MINIMUM REQUIRED) | Generates alarm number 102 or 3102 |
N9103 #3000 = 103 (DECIMAL POINT NOT ALLOWED) | Generates alarm number 103 or 3103 |
N9999 G#10 | Original setting of G90 or G91 restored |
M99 | End of macro |
Discover more from digit chain
Subscribe to get the latest posts sent to your email.