Programming, 5 goto statement and labels, 6 subroutines – Lenze E94P PositionServo with MVOB User Manual

Page 48

Advertising
background image

46

L

PM94H201B_13xxxxxx_EN

Programming

2.9.5 GOTO Statement and Labels

The GOTO statement can be used to transfer program execution to a section of the Main Program identified by a label.
This statement is often executed conditionally based on the logical result of an If Statement. The destination label may
be above or below the GOTO statement in the application program.

Labels must be an alphanumeric string of up to 64 characters in length, ending with a colon “:” and containing no spaces.

GOTO TestInputs

…statements

TestInputs:

…statements

IF (IN_A1) GOTO TestInputs

Table 13 provides a short description of the instructions used for program branching.

Table 13: Program Branching Instructions

Name

Description

GOTO

Transfer code execution to a new line marked by a label

DO/UNTIL

Do once and keep doing until conditions becomes true

IF and IF/ELSE

Execute once if condition is true

RETURN

Return from subroutine

WAIT

Wait fixed time or until condition is met

WHILE

Execute while a condition is true

GOSUB

Go to Subroutine

2.9.6 Subroutines

A subroutine is a group of SML statements that is located at the end of the main body of the program. It starts with a
label which is used by the GOSUB statement to call the subroutine and ends with a RETURN statement. The subroutine
is executed by using the GOSUB statement in the main body of the program. Subroutines can not be called from an
EVENT or from the FAULT handler.
When a GOSUB statement is executed, program execution is transferred to the first line of the subroutine. The subroutine
is then executed until a RETURN statement is met. When the RETURN statement is executed, the program’s execution
returns to the program line (in the main program) following the GOSUB statement. A subroutine may have more than
one RETURN statement in its body.
Subroutines may be nested up to 32 times. Only the main body of the program and subroutines may contain a GOSUB
statement. Refer to Section 3.1 for more detailed information on the GOSUB and RETURN statements. The flowchart
and code segment in Figure 16 illustrate the use of subroutines.

…statements

GOSUB CalcMotionParam

MOVED V1

OUT2=1

…statements

END

;

CalcMotionParam:

V1 = (V3*2)/V4

RETURN

Start

Subroutine

CalcMotionParam

GOSUB

CalcMotionParam

RETURN

Statement

... Statements

MOVED V1

OUT2=1

... Statements

End

V1 = (V3*2)/V4

Jump to

subroutine

Return from

subroutine

Figure 16: GOSUB Code and Flowchart

Advertising