top of page

Setting Up User Defined G and M codes

Parameters control countless things about the way your CNC machine tools behave. In each Parameter preference segment, we will expose parameters that have an important impact on how your machines run. But first, a disclaimer. Parameters vary from one Fanuc control model to another - as do the actual functions they control. Always check in your Fanuc Operators manual and/or Maintenance manual to confirm the parameter number and settings we show. Never blindly change a parameter! If there is any doubt about what the parameter does, contact your machine tool builder to learn more.


The application

As you know, there are many G and M codes included with your CNC machine tool. These words have a fixed usage that is determined by the control manufacturer (Fanuc in our case) and the machine tool builder. What you may not know is that you have a way of creating your own G and M codes - you can even change the function of those that your machine currently has.


Though this application is commonly thought of as a custom macro application, note that most machines don't require custom macro in order for you to create user defined G and M codes. By far, the best applications for user defined G and M codes involve custom macro techniques, but in this article, we'll limit our discussions to applications that do not require custom macro.


One example application involves safety words that many programmers like to include at the beginning of their programs. These words ensure that the machine is in the appropriate states (commonly the same states as when the machine is first turned on). Here is an example of a series of safety commands for a machining center program:

  • O0001 (Program number)

  • N005 G17 G20 G40 (XY plane, inch mode, cancel cutter comp.)

  • N010 G49 G64 G80 (Cancel tool length comp., normal cutting mode, cancel canned cycles)

  • N015 G90 G94 G98 (Select absolute mode, feed per minute, initial plane for canned cycles)

  • .

  • .

  • .

Remember that there is a limitation when it comes to how many G codes can be included in one command (with most Fanuc controls, it is three). This is why we've broken the safety words into three commands.


As stated, many programmers like to include these commands (or something like them) at the beginning of all programs. To shorten the amount of words needed in your program, you can define your own G or M code which would instate all of these words.


When picking the G or M code number you wish to define, you must be careful not to overwrite a current G or M code since Fanuc also allows you to redefine any G or M code as well as create new ones. To ensure that you don't pick one that's already in use, Fanuc allows you to define G and M codes over a value of one-hundred (G101 or M101, for instance).


So instead of invoking the safety words given in the three commands shown above, you can simply have the command

  • N005 G101 (Instate all safety words)

do so (after defining G101, of course).


You probably already have many programs that are run on your CNC machine/s. If you add G101 (or any new G code) now, it's likely that many programs will have to be changed. This is one time when re-defining a current G or M code can be helpful. If you can pick an appropriate G or M code, you may save a lot of editing.


In our case, we might want to change the function of G90 - the absolute mode selector. If we do, whenever the control executes a G90, it will invoke all of safety words and perform the normal function of G90. Since most programmers include a G90 at the beginning of all programs (and at every tool change), we can be sure that the machine is always in the desired states when programs are run.


How it works

There is really nothing magical going on. All we do to create a user defined G or M code is set up a cross-reference within the control. A series of parameters is used for this purpose. In the case of defining the G101, for example, a parameter is set to tell the control that whenever a G101 is executed, it must run a pre-determined program. Within this special program, we'll include all of the safety words.


The same is true for re-defining a current G or M code. And if the invoking word (G90 in our example) is included within the special program, the control will perform its normal function (selecting the absolute mode in our case). The control will not try to execute the special program a second time.


Finding the parameters

As always, we recommend starting in the Fanuc Operator's Manual in the section that is most closely related to the parameter/s in question. Since user defined G and M code parameters are associated with custom macro, I start by looking at the custom macro descriptions.


Toward the end of the custom macro descriptions in the 15 series manual, I find three headings of interest: Macro call using G code, Macro call using M code, and Subprogram call using M code. For M codes, I recommend using Macro call using subprogram, since it most closely emulates how true M codes work.


There are ten parameters related to user defined G codes, meaning up to ten user defined G codes can be defined. They range from parameter 7050 through 7059 (again, for a 15 series control). I notice that each is related to a pre-determined program. 7050 is related to program O9010. 7051 is related to O9011. And this continues through parameter 7059, which is related to program O9019.


User defined M code parameters are similar. They range (for this control) from parameters 7071 through 7079. Program O9001 is related to parameter 7071. Program O9002 is related to parameter 7072. And this continues through parameter 7079, which is related to program O9009.


Creating a user defined G code

Say I want to create a new user defined G code (G101). To do so, I must first set parameter 7050 (or any of the available user defined G code parameters) to a value of 101. I must also load/type program O9010 (or the corresponding program number) into the control:

  • O9010

  • G17 G20 G40

  • G49 G64 G80

  • G90 G94 G98

  • M99

Note that this program must end with an M99, like a subprogram.

From this point, whenever a G101 is read by the control, it will execute program O9010, and invoke the various safety words.


Changing the function of a current G code

The same thing goes for re-defining a current G code. Say I want G90 to be used to invoke all safety words, and, of course, to set the absolute mode. This time I'll set parameter 7050 to a value of 90. And here is the related program:

  • O9010

  • G17 G20 G40

  • G49 G64 G80

  • G90 G94 G98

  • G90

  • M99

Notice that the only difference is the addition of a G90 word. Remember, this will cause the control to perform the normal function of G90 - it will not try to execute program O9010 a second time.


An application for user defined M codes

Our simple application for user defined M codes is related to making two similar machines more compatible with regard to M codes. Consider, for example, two turning centers made by different manufacturers. One uses M41 and M42 for low and high spindle range. The other uses M23 and M25.


Any program that must be run in both machine will have to be edited on a regular basis. But we can make these two machines more compatible by adding two user defined M codes to one of the machines. We'll pick the machine that uses M25 and M25.


On this machine (assuming it has a 15 series control), we'll set parameter 7071 to a value of 41 and parameter 7072 to a value of 42. From this point on, whenever the control sees an M41, it will execute program O9001. When it sees an M42, it will execute program O9002. Here are the two programs:

  • O9001

  • M23

  • M99

  • O9002

  • M25

  • M99

Notice that all we've done is included the appropriate M code for low or high spindle range in each program. Again, when the control reads an M41, it will execute program O9001 and perform an M23. When it reads an M42, it will execute program O9002 and perform an M25.


There are countless applications for user defined G and M codes. We're showing some pretty simple ones. To learn more, you'll need to learn more about custom macro - which is Fanuc's version of parametric programming. Note that we offer several training products that can help your learn custom macro.

6,007 views0 comments

Recent Posts

See All

Tapping on a turning center (without canned cycles)

Unless your Fanuc controlled turning center came with live tooling, it’s likely that you don’t have canned cycles (G80-G89) like those you find on machining centers. You might have G74 and G75, the mu

Can you speed up your tool change time?

Machining centers, of course, have automatic tool changing devices to automate the tool changing process. Current models boast very fast tool changing times and you may be quite satisfied with tool ch

bottom of page