Lesson 1: What a CNC Program Is and How It Runs
Updated
A CNC program is a sequence of blocks (lines), each made of words: an address letter followed by a number, such as G01, X50.0 or F1200. Modal codes stay active until replaced by another code of the same group, so a program starts with the safe block G21 G17 G40 G49 G80 G90 and ends with G91 G28 Z0, G90, M05, M30.
In this lesson you will learn:
- what a CNC program consists of: program number, blocks and words;
- the difference between modal and non-modal codes;
- what each code in the safe start block means;
- how the control reads and executes a program;
- how to write the shortest complete program that only moves the tool to a safe position.
Throughout the course we use a Fanuc-style milling control (e.g. 0i-MF) and metric units (G21). Other controls differ in places, so your machine manual is always authoritative. The training part is an aluminium EN AW-6082 plate 100 × 60 × 20 mm, and tool T1 is a Ø10 mm 3-flute carbide end mill.
What a CNC program is
A CNC program is a text file that lists, line by line, what the machine must do: where to move, at what feed, with which tool, and when to switch the spindle and coolant on. The language is called G-code (see G-code basics). Programs can be written by hand or generated by a CAM system, but in both cases the operator must be able to read and check them.
Program parts: O, N, blocks and words
A Fanuc program starts with a number such as O1001. When the file is transferred to the machine, it is often enclosed in % characters. Each line is called a block, and a block is made of words – an address letter and a number.
| Word | Example | Meaning |
|---|---|---|
| O | O1001 | Program number |
| N | N10 | Block number – optional, helps searching |
| G | G01 | Preparatory function: motion type or mode |
| X, Y, Z | X50.0 | Target coordinate, mm |
| F | F1200 | Feed rate, mm/min |
| S | S6000 | Spindle speed, rpm |
| T | T1 | Tool number |
| H, D | H1 | Tool length / radius offset number |
| M | M03 | Miscellaneous function (see M-codes) |
| ( ) | (END MILL D10) | Comment – ignored by the control |
Two important rules. First, write coordinates with a decimal point: X50.0 or X50.. On many Fanuc controls, unless the relevant parameter is set, X50 without a point is read as 50 least input increments, i.e. 0.050 mm. Second, put comments in parentheses, in capitals and in plain ASCII – many controls reject accented or national characters.
Modal and non-modal codes
A modal code stays active in following blocks until replaced by another code of the same group. For example, after G01 every block with coordinates is a linear move until G00, G02 or G03 is programmed. F and S values are modal too. A non-modal code (Fanuc group 00) acts only in its own block.
| Fanuc group | Codes | Purpose | Type |
|---|---|---|---|
| 00 | G04, G28, G53 | Dwell, reference return, machine coordinates | Non-modal |
| 01 | G00, G01, G02, G03 | Motion type | Modal |
| 02 | G17, G18, G19 | Plane selection | Modal |
| 03 | G90, G91 | Absolute / incremental | Modal |
| 06 | G20, G21 | Inch / metric | Modal |
| 07 | G40, G41, G42 | Cutter radius compensation | Modal |
| 08 | G43, G49 | Tool length compensation | Modal |
| 09 | G73–G89, G80 | Canned cycles | Modal |
| 14 | G54–G59 | Work coordinate system | Modal |
The safe start block
Before your program, another program may have run, or the operator may have left G91 or G41 active in MDI mode. Every program therefore begins with a block that sets a known state:
G21– millimetre input (some control manuals recommend putting G21 in a block of its own);G17– XY working plane;G40– cutter radius compensation off;G49– tool length compensation off;G80– canned cycles cancelled (see drilling canned cycles);G90– absolute coordinates.
How the control executes a program
- The control reads blocks in order, top to bottom (only special commands such as subprogram calls cause jumps).
- Within a block the order of words usually does not matter, but the convention is N, G, X, Y, Z, F, S, T, M.
- The control sets the block's modal codes, performs the motion and auxiliary functions, and starts the next block only when the previous one is complete. Several blocks are read in advance (look-ahead) to keep motion smooth.
- In Single Block mode the control stops after every block – the safest way to prove out a new program.
First program: safe position
This program cuts nothing: it loads T1, positions it 50 mm above the plate centre and returns the Z axis to the reference point.
%
O1001 (LESSON 1 - SAFE POSITION)
N10 G21 G17 G40 G49 G80 G90
N20 T1 M06 (END MILL D10 3FL)
N30 G54 G00 X50.0 Y30.0
N40 G43 H1 Z50.0
N50 G91 G28 Z0
N60 G90
N70 M05
N80 M30
%
N30 selects the G54 work offset and rapids the tool above X50 Y30. N40 activates length offset H1 and brings the tool tip down to Z50.0 above the part surface. N50 G91 G28 Z0 returns Z to the machine reference point: G28 first moves through an intermediate point, and in G91 mode Z0 means "0 mm from the current position", so the tool goes straight up. N60 must restore G90 because G91 is modal. M05 stops the spindle, M30 ends the program and rewinds it.
Exercise
A program contains these blocks:
N60 G90 G01 X20.0 Y15.0 F1200
N70 G04 P500
N80 G91 G28 Z0
N90 X40.0
For each block, state which words are modal and which are non-modal, and whether block N90 runs in G90 or G91 mode. The answer is in the FAQ.
Next lesson: Lesson 2: Coordinate systems and work offsets.
Frequently asked questions
Do I have to write N block numbers?
No. The control does not need them, but they make searching and restarting from a given block easier. CAM systems often number in steps of 10.
Why should comments contain only plain ASCII characters?
Many CNC controls accept only ASCII, so accented or national characters may trigger an alarm or be corrupted.
Why write X50.0 instead of X50?
On many Fanuc controls a number without a decimal point is read in least input increments, so X50 can mean 0.050 mm. The decimal point removes this risk.
What is the difference between M30 and M02?
Both end the program. M30 also rewinds to the program start; M02 behaviour depends on control parameters.
Exercise answer
N60: G90, G01 and F1200 are modal, X and Y are target coordinates. N70: G04 is non-modal, P500 is its dwell time. N80: G28 is non-modal but G91 is modal. N90 therefore runs in G91 and moves 40 mm in X rather than to X40 – G90 should have been restored after G28.
Looking for someone to do this job? Find a manufacturer on the marketplace or post a request.
Services offered I need a service