3 subroutines, Programming – Lenze PM94P01C User Manual

Page 46

Advertising
background image

PM94P01C

44

Programming

The flowchart and code segment in Figure 15 illustrate the syntax for the WHILE instruction.

WHILE <condition>

…statements

ENDWHILE

…statements

WHILE IN_A3

MOVED 3

WAIT TIME

2000

ENDWHILE

…statements

Start

Is input A3 ON?

End

YES

NO

Move DIstance 3

units. Delay 2

seconds

Figure 15: WHILE Code and Flowchart

2.9.3 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 handlers.

When a GOSUB statement is executed, 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. Subroutines 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
;Subs usually located after END
;statement of main program
;
CalcMotionParam:
V1 = (V3*2)/V4
RETURN

Start

End

Label

GOSUB

RETURN

Main Program

Figure 16: GOSUB Code and Flowchart

Advertising