14 OOP
Introduction
What is OOP? OOP is short for Object Oriented Programming and implies an object is the focus of the program. This chapter discusses the use of objects in PLC programming and their use for making programs that are more readable. Software engineering in general has looked for programming tools that allow more robust and reliable computer programs.
Earlier programming discussions of “top-down” computer programming and “structured programming” have evolved into the use of objects that encapsulate an idea and stand alone in the program as that evolved concept. The overall idea of any programming endeavour has been to break the problem down into component parts and solve each part as it pertains to the whole and then combine the parts into a unified overall program. While the idea of a data structure is important, the focus of a top-down program or structured program would focus on the structure or flow rather than the task.
OOP
The idea of pluggable entities or “lego” modules implies that modules can be created that can be plugged into one another and perform as a whole. This entails the idea of information that may be needed only inside the “lego” program and thus hidden from the outside world, in short, the idea of encapsulation. This leads to the idea of objects and object oriented programming or OOP. In OOP, the data is protected since it can be manipulated only inside the shell or protected program called the object. The logic is protected since it is only executed inside the object and the details of the program are known inside the object as to how the program and data interact.
OOP protects the data. It is easier to write in a general sense since the module can block out other programming considerations and the data can be stored inside the OOP as opposed to elsewhere. Data can, obviously, be stored anywhere the programmer wants but the protection of the OOP gives a security that wasn’t available in earlier programming methods. And the programmer and the end user can focus on the object. This gives a great advantage to maintaining the program since the focus is placed where it was originally intended, on the object itself. And if some part changes, the programmer is reminded of the entity that is being affected and that changes should be studied as a whole for the object and not for just a part of the object. In the case of maintainability of control programs for the factory floor, the diagnostic part should be modified when the control portion is modified. If encapsulated, the programmer is reminded of both together since both should be encapsulated inside the object.
The programming language C refers to an object as a class. Other languages have similar names. In Siemens S7-Basic, the object is referred to as a Function (FC) or Function Block (FB). Allen-Bradley’s RSLogix 5000 also has introduced a function with version 18 called the AOI or add-on instruction. While it will not be featured in this chapter, its use is similar to the function or function block described here.
To program an FC or FB, first identify the object or objects involved. Identify the messages or signals the object needs to respond to and the outputs that result from these messages or signals. The FC or FB, while considered a class, may also be considered a template for the program.
And the idea of FB’s or FC’s calling other FB’s or FC’s is a powerful concept and creates the idea of sub classes or sub-sub classes. The FC or FB can also be re-used again and again in the same or other programs for the same or different clients. This gives the idea of a class or OOP a huge advantage over conventional programming as we build programs over the years or from job to job.
Arguments for the benefits of reusability include:
Reliability
Efficiency of programming effort Saving time in program development Decreased maintenance effort Resulting cost savings
Consistency of programs Some additional terms of OOP include:
Encapsulation – combining of programs and data to manipulate outcome in an object Inheritance – building a hierarch of objects, each with inheritance of the parent object Polymorphism – allowing one set of actions to share an object with another set of actions
FCs can be locked by the creator. This helps to preserve and protect the code and can actually help to simplify the overall program by firmly defining a functionality that is unchanged from one instance of code to another. STEP7 enables the user to create a storage location for custom functions called a Library. Several frequently used standardized and system functions are provided to the user in several libraries included with STEP7. The user can create a custom library and add items as needed, supporting programming standardization across projects.
The S7 architecture also supports the structuring of user-defined data storage locations, called data blocks, and reusable data templates called PLC Data Types.
The S7-1200 and 1500 controllers use programming elements that comply with IEC 61131-3 standard. At the core of the programming structure are code and data containers, known collectively as “Blocks”. The programmable logic controller provides various types of blocks in which the user program and the related data can be stored. Depending on the requirements of the process, the program can be structured in different blocks.
Organization blocks: (OB’s) form the interface between the operating system and the user program. The entire program can be stored in OB1 that is cyclically called by the operating system (linear program) or the program can be divided and stored in several blocks (structured program).
Function: (FC’s) contains a partial functionality of the program. It is possible to program functions so that they can be assigned parameters. As a result, functions are also suited for programming recurring, complex partial functionalities such as calculations. System functions (SFC) are parameter-assignable functions integrated in the CPU‘s operating system. Both their number and their functionality are fixed. More information can be found in the Online Help.
Function Block: (FB’s) offer the same possibilities as functions. In addition, function blocks have their own memory area in the form of instance data blocks. As a result, function blocks are suited for programming frequently recurring, complex functionalities such as closed-loop control tasks. System function blocks (SFB) are parameter-assignable functions integrated in the CPU‘s operating system. Both their number and their functionality are fixed.
Data Blocks: (DB’s) are data areas of the user program in which user data is managed in a structured manner.
Permissible Operations: You can use the entire operation set in all blocks (FB, FC and OB). We will now start to explore these basic “program structuring elements” of S7 beginning with the FC, or Function. A Function is defined by the IEC 61131 standard as a code container that does not retain internal values from one scan to the next. Functions in the S7 PLC behave in this fashion, and act as a container for user developed program code. A function may have a set of local variables defined for use within the function. Typically, when “called” in the main program, a function will have new “values” (or actual parameters) loaded into the local variable (called formal parameters) for use during execution of the function. Once the “results” are calculated and function execution finishes, the resulting “output value(s)” get returned to the main program.
Before you can create the program for the parameter-assignable FC, you have to define the formal parameters in the declaration table. It is up to the programmer to select the declaration type for each formal parameter.
- The ‘IN‘ declaration type should be assigned only to declaration types that will be read for instructions in the subroutine.
- Use the ‘OUT‘ declaration type for parameters that will be written to within the function.
- Use the “IN_OUT” for formal parameters that need to have a reading access and a writing access, such as a bit passed into the block that is used in the block for an edge operation.
- TEMP variables are intended to be used for holding interim calculation values or other values that are only required when the block is executing. TEMP variables exist in the local data stack while the block is executing and are overwritten when the block exits. The TEMP variables are – even though they are listed under “Interface” – not components of the block interface, since they do not become visible when the block is called and that no actual parameters have to be passed for the declared TEMP variables in the calling block.
The interface of a block forms the IN, OUT, and IN_OUT parameters. The RETURN parameter is a defined, additional OUT parameter that has a specific name according to IEC 61131-3. This parameter only exists in FCs in the interface. The declared formal parameters of a block are its interface to the “outside” meaning they are “visible” or relevant to other blocks that call this block. If the interface of a block is changed by deleting or adding formal parameters later on, then the calls have to be updated.
When an FC is added to a project, the FC is accessible via the Project Browser. When the FC is to be executed must be determined. This is defined by which OB in which the FC is to be called. For example, if the FC is to be executed every scan, it is placed in OB1. To call an FC in OB1, drag and drop the FC from the project browser onto a network.
Blocks Types
This is primarily a Siemens concept although Allen-Bradley has also introduced the idea with their function blocks in later versions of RSLogix 5000.
Global DBs
A data block (DB) is a data area in the user program containing user data. Global data blocks store data that can be used by all other blocks. The structure of the global data blocks is user defined.
Several Types of Blocks in STEP 7 Basic
Interaction between the operating system and the various block types is pictured below:
IMAGE
We will begin with a Function and start the project at the end of the chapter. Remember the directions from Ch. 8 – no instructions other than contacts and coils could be used to add two 16- bit numbers. The following show an 8-bit version of the same problem:
IMAGE
For the application, we need a FC rather than an FB since there is nothing to remember from function to function. Something comes in. Something goes out. Nothing is set in the function we need to remember. Choose Add new block, then Function and we will choose LAD (Ladder) because of the need for only contacts and coils:
IMAGE
Notice at left, we are now in the Function “Block_1” and we have a new network to start.
IMAGE
These variables are the ones inside the Function and are, in our case, Bool. We give pseudo- names to these variables and begin to program the program inside the Function:
Input – InBitWd1
InBitWd2
Output – SumBit
In/Out – Carry
As can be seen later, the Input, Output and InOut variables are the ones that are visible inside the Function when used in OB1.
IMAGE
Using these pseudo-variables, we enter the program inside the Function:
IMAGE
After building the logic, right click on Block_1 in the project tree and compile the function:
IMAGE
Now, we are ready to incorporate the FC in the main program OB. We first add the words we want to add together:
IMAGE
After the words to be added are defined, we start programming in OB1. First, we add the logic for bit 0. This is a half-adder and we need only build this logic once. Then we drag the block Block_1 from the left and add it to the logic. We finish the process by adding the word.bit addresses for bit 1 to this function. We now are ready to add the bits 2-15 with function block for each and we are complete. This is left as an exercise.
IMAGE
A review of the Variables inside the FC and FBs defined by Siemens:
Before you can create the program for the parameter-assignable block, you have to define the block parameters in the Interface table. The block interface allows local tags to be created and managed.
The tags are subdivided into two groups shown by the table below:
IMAGE
A review of when Temporary Tags are active:
IMAGE
Creating an FB Block
“FB – Function block Code blocks that store their values permanently in instance data blocks, so that they remain available even after the block has been executed.
All In-, Out-, InOut- parameters are stored in the instance DB – the instance DB is the memory of the FB.
Definition Function blocks are code blocks that store their values permanently in instance data blocks, so that they remain available even after the block has been executed. They save their input, output and in/out parameters permanently in the instance data blocks. Consequently, the parameters are still available after the block execution. Therefore they are also referred to as blocks “with memory”.
Block Interface The block interface for an FB looks similar to that of an FC. There are two groups of Block interface tags:
- Block parameters that form the block interface when it is called in the program.
- Input, Output, and In/Out parameters are a part of this group
- Local data that are used for storage of intermediate results
- Temporary local data and Static local data are part of this group
Static Local Data An instance DB is used to save static local data. These static local data can only be used in the FB, in whose block interface table they are declared.
When the block is exited, they are retained.
Parameters When the function block is called, the values of the actual parameters are stored in the instance data block.
If no actual parameter is assigned to an interface parameter in a block call, then the last value stored in the instance DB for this parameter is used in the program execution.
You can specify different actual parameters with every FB call. When the function block is exited, the data in the data block is retained. To keep the data unique for each instance of a call it is required to assign a different instance DB each time a call instruction to an FB is written in code.
You can program parameter-assignable blocks for frequently recurring program code. This has the following advantages:
- The program only has to be created once, which significantly reduces programming time.
- The block is only stored in the user memory once, which significantly reduces the amount of memory used.
- The FB can be called as often as you like, each time with a different address assignment. For this, the interface parameters (input, output, or in/out parameters) are supplied with different actual operands every time called.
Multi-instance data block
Definition Multi-instances enable a called function block to store its data in the instance data block of the calling function block. This allows you to concentrate the instance data in one instance data block and thus make better use of the number of instance data blocks available.”
So, where do most programmers write the majority of their code when using Siemens? That is a good question but one that should consider the use of FB’s as the main area for large control programs. Why? We have the ability to use static global variables in this area and also the ability to have all the variable types including arrays present. In the classroom environment, it is not necessary to consider this because programs here probably are rather small. When they grow, however, use the FB as a primary area for large programming efforts.
Later, in the Lab Text, we will see what the Festo Programs give in the way of FB’s. They are very large and complex.
Summary
This chapter introduces the student to the important concept of functions and function blocks from Siemens. It is not a complete study of this subject. In the appendix below is found a more thorough discussion as well as a discussion of optimization techniques using functions and function blocks. Data types are discussed as well here, a subject that has been discussed in earlier chapters but not to the extent as found in this appendix. The movement toward functions and function blocks from Siemens shows a steady move to convince the programming engineer that their organizational advantages are to be considered.
The appendix also explains more thoroughly global versus local data. Also, the subject of retentive data and how it is saved is shown. An example of newer programming techniques such as interspersing SCL in a network in a LAD program is shown as well. This provides very powerful design in program building.
Personally, I have not had many opportunities to use functions and function blocks in real-world applications. Most programs written by myself have been unique to the point that the organization of functions and function blocks gave little or no advantage. Even so, they should be understood because of their organizational advantages and time saved in constructing programs that may be used more than once.
Lab 14.1
Revisit the Binary Addition/Binary Subtraction lab from chapter 8 to subtract one 16 bit word from another and put the 16 bit result in a third word using a function and using the Siemens TIA software.
Remember:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 carry 1
1 + 0 + carry = 0 carry 1
1 + 1 + carry = 1 carry 1
These are the rules for binary addition. To see binary addition at work:
|
Carry |
|
|
|
1 |
1 |
1 |
1 |
|
|
|
|
|
Number 1 |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
1 |
0 |
0 |
|
+ Number 2 |
0 |
1 |
0 |
1 |
1 |
0 |
1 |
1 |
0 |
1 |
0 |
|
Results |
1 |
0 |
1 |
0 |
1 |
0 |
0 |
0 |
1 |
1 |
0 |
Binary addition may take place in ladder logic. Instructions are provided to carry out this function (ADD), but it is worthwhile to examine the process of binary addition using ladder logic
Since Bit 0 does not have a carry_in, half-adder logic may be employed but only for this bit. It can be seen that half-adder logic is simpler than full-add logic by comparing Fig. 8-35 (Half- Adder) to Fig. 8-36 (Full Adder).
IMAGE
Accessing Bits in Words (Siemens)
IMAGE
Accessing a tag with an AT overlay
The AT tag overlay allows you to access an already-declared tag of a standard access block with an overlaid declaration of a different data type. You can, for example, address the individual bits of a tag of a Byte, Word, or DWord data type with an Array of Bool. To overlay a parameter, declare an additional parameter directly after the parameter that is to be overlaid and select the data type “AT”. The editor creates the overlay, and you can then choose the data type, struct, or array that you wish to use for the overlay.
IMAGE
Rules
- Overlaying of tags is only possible in FB and FC blocks with standard access.
- You can overlay parameters for all block types and all declaration sections.
- An overlaid parameter can be used like any other block parameter.
- You cannot overlay parameters of type VARIANT
- The size of the overlaying parameter must be less than or equal to the size of the overlaid parameter.
- The overlaying variable must be declared immediately after the variable that it overlays and identified with the keyword “AT”.
Complete the lab using a function instead of coding each network as separate logic.
Binary Subtraction:
To perform binary subtraction, the easiest method is to find the 2’s complement of the second number and then add the two numbers together.
The best method of finding the 2’s complement requires the use of a memory bit. The rule requires that bits from the original number be copied to the 2’s complement number starting at the right-most bit. The rule applies until a “1” is encountered. The first “1” is copied but a memory bit is set after which the bits are “flipped”. Try this rule. It works and may be employed using ladder logic and a Latch bit to quickly find the 2’s complement of a number. The logic for finding the 2’s complement of a number in ladder logic is begun in Fig. 8-37.
Again, logic must be added to complete the function using rungs similar to rungs 4 and 5 of this figure but using bits 2 through15.
Again, code the logic using a function.
Lab 14.2
Implement the following:
Linking PLC UDT Tags to HMI Faceplates and Pop-ups
https://www.dmcinfo.com/latest–thinking/blog/id/9136/linking-plc-udt-tags-to-hmi-faceplates- and-pop-ups-in-tia-portal-v13-sp1
Lab 14.3 Repeat Lab 13.2C1 using an FB and UDT – Whack-a-Mole
Lab 13.2C Add a table of results including whether the player hit the light while the light was on and how long the response was delayed from when the light first turned on.
Results for each hit are to be saved sequentially in the table.
Lab 13.2C1 Implement 13.2C above with a UDT output table. Save sequential hit data for later display or analysis.
Problems
- Three types of parameters for interface of a function are: a
b c
- Local data is of two types. They are: a
b
- Data blocks are either Single or multi . What is the deciding factor which to use?
- List some program blocks that are standard.
- Describe a function or function block that would have the title “Engine” and have two types of engines that could be called – Diesel or Gasoline.
- What is the A-B process for adding a function?
For the next three programs, only use skeletal statements but enough to get the idea:
- In the Kitchen:
In the kitchen are several needs for automation including cooking breakfast. In the breakfast shelf are several kinds of cereal including oatmeal, cream-of-wheat and grits. Each requires the microwave and a cooking time. Each requires an amount to be weighed on a scale.
When the weight is achieved, the bowl is placed in the microwave for a time period. Write a program using FC’s or FB’s to achieve cooking of the breakfast cereal.
- Three numbers are to be added and the result displayed. Use a FC or FB to accomplish this.
- In OB1, there is a FC1 accessed that subtracts 1 from a number. Build the function block SUB1 to complete the operation. Show all tables and logic:
IMAGE
10. In OB1, there is a FC1 accessed that does the following:
IMAGE
Build the function block “INV” to complete the operation. Show all tables and logic:
11. In Siemens’ OB1, there is a FC1 accessed that does the following:
IMAGE
Build the function “INV” to complete the operation. Show all tables and logic inside the Function (FC):
12. In Siemens’ OB1, there is a FC1 accessed that does the following:
IMAGE
Build the function “MIX” to complete the operation. Show all tables and logic inside the Function (FC1):
Appendix 1
Siemens SIMATIC S7-1200 Part 2 – Re-Usable Libraries
See how easy it is to implement reusable Libraries in Step 7 Basic Software eliminating time consuming coding of repeat functions. This is part two of a four part series showcasing the time and cost saving benefits of the new S7-1200 and its Step 7 Basic development software. For more information see: http://www.usa.siemens.com/s7-1200
Siemens SIMATIC S7-1200 Part 4 – Project-wide Cross Referencing Made Easy
See how easy it is to troubleshoot the complete Controller and HMI software project together for both SIMATIC Basic HMI panels and S7-1200 Controllers. This is part four of a four part series showcasing the time and cost saving benefits of the new S7-1200 and its Step 7 Basic development software. For more information see: http://www.usa.siemens.com/s7-1200
Appendix 2
The following pages are from the Siemens Text:
Programming Guideline for S7-1200/1500 Entry ID: 81318674, V1.6, 12/2018
They summarize changes and upgrades to the Siemens Portal Language from Version 14 – TIA and later. Covered below is a review of the discussions above concerning FB’s and FC’s as well as general organization using them. Advantages of this type of programming are discussed as well. Speed of execution is an important part of this discussion.
3.2.4
Instances
The call of a function block is called instance. The data with which the instance is wo1rkiing is saved in an instance DB.
Instance DIBs are always created according to the specifications in the FB interface and can therefore not be changed in the instance DB.
Figure 3-8: Structure of the intelfaces of an FB
IMAGE
TheinstanceDB consists of a penmanent memory withthe interfacesinput,output, lnOut and static. Temporarytags are storediina volatile memory (Lstack).The L staokis always only validfor thecurrent processing..I.e.temporarytags have to be initiializedin each cycle.
Properties
- Instance DBs are always assigned to a FIB.
- Instance DBs do not have to be created manually in the TIA Portal and are created automatically when calling an FB.
- The structure of the instance DB iis specified in the appropriate FB and can only be changed there.
Recommendation
- Program it in a way so that the data of the instance DB can only be changed by the appropriate FB. This is lhow you can guarantee that the block can be used universally iin all kinds of projects.
For more information, please refer to chapter 3.4.1 Block interfaces as data exchange.
3.2.5
Multi-instances
With multi-instances called function blocks can store heir data in the instance data block of the called function block. This means, if another func1ion block is called in a function block, it saves its data in the instance DB of the higher-level FBs. The functionality of the called block is thus maiintained even when the calling block.is transferred..
The following figure shows an FB that uses another FB (“IEC Timer”). All data is saved in a multi instance DB. It is thus possible to create a block wiith an independent time behavior, for example, a clock generator.
Figure 3-9: Multi-instanoes
IMAGE
Advantages
- Reusability
- Multiple calls are possible
- Clearer program with fewer instance DBs
- Simple copying of programs
- Good options for structuring during programming
Properties
- Multi-instances are memory areas within iinstance DBs.
Recommendation
Use multi-instances in order to …
- reduce he number of instance DBs.
- create reusable and olear user pmgrams.
- program local functions, for example, timer, counter, edge evaluation.
Example
If you require the time and counter function, use he “IEC Timer” blocks and tihe “IEC Counter” bloc!ks instead of the absolutely addressed SIMATIC Timer. If possible, also always use multi-instances here. Thus, the number of blocks in the user program is kept low.
3.2.6
- Transferring instance as parameters (V14)
Instances of called blocks can be defined as lnOut parameters.
Advantages
- It is possible to create standardized functions whose dynamic instances are transferred.
- Only when calling the bloc!k it is specified what instance iis used.
IMAGE
3.2.7
IMAGE
Advantages
- Well-structured memory area
- High access speed
Properties
- All blocks in the user program can access global DBs.
- The structure of the global DBs can be arbitrarily made up of all data types.
- Global DBs are either created via the program editor or according to a previously created “user-defined PLC data type” (see chapter 3.6.4 STRUCT data type and PLC data types).
- A maximum of 256 structured tags (ARRAY, STRUCT) can be defined. This does not apply to tags that are derivedrom a PLC-data type.
Recommendation
- Use the global DBs when data is used in different program parts or blocks.
Note
More information can be found in the following entry:
How is the declaration table for global data blocks structured in STEP 7 (TIA Portal}?
https://support.industry.siemens.com/cs/WN/ en/view/68015630
3.2.8
Downloading without reinitialisation
In order to change user programs that already run in a controller, S7-1200 (firmware V4.0} and S7-1500 controllers offer the option to expand the interfaces of optimized function or data blocks during operation. You can load the changed blocks without setting the controller to STOP and without influencing the actual values of already loaded tags.
IMAGE
Execute the following steps whilst the controller is in RUN mode.
- Enable “Downloading without reinitialisation”
- Insert newly defined tags in existing block
- Load block iinto controller
- Advantages
Properties
Reloading of newly defined tags without interrupting the running process. The controller stays in “RUN” mode.
Downloading without reinitiatialization is only possible for optimized blocks.
The newly defined tags are initialized. The existing tags keep their current value.
A block with reserve requires more memory space in the controller .
The memory reserve depends on the work memory of the controller; however, it is max. 2 MB.
It is assumed that a memory reserve has been defined for block. By default, the memory reserve is set to 100 byte.
The memory reserve is defined individually for every block. The blocks can be variably expanded.
Recommendat;ion
- Define a memory reserve for blocks that are to be expanded during commissioning (e.g. test blocks). The commissioning process is not disturbed by a download since the actual values of the existing tags remain.
Example: Stetting memory reserve on the block
The following table describes how you can set the memory reserve for the downloading without reinitializing.
Table 3-3: Setting memory reserve
IMAGE
Note You can also set a default value for the size of the memory reserve for new blocks in the TIA portal.
In the menu bar, navigate to “Options – Settings” and then to “PLC programming
– General – Download without reinitialization”.
Example: Downloading without reinmahsation
The following example displays how to download without reinitialization.
Table 3 Load without reinitialization
IMAGE
3.2.9
Reusability of blocks
The bloclk concept offers you a number of optiions to program in a structured and effective way.
Advantages
- Blocks can be used universally in any location of the user program.
- Blocks can be used universally in different projects.
- When every block receives an iindependent task, a clear and well-st11uctured user program is automatically created.
- There are dearly fewer sources of errors.
- Simple error diagnostiic possible..
Recommendation
If you want to reuse the block, please note the following recommendations:
- Always look at blocks as encapsulated functions. I.e. each block represents a completed partial t.ask within the entire user program.
- Use several cyclic Main OBs to group the plant parts.
- Always execute a data exchange between the blocks via its interfaces and not via its instances (chapter 3.4.1 Block interfaces as data exchange).
- Do not use project-specific data and avoid the following block contents: Access to global DBs and use of single-instance DBs
Access to tags
Access to global constants
- Reusable blooks have the same requirements as know-how-protected blocks in libraries..This is why you have to check the blocks for reusability based on the “Multiple instance capabillity” block property. Compile the block before the check.
Figure 3-15: Block attributes
Attributes
0 IECcheck
0 Handle error wi hin block.
0 Se ENO automatically Optimized block acceH
Mui iple instance capabili
3.2.10
Auto numbering of blocks
For iinternal processing, required block numbers are automatically assigned by the system (setting in the block propertiies).
IMAGE
Advantages
- Conflicting block numbers, e.g. as a result of copying, automatically deletes the TIA Portal during compilation.
Recommendation
- Leave the existing setting “Automatic” unchanged.
IMAGE
3.3
- Block interface types
FBs and FCs have three different interface types: In, lnOut and Out. Via these interface types the blocks are provided with parameters. The parameters are processed and output again in the block. lnOut parameters serve for the transfer of data to the called block as well as the return of results. There are two diifferent options for the parameter transfer of data.
3.3.1
Call-by-value
When calling the block, the value of the actual parameter is copied onto the formal parameter of the block. For this, an additional memory in the called block is provided.
IMAGE
Properties
- Each block displays the same behavior as the transferred parameters
- Values are copied when calling the block
3.3.2
- Call-by-reference
When calling the block, a reference iis transferred to the address of the actual parameter. For this, no additional memory is required.
figure 3-19: Referencing the actual parameter (pointer to data storage of the parameter)
IMAGE
Properties
- Each block displays the same behavior as the referenced parameters.
- Actual parameters are referenced when the block is called, i.e. with the access, the values of the actual parameter are directly read or written.
Recomme-ndat;ion
- Generally use the lnOut interface type for structured tags (e.g. of the ARRAY, STRUCT, STRING, type ) in order to avoid enlarging the required data
memory unnecessarily.
3.3.3
Ove.rview for transfer of parameters
The following table gives a summarized overview of how S?-1200/1500 block parameters with elementary or structured data types are transferred.
Table 3-5: Overview for transfer of parameters
TABLE
Note When optimized data with the property “non-optimized access” is transferred when calling the block, it is generally transferred as copy. When the block contains many structured parameters this can quickly lead to the temporary storage area (local data stack) of the block to overflow.
This can be avoided by setting the same access type for both blocks (chapter
2.6.5 Parameter transfer between blocks with optimized and non-optimized access).
3.4
Memory concept
For STEP 7 there is generally the difference between the global and local memory area. The global memory area is available for each block in the user program. The local memory area is only available within the respective block.
3.4.1
Block interfaces as data exchange
If you are encapsulating the functions and program the data exchange between the blocks onlly via the interfaces, you will clearly have advantages.
Advantages
- Program can be made up modularly from ready blocks with partial tasks.
- Program is easy to expand and maintain.
- Program code is easier to read and test since there are no hidden cross accesses.
Recommendation
- If possible, only use local tags. Thus,. you can use the blocks universally and in a modular fashion.
3.4 Memory concept
- Use the data exchange via the block interfaces (In, Out, lnOut), his guarantees the reusability of the blocks.
- Only use the instance data blocks as local memory for the respective function block..Other blocks should not be written into instance data bloclks.
IMAGE
If only the block interfaces are used for the data exchange it can be ensured that all blocks can be used independent firom each other.
IMAGE
3.4.2
- Global memory
Memories are called global when they can be accessed from any location of the user program. There are hardware-dependent memories (for example, bit memory, times, counters, etc.) and global DBs. For hardware-dependent memory areas there is the danger that the program may not be portable to any controller because the areas there may already be used. This is why you should use global DBs instead of hardware-dependent memory areas.
Advantages
- User programs can be used universally and independent from the hardware..
- The user program can be modularly configured without having to divide bit memory areas for different users.
- Optimized global DBs are clearly more powerful than the biit memory address area that is not optimized for reasons of compatibility.
Recommendation
- Do not use any bit memory and use global DBs instead.
- Avoid hardware-dependent memory, such as, for example, clock memory or counter. Use the IEC counter and timer in connection with multi-instances instead {chapter 3.2.5 Multi-instances). The IEC tiimers can be found in “Instructions – Basic Instructions – Timer operations”.
IMAGE
3.4.3
Local memory
- Static tags
- Temporary tags
Recommendation
- Use the static tags if tihe values are required in the next cycle.
- Use the temporary tags as intermediate memory in the current cycle. The access time for temporary tags is shorter than for static ones.
- If an Input/Output tags is accessed very frequently., use a temporary·tag as intermediate memory to save runtime.
Note Optimized blocks: Temporary tags are initialized in each block call with the default value (S7-1500 / S7-1200 firmware V4 or higher).
Non-optimi ed blocks: Temporary tags are undefined for each call of the block.
3.4.4
Access speed of memory areas
STEP 7 offers different options of memory access. For system-related reasons there are faster and slower accesses to different memory areas.
Figure 3-23: Different memory access
IMAGE
Fastest access in the S7-1200/1I500 in descending order
- Optimized blocks: Temporary tags, parameters of an FC and FB, non-retentive static tags, tags [PLC data type]
- Optimized blocks whose access for compiiling is known: Retentive FB tags
Optimized global DBs
- Access to non-optimized blocks
- Indexed accesses with index that was calculated at runtime (e.g. Motor [ i] )
- Accesses that require checks at runtime
Accesses to DBs that are created at runtime or which were opened iindirectly (e.g. OPN DB [ i])
Register access or indirect memory access
- Copying of st11uctures between optimized and non-optimized blocks (apart from Array of Bytes)
3.5
Retentivity
In tihe event of a failure of the power supply, the controller copies the retentive data with its buffer energy from the controller’s work memory to a non-volatile memo1ry. After restarting the controller, the program processing is resumed with the retentive data. Depending on the controller, the data volume for retentivity has different sizes.
TABLE
Advantages
- Retentive data maintains its value when the controller goes to STOP and back to RUN or in the event of power failure and a restart of the controller.
Properties
For elementary tags of an optimized DB the retentivity can be set separately. Non optimized data blocks can only be defined completely retentive or non-retentive.
Retentive data can be deleted with the actions “memory reset° or “Reset to factory settings” via:
- Operating switch on the controller (MRES)
- Display of the controller
- Online via STEP 7 (TIA Portal)
Recommendation
- Do not use the setting “Set in IDB”. Always set the retentive data in the function block and not in the instance data block.
The “Set in IDB” setting increases the processing time of the program sequence. Always either select “Non-retain” or “Retain” for the interfaces in the IFB.
IMAGE
Example: Retentive PLC tags
The setting of the retentive data is performed in the tables of the PLC tags, function blocks and data blocks.
IMAGE
3.6
Symbolic addressing
3.6.1
Symbolic instead of absolute addressing
The TIA Portal is optimized for symbolic programming. This results in many advantages. Due to symbolic addressing, you can program without having to pay attention to the internal data storage. The controller handles where the best possible storage is for the data. You can therefore completely concentrate on the solution for your application task.
Advantages
- Easier to read programs through symbolic tag names
- Automatic update of tag names at all usage locations in the user program
- Memory storage of the program data does not have to lbe manually managed (absolute addressing)
- Powerful data access
- No manual optimization for periormance or program size reasons required
- Auto-complete for fast symbol input
- Fewer program errors due to type safety (valiidity of data types is checked for all accesses)
Recommendation
- “Don’t worry about the storage of the data”
- “Think” symbolically. Enter the “descriptive” name for each function, tag or data, such as, for example, Pump_boiler_1, heater_room_ 4, etc..Thus a created program can be simply read, without requiring many comments.
- Give all the tags used a direct symbolic name and define them afterwards with a right-click.
IMAGE
There is an elegant method to save time, if you want to define several tags in a network. First of all, assign all tag names. Then define all tags at the same time wiith the dialog of step 4.
Note More information can be found in the following entry:
What are the advantages of using symbolic addressing for S7-1500 in STEP 7
(TIA Portal)? https://support.industry.siemens.com/cs/ww/en/view/67598995
3.6.2
ARRAY data type and indirect field accesses
The ARRAY data type represents a data structure which is made up of several elements of a data type. The ARRAY data type is suitable, for example, for the storage of recipes, material tracking in a queue, cyclic process acquisitiion, protocols, etc.
IMAGE
You can indirectly access individual elements in the ARRAY with an index (array
[“index”]).
IMAGE
Advantages
- Easy access through ARRAY index
- No complicated pointer creation required
- Fast creation and expansion possible
- Useable in all programming languages
Properties
- Structured data type
- Data structure made of fixed number of elements of the same data type
- ARRAYs can also be created multi-dimensional
- Possible indirect access with runtime tag with dynamic index calculation at runtime
IMAGE
3.6.3
Formal parameter Array[*] (V14 or higher)
With the formal parameter Array [‘”], arrays with variable length can be transferred to functions and function blocks.
With the instructions “LOWER_BOUND” and “UPPER_BOUND” the array limits can be determined.
Advantages
- Blocks that can process the flexible arrays with different lengths
- Optimum readability due to fully-symbolic programming
- No pointer programming for arrays of different lengths necessary anymore
IMAGE
3.6.4
STRUCT data type and PLC data types
The STRUCT data type represents a data structure whiich is made up of elements of different data types. The declaration of a structure lis performed in the respective block.
IMAGE
In comparison to structures, PLC data types are •defined across the controller in the TIA Portal and can be centrally changied. All usage locations are automatically updated.
PLC data types are declared in the “PLC data types” folder in the project navigation before being used.
IMAGE
Advantages
- A change in a PLC data type lis automatically updated in all usage locations in the user program.
- Simple data exchange via block interfaces between several blocks
- In PLC data types STRING tags with defined length can be declared {e.g., String[20]). As of TIA V14 a global constant can also be used for the length (e..g., String[LENGTH]).
If a STRING tag is declared without de ined length, the tagi has the maximum length of 255 characters.
Properties
- PLC data types always end at WORD limits (see the figures below).
- Please consider this system property when …
using structures in 1/0 areas (see chapter 3.6.5 Access to 1/0 areas with PLC data types).
using frames with PLC data types for communication. using parameter records with PLC data types for 1/0. using non-optimized blocks and absolute addressing.
IMAGE
Recommendat;ion
Use the PLC data types to summanize several associiated data, such as, e.g. frames or motor data (setpoint, speed, rotational direction, temperature, etc.)
3.6.5
Access to 1/0 areas with PLC data types
With S?-1500 controllers, you can create PLC data types and use them for structured and symbolic access to inputs and outputs..
Figure 3-35: Access to 1/0 areas with PLC data types
IMAGE
- PLC data type with all required data
- PLC tag of the type of the created PLC data type and start address of the 1/0 data area (%Ix.0 or %Qx.0, e.g., %10.0, %Q12.0, …)
- Transfer of the PLC tag as actual parameter to the function block
- Output of the function block is of the type of the created PLC data type
Advantages
- High programming efficiency
- Easy multiple usability thanks to PLC data types
Recommendation
- Use PLC data types for access to 1/0 areas, for example, to symbolically receive and send drive telegrams.
Note Individual elements of a PLC data type of a tag can also be directly accessed in the user program:
IMAGE
3.6.6
- Slice access
For S7-1200/1500 controllers, you can access the memory area of tags of the Byte, Word, DWord or LWord data type. The division of a memory area (e.g. byte or word) into a smalleirmemory area (e.g. Bool) is also called slice. The figure below displays the symbolic bit, byte and word accesses to tlhe operands.
IMAGE
Advantages
- High programmiing efficiency
- No additional definition in the tag declaration required
- Easy access (e.g. control bits)
Recommendation
- Use the slice access via AT construct rather than accessing certain data areas in operands.
Note More information can be found in the following entry:
How in STEP 7 (TIA Portal) can you access the unstructured data types bit-by bit, byte-by-byte or word-by-word and symbolically?
https://support.industry.siemens.com/cs/ww/en/view/57374718
3.6.7
IMAGE