How to Organize a Program (GS 2.3)

Basic Elements of a Control Program

Your control program for an S7-200 CPU consists of the following types of Program Organizational Unit (POU):

Main program The main body of the program (known as OB1) is where you place the instructions that control your application. The instructions in the main program are executed sequentially, once per scan cycle of the CPU.

Subroutines A subroutine is an optional set of instructions located in a separate block that is executed only when called from the main program or an interrupt routine.

Interrupt routines An interrupt routine is an optional set of instructions located in a separate block that is executed only when an interrupt event occurs.

STEP 7-Micro/WIN 32 organizes your program for you by providing separate tabs in the Program Editor window for each POU. The main program, OB1, is always the first tab, followed by any subroutines or interrupts that you may have created.

How POUs Are Terminated

Because your program is compartmentalized (each POU occupies a separate tab), there is no question about where OB1 or your various subroutines and interrupt routines end. The compiler terminates each POU for you with an unconditional END, MEND, RET, or RETI as appropriate. You should not supply this code in your program; if you put an unconditional END, MEND, RET, or RETI in your program, the compiler returns an error.

Subroutines

Subroutines are useful in cases where you want to execute a function repeatedly; rather than rewriting the logic 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. This provides several benefits:

ˇYour overall code size is reduced.

ˇYour scan time is also decreased because you have moved the code out of the main program (where it is automatically evaluated every scan cycle, whether it is executed or not). Your subroutine can be called conditionally and is not evaluated during the scans in which it is not called.

ˇSubroutines are easily portable; they allow you to isolate a function and copy it to other programs with little or no rework.
Note: Use of V memory limits 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 for all address assignments, by contrast, are highly portable because there is no concern about addressing conflicts.

Interrupt Routines

You may write interrupt routines to handle certain pre-defined interrupt events: the interrupt routines are not called by your main program; they are called by the PLC operating system when interrupt events occur. Because it is not possible to predict when the system will call an interrupt, it is undesirable to allow the interrupt routine to write to memory that might be used elsewhere in the program. By using the Local Variable Table, you can ensure that your interrupt routines use only temporary memory and do not overwrite data from elsewhere in your program.

Hardware Support for the Local Variable Table

The Local Variable Table feature of STEP 7-Micro/WIN 32 requires hardware support. You must have a CPU 221, CPU 222, CPU 224, CPU 226, or CPU 226XM to make use of the Local Variable Table.

See Also:

Subroutine

Interrupt Routine

Local Variable Table