Programming concepts
5.2 Elements of the user program
S7-200 SMART
System Manual, V2.3, 07/2017, A5E03822230-AF
105
5.2
Elements of the user program
A program organizational unit (POU) is composed of executable code and comments. The
executable code consists of a main program and any subroutines or interrupt routines. The
code is compiled and downloaded to the CPU. You can use the program organizational units
(main program, subroutines, and interrupt routines) to structure your user program.
●
The main body of the user program contains the instructions that control your application.
The CPU executes these instructions sequentially, once per scan cycle.
●
Subroutines are optional elements of your program which are executed only when called:
by the main program, by an interrupt routine, or by another subroutine. Subroutines are
useful in cases where you want to execute a function repeatedly. Rather than rewriting
the logic for each place in the main program where you want the function to occur, you
can write the logic once in a subroutine and call the subroutine as many times as needed
during the main program. Subroutines provide several benefits:
–
Using subroutines reduces the overall size of your program.
–
Using subroutines decreases your scan time because you have moved the code out of
the main program. The CPU evaluates the code in the main program every scan
cycle, whether the code is executed or not, but the CPU evaluates the code in the
subroutine only when you call the subroutine, and does not evaluate the code during
the scans in which the subroutine is not called.
–
Using subroutines creates code that is portable. You can isolate the code for a
function in a subroutine, and then copy that subroutine into other programs with little
or no rework.
Note
Using V memory addresses can limit the portability of your subroutine, because it is
possible for V memory address assignment from one program to conflict with an
assignment in another program. Subroutines that use the local variable table
(L memory) for all address assignments, by contrast, are highly portable because
there is no concern about address conflicts between the subroutine and another part
of the program when using local variables.
●
Interrupt routines are optional elements of your program that react to specific interrupt
events. You design an interrupt routine to handle a pre-defined interrupt event. Whenever
the specified event occurs, the CPU executes the interrupt routine.
The interrupt routines are not called by your main program. You associate an interrupt
routine with an interrupt event, and the CPU executes the instructions in the interrupt
routine only on each occurrence of the interrupt event.