Subroutine techniques
As in other fields, it is also valuable in NC programming to organize frequently used command sequences as subroutines. This makes it possible to employ pre-prepared and tested functions in various workpiece programs.
Subroutines are identified within a program by a number. This number must be unique: there must be only one subroutine with a particular number (1..>2.000.000.000).
As interpretation proceeds, the calling program is suspended. The text of the subroutine is worked through, as often as necessary. Processing then returns to the calling program after the call location.
It is of course possible for one subroutine to call another subroutine. This call is executed in a similar way. This causes a stack of return information to be created. For technical reasons this nesting of subroutines is presently limited to 20 levels.
Definition of a Subroutine
The code for a subroutine can be written to the same file as the calling program. In this case the subroutine is linked directly: it is automatically also loaded as the file is read. If it is to be made generally available then it must be written in its own file that must be located in the CNC directory.
The name of the file begins with the letter 'L', and continues with a string of digits. This digit string must repeat the subroutine number, without any leading '0's.
The code should contain a label to indicate the starting point of the subroutine. Like the file name, it consists of the letter 'L' and the digit sequence described above.
The interpreter starts immediately after this label.
Subroutine syntax:
(Datei L2000.NC)
L2000
N100...
N110...
...
N5000 M17 (return command)
Calling a Subroutine
The following syntax must be used to call a subroutine from some block within the NC program. It is important that the expression "L2000" does not stand at the start of the line, in order to avoid confusion with a subroutine label.
(syntax of the subroutine call)
N100 L2000
In the following sample the expression "P5" causes the subroutine to be repeated 5 times.
(n-fold subroutine call (here: 5- fold))
N100 L2000 P5
Dynamic subroutine call
In some cases the subroutine to be called is not known until runtime. In this case the subroutine can be called with an R-parameter, thereby avoiding the need for a CASE instruction. The value for R must be allocated or calculated in a dedicated line.
(Dynamic call of a subroutine)
N099 R47=R45+1
N100 L=R47
Parameter passing
Parameters are passed to subroutines with the aid of R-parameters. Note that R-parameters are not saved automatically (see Rescuing R-parameters).
Use of Parameters
R-parameters can, in general, be freely used within subroutines. This has a number of consequences that can lead to errors if they are not borne in mind. On the other hand their careful use offers the NC-programmer a range of useful working techniques.
Results of Subroutines
If an R-parameter is changed without its contents being saved and restored, the change is effective after a return from the subroutine. If this was not intended, the result can be machine behavior that was not planned.
This feature can however be deliberately used in order to make the continuation of the processing dependent on the results of a subroutine. No restriction need be considered here other than those on the R-parameters.
Sample:
N100 L2000
N110 R2=R3+R4
...
N999 M30
L2000
N10 R3=17.5
N20 R4=1
N99 M17
Values are specified here in a subroutine. The values are then used in the calling program.
Ending a Subroutine
A subroutine is ended with M17.