1. Some foreign small PLC program structure
The PLC user program by the main program, subroutine and interrupt program. The CPU should call the main program every scan cycle. The main program can call the subroutine, the small control system can only the main program. Interrupt program for quick response to interrupt events. When an interrupt event occurs, the CPU will stop executing the program or task currently being processed and execute the user-written interrupt program. After executing the interrupt program, continue to execute the program or task suspended. Their subroutines and interrupt routines have no local variables, subroutines have no input or output parameters.
The company is located in:
2. Siemens S7-200 program structure
The process image inputs / outputs (I / Q), variable memory V, internal memory bits M, timer T, counter C etc. belong to global variables. Program Organizational Unit (POU) of S7-200 includes main program, sub-program and interrupt program. Each POU has its own 64-byte local variables, local variables can only be used in the POU where it is located. In contrast, global variables can be used in each POU.
The following is a subroutine can use local variables:
1) TEMP (Temporary Variable) is a variable temporarily saved in the local data area. Only in the implementation of the POU, the definition of temporary variables were used, POU execution is no longer save the temporary variable value.
2) IN is the input parameter provided by the POU that called it.
3) OUT is the output parameter returned to the calling POU (the execution result of the subroutine).
4) IN_OUT is an input_output parameter, the initial value of which is sent to the subprogram by the POU calling it, and the same variable returns the result of the subprogram to the POU that called it.
There are only temporary variables TEMP in the main program and interrupt program.
Subroutines with input, output parameters and local variables are easy to implement for structured programming and are especially useful for long-term manufacturers of similar equipment or production lines. The programmers at these manufacturers write a large number of common subroutines for the various components or process functions of the device. Even if you do not know the subroutine’s internal code, as long as you know the function of the subroutine and the meaning of the input and output parameters, you can quickly “assemble” the control program to meet different user requirements through the call between the programs. It’s like using digital integrated circuit chips to compose complex digital circuits.
If there is no subroutine input and output parameters, and it calls the program does not have a clear interface, it is difficult to achieve structured programming.
If there is no subprogram local variables, it can only exchange data with the program that called it through the global variable, and only the global variables can be used inside the subprogram. When porting subprograms and interrupt programs to other projects, you need to reorganize the global variables they use to ensure that address conflicts do not occur. When the program is complex, subroutine and interrupt procedures, this re-allocation of the workload of the address is very large.
If the subroutine and interrupt routines have local variables, and they only use local variables, do not use global variables, because there is no address conflict with other POUs, without any changes, you can port subroutines to other projects.
The company is located in:
3. Siemens S7-300 / 400 program structure
S7-300 / 400 divides the subroutine into functions (Function, or function) and function blocks (FunctionBlock).
The function of the S7-300 / 400 is basically the same as that of the S7-200. They have input, output parameters and temporary variables, the return value of the local data in the function actually belongs to the output parameters. They do not have a dedicated memory area, no longer save the data in the temporary variables after the function is executed.
You can use global variables to save data that needs to be saved after the function has finished, but this can affect portability.
Function blocks are user-written blocks that have their own dedicated memory area (ie, the background data block). The function block input and output parameters and static variables are stored in the specified instance data block. The temporary variables are stored in the local data stack. Each time you call a function block, you specify a context data block. After the function block is executed, the data in the instance DB will not be lost, but the data in the local data stack will not be saved.
Function block similar to C + + package concept, the program and data package together, has a very good portability.
The shared data blocks of the S7-300 / 400 are available to all logic blocks.
The company is located in:
4. IEC61131-3 program structure
IEC61131-3 is the PLC programming language standard. IEC61131-3 is the world’s first and only programming language standard to date in industrial control. IEC
The 61131-3 has three types of POUs: programs, function blocks and functions.
Function is a POU that has multiple input parameters and one output parameter (return value). The name of the returned value is the same as the name of the function, and the data type of the return value needs to be defined. Calling tools
Functions that have the same input value always return the same result. Function You can call other functions, but you can not call function blocks or programs. Functionally definable local variables are VAR and VAR_INPUT.
A function block is a POU that has multiple I / O parameters and internal memory locations. The output parameter values ​​of the function block depend on the value of its internal memory location. Function blocks can call other functions
Block or function, but can not call the program.
Before calling a function block, an instance of the function block must be declared for every call in the POU in which the function block is to be called, and the operating system will allocate a block-specific memory for each call
(Similar to the instance DB of S7-300 / 400).
Function Because there is no internal memory, the call does not require instantiation.
The behavior and purpose of a program is similar to that of a function block. The program has input and output parameters and can have an internal memory area. The program usually contains functions and function block calls.
IEC61131-3 defines a number of standard functions and function blocks.
The company is located in:
5. S7-300 / 400 and IEC61131-3 program structure difference
1) S7-300 / 400 function can have multiple output parameters, the return value also belongs to the output parameters. The function of IEC61131-3 has only one return value.
2) Function Blocks of IEC61131-3 The dedicated memory area for saving local variables is assigned when declaring a function block instance. It is not transparent to the user and can not be directly accessed by other POUs.
Local variables (excluding temporary variables) of the function blocks of the S7-300 / 400 are stored in its instance DB. Other POUs can access the variables in the instance DB. If you need to call the same function block multiple times to control the same type of controlled object, you need to specify a background data block for each call. However, there are very few variables in these background data blocks, so a large number of Background data block. You can use multiple instance DBs to reduce the number of instance DBs. However, we need to add a function block to manage multiple contexts.
3) The local variables of the function blocks of S7-300 / 400 have temporary and static variables. The internal variable Var of the function blocks of IEC61131-3 corresponds to the static variable of S7-300 / 400.
4) S7-300 / 400 data area is divided into data blocks to use, the size of the data block and data block variables defined in the data type and the number of variables. IEC61131-3 no data block concept.

Leave a Reply