Macro Programming

MACRO

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 VariablesCommon 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.

operationFormat 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

functionFormat 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

Frame Hole Pattern
Frame Hole Pattern
(MAIN PROGRAM) 
O0023 
N1 G21Metric mode
N2 G90 G00 G54 X0 Y0 S800 M03Any X and/or Y motion may be included
N3 G43 Z25.0 H01 M08Tool length offset + clearance above
N4 G99 G81 R2.5 Z-14.7 F150.0 L0No machining but cycle data memorized
N5 G65 P8103 X10.0 Y9.5 U6 V5 I16.0 J14.0Macro call with assignments
N6 G80 Z25.0 M09G90 omitted on purpose (see macro)
N7 G28 Z25.0 M05Return to machine zero
N8 M01End of current tool
  
(FRAME HOLE PATTERN MACRO) 
O8103 
#10 = #4003Store current setting of G90 or G91
IF[#4 LE 0] GOTO9101Alarm if I not defined (spacing between holes in X)
IF[#5 LE 0] GOTO9101Alarm if J not defined (spacing between holes in Y)
IF[#21 LT 2] GOTO9102Alarm if U less than 2 (minimum of 2 horizontal holes)
IF[#21 NE FUP[#21]] GOTO9103Alarm if U uses a decimal point (number of horizontal holes)
IF[#22 LT 2] GOTO9102Alarm if V less than 2 (minimum of 2 vertical holes)
IF[#22 NE FUP[#22]] GOTO9103Alarm if V uses a decimal point (number of vertical holes)
G90 X#24 Y#25Lower left corner hole = the first hole of the pattern
#33 = #21-1Number of spaces horizontally (positive)
WHILE [#33 GT 0] DO1Start the loop for positive horizontal holes
G91 X#4Incremental + complete bottom row (to the right in X+)
#33 = #33-1Update counter
END1End of loop
#33 = #22-1Number of space vertically (positive)
WHILE [#33 GT 0] DO1Start the loop for positive vertical holes
Y#5Complete right column (up in Y positive)
#33 = #33-1Update counter
END1End of loop
#33 = #21-1Number of spaces horizontally (negative)
WHILE [#33 GT 0] DO1Start the loop for negative horizontal holes
X-#4Complete bottom row (to the left in X negative)
#33 = #33-1Update counter
END1End of loop
#33 = #22-1Number of spaces vertically (negative)
WHILE [#33 GT 1] DO1Loop for vert. neg. holes – see condition – no first hole!
Y-#5Complete left column (down in Y negative)
#33 = #33-1Update counter
END1End of loop
GOTO9999Bypass 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#10Original setting of G90 or G91 restored
M99End of macro


Discover more from digit chain

Subscribe to get the latest posts sent to your email.

Leave a Reply