Programming Recommendation 081021
Programming Recommendation 081021
SIMATIC S7
Advanced Workshop
• Goal 2
• Fast System reaction 13
• Symbolic access 25
• New instructions 53
• Multiple use 62
• Error handling 67
• Hints 70
• Recommendations 74
For each application the right program block and the right program language
OB FC FB DB
DB Global
Local
Program block
OB – Organization block
OBs are the interface between operating system and user program
FB FC
Local
HW OS OB FC
FC
Global
OS : Operating system
HW: Hardware configuration
Program block: OB – Organization block
OB – Organization block:
OB
▪ Interface between CPU operating system and user
program
▪ OBs including the user program
▪ Organization blocks are called from CPU operating
system if an event occurs
▪ Startup program
▪ Cyclic program
▪ Event triggered program
▪ Error triggered program
Function:
FC ▪ Code blocks with temporary program memory
▪ For the programming of often needed complex
functions
▪ Parameter should be shared via the interface
▪ For storing data permanently, functions have to use
data blocks
▪ Several outputs are possible
▪ SCL can use a Function in a formula
<Operand> := <FC-Name> (Parameter list) …...;
Call of a
function in the
block editor
Program block: Function block (FB)
Function block :
▪ Code blocks that store their values so that they remain available
FB
even after the block has been executed.
Local
▪ All data are stored in the instance DB - the instance is the static
program memory of the FB.
Instance
FB
DB Instance
Local
Input
Output
InOut
Static
Program block: Instance – Data Block
Instance-Data Block:
▪ IDBs are assigned to a specific FB
▪ IDBs should only be accessed from their owner-FB and not from all over the
program
▪ IDBs are created when calling an FB. The structure can not be modified
My_FBD_BLOCK_DB
Instance-
DB
Data for
“My_FBD_Block“
Program blocks: Multi-instance
Multi-instance:
▪ A FB (FB_b) can also call other FBs (FB_a) (embedded) and will be used as one unity. The
instance will be created if the FB are used (DB_b)
▪ A multi-instance DB (DB_b), is a data block that includes other instance data block of other
FBs
FB_b
FB_a
FB
FB_b FB_a
DB_b
_a
DB_b
Local Data FB_b
Data FB_a_Instance
Program block: Global Data-Block
FB
DB
Local FC
TIA Portal – Programming recommendations
• Goal 2
• Fast System reaction 13
• Symbolic access 25
• New instructions 53
• Multiple use 62
• Error handling 67
• Hints 70
• Recommendations 74
Benefit:
1. The data access is as fast as possible, optimized of the system and independent of the
declaration
2. No risk of error due to inconsistent absolute access, because the access only is symbolic
3. Changes on declaration do not cause access errors. Sample: insert a variable
4. It’s possible defined single variables retentive
5. No need to change the instance DB settings. All IDB settings used from the related DB. E.g.
setting retentive)
6. Reserve allow online changes without CPU STOP, this keeps the actual values
Programming: access speed on different memory areas
1 Retentive variables
Access in S7-1200/1500 1 = fast … 3 = slow
Not structured
elementary data types
FC-Parameter 1
1
Non retentive
variables 1 Access to
optimized blocks
Temporary
variables Copies between
1 3
optimized and
“not optimized” blocks
Indexed access Access to “not
with runtime index 1 optimized“ blocks
Access which cause 3
checks on runtime 3
(register- and indirect 3 2
DB-access)
Programming: access speed on different memory areas
▪ Temporary variables
1 ▪ Optimized blocks, which access already known during compile
▪ Global DBs, which often used
▪ Indirect access which are calculated on runtime access, (e.g. Motor[i])
▪ Access on DBs which are created during runtime or opened indirect (e.g. OPN DB[i])
3 ▪ Register access or indirect memory access (e.g. L DBW[AR1, P#0.0])
▪ Copy of structures from optimized to “not optimized” blocks
▪ FB-variables with retentive setting “set in IDB”
Program optimizing for high speed performance S7-1200/1500
Performance optimizing:
▪ Indirect access only with ARRAY index
▪ Only symbolic access
▪ Reduce program loops as short as possible
▪ Call instructions only if:
Result is required and the input values has changed
▪ STL:
▪ Don’t use “status word” or flags of it – this bits are emulated if you use it (slow)
▪ LAD/FBD:
▪ Activate ENO only if it is used
▪ Deactivated ENO save time for internal check routines
Higher Performance: ENO deactivated
2
connected instruction always
executed Advantage: fast process
due to minimal checks
Interrupt: HW event Organization Block
e.g. edge up
e.g. rising I6.1
edge I0.0
Hardware Hardware
interrupt interrupt_1
OB40 OBxxx
Interrupt: Fast Reaction due to short input delay time
Additional instructions:
Change relations between OB and
event
Only direct
periphery access Attach / Detach
on outputs enable events with OB
fast reactions
<output>:P
Enable / Disable
interrupt OBs
TIA Portal – Programming recommendations
• Goal 2
• Fast System reaction 13
• Symbolic access 25
• New instructions 53
• Multiple use 62
• Error handling 67
• Hints 70
• Recommendations 74
PLC_1 HMI_1
PLC tag Absolute Intern PLC Intern HMI
Interne HMI HMI tag Access Connection
name address reference ID reference ID name mode with PLC
Clock_1Hz M2.5 000123 009876 Clock_1Hz <symbolic PLC_1
access>
Valve_2 Q0.3 000138 000578 Valve_2 <symbolic PLC_1
access>
Think symbolic! Add direct the tag name and select the address later on
It is possible selecting
multiple tags with one
right mouse click.
Blocks: Auto numbering of blocks
Array:
▪ Used for
▪ Recipes
▪ Monitoring material in a queue
▪ Collect process values
▪ Create a protocol
Indirect ARRAY access possible with LAD / FBD, STL and SCL
Sample:
“Field_Data”. Syntax: <name>[„index“]
Numbers[“i”]
Access index calculated
i := DINT during runtime
(Node: temporary DINT
index gets the highest
performance!)
9
8
7
6
5
4
3
2
1
0
AWL:
SCL:
All Languages S7-1200/1500: ARRAYs in use
Useable memory areas: Suggestion: Use only tags of the block interface
- Temporary variables
Type-safe and fast access possible with optimized blocks
All languages S7-1200/1500: Slice access
„My_LW_Variable“ LWORD
Passing by value:
FB
„My_Int“ IN
FC
„My_Int“ Wert: '2' Wert: '2'
IN
Wert: '2'
Wert: '2'
When passing data from different data areas (e.g. standard to optimized) the adaption
of the data is done by the system:
1. Copy the data to an area of the local data of the caller in the format of the called block
2. Pass the data in the correct format to the called block
Callee block
Consequences:
Local data
➔ Called block needs more local (optimized)
data when passing data from
different data areas Variables / structures 2
1
in the standard area, Called block
➔ With parameter passing by e.g.: (optimized)
reference, the reference to the - Standard data
local data of the called (and not blocks
to actual data) is used - I/O/M/T/C
Symbolic access: Communication with optimized blocks
VARIANT:
pointer includes the length
Communication blocks sending data in well known classic format
▪ The content of optimized blocks is send in same order like “not optimized” blocks.
STEP 7 takes care about the byte order.
Examples S7-300 CPUs – S7-1500 CPUs
Direct, symbolic
access to DB-variable
Open DB and DB-array
Absolute access to an
element of the DBs
Access to an array-
element of the DB
• Only one DB-access, thus performance improvement in S7-300 • Symbolical access for best performance
• Absolute access on array-elements (register indirect) • Better comprehensibly and readability
➔ thus error-prone
Examples S7-300 CPUs – S7-1500 CPUs
Copy of structures
Direct, symbolical
Symbolical access to
access to DB-variable
structure which was
copied in
• Absolute access to overwrite structure – thus error- • Symbolical access for best performance
prone • Better comprehensibly and readability
Examples S7-300 CPUs – S7-1500 CPUs
Copy anonymous
structures by use of
BLKMOV
Using indirect addressing the address, the values Ideas to make the indirect addressing more flexible
should be read/written to, are calculated during and safe:
runtime of the program. Most of the times, the addresses that should be accessed
are already known during program creation:
Advantages: ➔ Adjoined data of same type: use array
• Reducing the programming effort ➔ Accesses to different areas or similar:
• Make the program more flexible Access-list(s) with the equivalent accesses are created
for indirect addressing
Disadvantages: e.g. program needs access to 3 variables (in different
access areas):
• Bad comprehensibility because cross-
Index Access
references are not available Access-list: access to
the respective 1 Input_Word_0 (%IW0)
• Access errors or writing in wrong areas might
variables is done by 2 „Processdata“.Temperature
happen index 3 Output_Word_4 (QW4)
Safe indirect addressing
• Use of a FCs with input INT, returning the used data 1 Input_Word_0 (%IW0)
type 2 „Processdata“.Temperature
Any-Pointer…for programming of
functions, which are able to process Function with Variant-Pointer as InOut
various data types parameter
FC FC
ANY1: P#DB.DBx0.0 Byte 10 MOVE_BLOCK(Data1[0], 10, Data2[#index])
ANY2.Any_ID = B#16#10 //RID for Any
ANY2.DataType = B#16#02 //Byte Data1 : Array[0..10] of Byte
... Data2 : Array[0..50] of Byte
ANY2.Address = …… //DB + Offset calculation
BLKMOV(ANY1, ANY2)
Data1 : Array[0..10] of Byte
Data2 : Array[0..50] of Byte
Intuitive programming and less code
Long-winded, cumbersome Symbolic – Easier to read and less fault-prone
Internal CPU knowledge must be available Numerous system functions available (FILL_BLK
Fault-prone at address changes for initializing, MOVE_BLK for copying,…)
ANY vs. symbolic programming
Usage of ANY (2): Structure transfer
FC
FC
UDT InOut: UDT
Struct IN: ANY
FC UDT FC
UDT
UDT In: Val : ANY UDT InOut: Val : Variant
Performant transfer of structured variables by S7- Flexible forwarding of various structured variables
300/400 at S7-1200/1500
Structure gets lost at module interface Instruction of type analysis of Variant variables
Bytes have to be paraphrased manually according Variables can be used type safe with the
to memory structure assistance of system functions
ANY vs. symbolic programming
DB or DB_ANY is assigned
Precondition: DB of type UDT, SDT or array-DB
• Goal 2
• Fast System reaction 13
• Symbolic access 25
• New instructions 53
• Multiple use 62
• Error handling 67
• Hints 70
• Recommendations 74
How can the data a VARIANT is pointing to, be used? Examples to instructions
VARIANT to Target type
To use the data it must be copied to variable:
Copy single variables • UDT_1 • UDT_1
VariantGet Data type of target variable and variable the • REAL • REAL
VARIANT is pointing to have to match • DINT • DWORD
After having processed the data, the following instructions can be used to transfer them (back) to
the with the VARIANT referenced variables, if needed:
1 5
DB
Silo_Water DB
Silo_Sugar
2 Silo_
DB
Handling
Silo_Milk
3 Precondition: Use DB of UDT
Application example „variable data-access“
„Indexed addressing“ with VARIANT
1 Silo_Water
3
Silo_ Silo_Sugar
Handling
Silo_Milk
GetBlockName
GetInstanceName
GetSymbolName
Status of CPU load S7-1500 S7-1200 S7-300/400/WinAC
Percentage of Percentage of
• Analysis of the CPU work load e.g. regarding
cycle OB´s communication load
communication and user program code
✓Percentage of the communication load regarding the 45
55 %
%
cycle time
30 %
✓Percentage of program cycle Organization Blocks 40 %
(e.g. OB1,..)
regarding the cycle time höher priore
Obs (z.B. OB
✓Percentage of the cyclic interrupt Organization Blocks 3x)
(e.g. OB 3x, OB 8x) regarding the cycle time 15 %
15 % zyklisches
Programm (OB
1)
Percentage of
Basis of percentage calculations:1s cyclic interrupt
OB´s
Programming: Status of CPU-load S7-1500 S7-1200 S7-300/400/WinAC
MODE
• 1 - 3: Return actual Runtime data of an OB
configured at parameter „OB“
• 10 and 11: Part of high priorized OB´s and
communication from the main cycle time
• 30 – 32: Configured settings of the user program
Example:
▪ MODE 1: runtime of the OB, configured at parameter
„OB“ in LTIME
▪ MODE 11: Communication percentage from the main
OB / INFO cycle time in UINT
• Goal 2
• Fast System reaction 13
• Symbolic access 25
• New instructions 53
• Multiple use 62
• Error handling 67
• Hints 70
• Recommendations 74
If a FB is called on a FB you get the choice “Multi instance”. The instance-data of the called FB is embedded
on the source FB.
“Multi instance” supports multiple use and multiple call
Instances are
placed on the
interface of
calling FB
User program
Common Group
Main FB
OB1 FC
Local
Arrangement of the program in different
executable groups is possible.
Group A
A_Main FB Execution of OBs in numeric order
Local
OB200 FC
Group B
B_Main FB
Local
OB300 FC
Tasks, plant parts: Arrange multiple Program-cycle blocks (Main)
• Goal 2
• Fast System reaction 13
• Symbolic access 25
• New instructions 53
• Multiple use 62
• Error handling 67
• Hints 70
• Recommendations 74
CPU stays in RUN also without error OB, Entry to diagnostic buffer
FC
S7-1200
FC
FC OB
SFC
• Goal 2
• Fast System reaction 13
• Symbolic access 25
• New instructions 53
• Multiple use 62
• Error handling 67
• Hints 70
• Recommendations 74
SCL
Use instance names which
include the instruction name
Advantage:
More intelligible programs
Hints: LAD/FBD recording of network content
LAD/FBD
Use short network titles on the networks
Advantages:
If all networks collapsed, the program
structure and network contents is easily
to identify
Hot key:
<Alt+F11> expand all networks
<Alt+F12> collapse all networks
Hints: expand the editor area
▪ If possible, use a PC with multiple large screens. Not only advantageous for TIA-Portal
▪ Advantage: drag & drop can be better used if more as two editors open
▪ If only one editor or beamer with small resolution is used:
Recommendation: with double-click on the editor bar the work area can easily
expanded or collapsed
TIA Portal – Programming recommendations
• Goal 2
• Fast System reaction 13
• Symbolic access 25
• New instructions 53
• Multiple use 62
• Error handling 67
• Hints 70
• Recommendations 74
Programming recommendations for S7-1200 and S7-1500 with STEP 7 (TIA Portal)
http://support.automation.siemens.com/WW/view/en/67582299
Migration:
Migration production plants – http://support.automation.siemens.com/WW/view/en/83558085
Migration PLC´s – http://support.automation.siemens.com/WW/view/en/83557459
Migration Vizualisation – http://support.automation.siemens.com/WW/view/en/76878921
Migration communication – http://support.automation.siemens.com/WW/view/en/83558087
Migration periphery – http://support.automation.siemens.com/WW/view/en/83558994
Let us know if there is anything
we can support you with!
For further information and support, please contact:
Mr. Tran Van Hieu
SITRAIN Manager – Digital Industry Academy
Siemens Vietnam
Email: [email protected]
www.siemens.com.vn
www.facebook.com/Siemens.Vietnam