Haas CNC Macro Programming Examples

Haas CNC Macro Programming Examples
— · 0 votes

A Haas CNC macro example is a short G-code program that uses variables and logic to do a job the plain code cannot do on its own. The control stores numbers in macro variables, then a G65 call or WHILE loop changes those numbers and runs the same motion again with new values. Typical examples are bolt-hole circles, stepped pockets and family-of-parts programs driven by a few input values.

  • Macro variables hold the numbers, and the program math changes them between passes.
  • A G65 call passes arguments into a subprogram so one routine serves many parts.
  • A WHILE loop repeats a block until a counter reaches its limit.
  • An IF branch skips or selects a block, which keeps one program handling several part sizes.
  • Test every macro with dry runs and single block before it touches a workpiece.

What a Haas macro actually changes

A Haas macro program is ordinary G-code wrapped around numbers the control can calculate. Standard code fixes a coordinate, while a macro writes that coordinate as a variable and lets the program compute it. The result is one program that adapts to the values you enter.

The Haas control keeps a set of macro variables that hold whole numbers and decimals. You assign a value with a hash sign and a number, such as #100 = 2.0, and use that variable anywhere a coordinate or feed value fits. The control evaluates the expression at run time, so the motion follows the value in the variable.

Haas G-code macros also carry local variables, which exist inside a called subprogram and pass arguments in and out. Local variables keep a routine self-contained, so the same subprogram works for different parts without editing its body. Understanding which variable type you are writing to prevents values from leaking between passes.

Setting variables and doing math

Haas macro variables become useful when the program computes with them instead of only storing them. The control accepts basic operators for add, subtract, multiply and divide inside an assignment. Functions for square root, sine, cosine and arctangent let a program derive a coordinate from an angle or a radius.

A bolt-hole circle shows the idea. The program stores the circle center, the radius and the hole count, then computes each hole position from an angle. Each pass adds one step to the angle, converts it with sine and cosine, and moves to the new point. Trigonometry on the control removes the need to write every coordinate by hand.

Parentheses group an expression so the order of operations is explicit. Writing the math in small steps across several lines makes a program easier to read and to correct. A comment beside each assignment records what the number means, which helps the next operator.

Calling a routine with G65

A G65 call runs a subprogram and passes arguments into it, which is how one routine serves a family of features. The call names the subprogram and lists values that land in its local variables in order. The subprogram then works from those values and returns to the main program.

Haas control programming examples often use a drilling routine this way. The main program supplies the hole position, depth and feed for each feature, and the subprogram holds the machining logic once. Changing a value at the call site changes the feature without touching the routine.

Arguments passed by G65 are read inside the subprogram through its local variable numbers. Keep the order of the arguments and the variable numbers matched, and note the mapping in a comment. A mismatch here moves the tool to the wrong place, so check the call before the first run.

Repeating work with a WHILE loop

A WHILE loop repeats a block of code until a test turns false, which suits any job with a counted pattern. The loop checks its condition at the top, so the block runs only while the test stays true. A counter variable, incremented by a fixed step, ends the loop when it passes the limit.

Stepped pocket depths fit this structure well. The program sets a start depth and a depth per pass, then loops until the current depth reaches the final depth. Each pass machines one level and adds the step to the counter, so the number of passes follows from the values you enter.

A loop must always reach its exit condition, or the program runs until the operator stops it. Increment the counter inside the loop and keep the step value above zero. Watch the first loop cycle on a dry run to confirm the counter and the exit test agree.

Choosing between a loop and an IF branch

A WHILE loop repeats a block, while an IF branch chooses whether to run a block at all, and the two solve different problems. Use the loop for a counted pattern and the branch for a decision. Many macros combine both, branching on an input value and then looping through the work.

An IF branch tests a condition and skips to a labeled line when the test is false. That structure lets one program handle several part sizes or skip an optional feature. The branch keeps the code for each case in one program instead of separate files.

Readability decides the choice in most shop programs. A loop that repeats a simple motion stays short and clear, while a branch with many cases grows quickly. Where a decision needs dozens of lines, a separate subprogram called by G65 is easier to follow.

A worked example: bolt-hole circle

A bolt-hole circle macro shows the parts working together in one short program. Variables hold the center, the radius, the hole count, the start angle and the depth. The program computes each hole position, drills it, then advances the angle by one step.

The first block sets the values and calculates the angular step as a full turn divided by the hole count. A WHILE loop then runs until the counter equals the hole count. Inside the loop, sine and cosine give the X and Y offsets from the center, and a drilling cycle cuts the hole.

On a Haas control, confirm the work offset and the drilling cycle before running the macro. Dry-run the program above the part and watch the positions of the first few holes. If a hole lands wrong, check the angle units the control expects and the sign of the offsets.

A worked example: stepped pocket depth

A stepped pocket macro walks a tool down in equal depths until it reaches the final floor. Variables hold the top surface, the final depth, the depth per pass and the feed. The loop cuts one level per cycle and stops when the current depth passes the target.

Order matters in this routine. The program cuts at the current depth, then adds the step to the counter, then tests the counter against the final depth. Cutting before the step keeps the final pass from overshooting the floor.

A negative depth value is easy to get wrong, since the pocket floor sits below the top surface. Write the test so the loop ends when the counter reaches or passes the target, whichever direction the depth runs. Confirm the arithmetic on a dry run before the tool enters the material.

Testing macros without scrapping parts

A macro needs a dry run before it cuts metal, because a single wrong variable moves the tool far from the intended path. Run the program with the tool clear of the part and watch the position display. Single block each pass and compare the numbers on screen with the values you expect.

An offset mistake in a macro can drive a tool into a fixture or the table. Keep the feed override within reach and the rapid override low during the first run. Where the program drives a large motion, verify the coordinates by air-cutting the path first.

Record the input values that worked, so the next setup starts from a known state. A comment block at the top of the program listing each variable and its meaning saves time on the next job. Where a macro controls a risky motion, have a trained programmer review the logic before production.

Questions about Haas macro programming

What are Haas macro variables used for?

Haas macro variables store the numbers a program calculates and reuses, such as coordinates, depths and counts. A program writes a value into a variable and then reads it wherever a number fits. Changing the variable changes every place it is used.

Do I need a special option to run macros on a Haas control?

Macro programming is a function of the Haas control, and whether your machine has it enabled depends on the control model and the options set on that machine. Check the machine documentation or the control settings for your specific machine. The programming office can confirm what your control supports before you write the program.

How does a G65 call pass values to a subprogram?

A G65 call lists values after the subprogram number, and those values land in the subprogram's local variables in order. The subprogram reads the local variables and runs its logic from them. Keep the argument order and the variable numbers matched to avoid wrong motion.

Why does my WHILE loop never end?

A WHILE loop that never ends has a counter that does not reach its exit condition or a step value of zero. Check that the counter changes inside the loop and that the step moves toward the limit. Stop the program and dry-run the logic before cutting again.

Can one macro program machine several part sizes?

Yes, one macro program can machine several part sizes when the dimensions live in variables and the logic branches on those values. The operator enters the sizes at the top of the program or passes them through a G65 call. The same logic then drives each size without editing the motion blocks.

What should I check before running a new macro on a machine?

Check the work offset, the tool length offset and the variable values before a new macro runs. Dry-run the path above the part and single block through the first loop cycles. Have a trained programmer review logic that controls large or fast motions.

Read next

Latest articles