Click here to load reader
Upload
dinhxuyen
View
213
Download
0
Embed Size (px)
Citation preview
t-
Automating withSTEP7inSTLandSCLProgrammable ControllersSIMATIC S7-300/400
by Hans Berger
3rd revised edition, 2005
Publicis Corporate Publishing
Tableof Contents
Table of Contents
Introduction 21
1 SIMATIC S7-300/400Programmable C o n t r o l l e r . . . . 22
1.1 Structure oftheProgrammable Controller 22
1.1.1 Components 221.1.2 S7-300 Station 221.1.3 S7-400 Station 241.1.4 Fault-Tolerant SIMATIC 251.1.5 Safety-Relevant SIMATIC . . . . 261.1.6 CPU Memory Areas 27
1.2 Distributed I/O 31
1.2.1 DP Master System 311.2.2 DP Master 321.2.3 DPSlaves 321.2.4 Further Network Components. . . 331.2.5 Connection to PROFIBUS PA . . 341.2.6 Connection to AS-Interface . . . . 341.2.7 Connection to a Serial Interface. . 35
1.3 Communications 36
1.3.1 Introduction 361.3.2 Subnets 371.3.3 Communications Services 391.3.4 Connections 40
1.4 Module Addresses 41
1.4.1 Signal Path 411.4.2 SlotAddress 421.4.3 Module Start Address 421.4.4 Diagnostics Address 431.4.5 Addresses for Bus Nodes 43
1.5 Address Areas 43
1.5.1 User Data Area 431.5.2 Process Image 441.5.3 Consistent User Data 451.5.4 BitMemories 46
2 STEP 7 Programming Software . 47
2.1 STEP 7 Basic Package 47
2.1.1 Installation 472.1.2 Automation License Manager . . . 472.1.3 SIMATIC Manager 482.1.4 Projects and Libraries 492.1.5 Multiprojects 512.1.6 Online Help 52
2.2 Editing Projects 52
2.2.1 Creating Projects 522.2.2 Managing, Rearranging and
Archiving 542.2.3 Project Versions 542.2.4 Creating and Editing Multiprojects. 55
2.3 Configuring Stations 56
2.3.1 Arranging Modules 582.3.2 Addressing Modules 582.3.3 Parameterizing Modules 582.3.4 Networking Modules with MPI . . 592.3.5 Monitoring and Modifying
Modules 59
2.4 Configuring the Network 59
2.4.1 Configuring the Network View. . . 602.4.2 Configuring a DP Master System
with the Network Configuration . . 612.4.3 Configuring Connections 622.4.4 Network Transitions 652.4.5 Loading the Connection Data . . . 662.4.6 Adjusting Projects in the
Multiproject 66
2.5 Creating the S7 Program 68
2.5.1 Introduction 682.5.2 Symbol Table 682.5.3 STL Program Editor 702.5.4 SCL Program Editor 742.5.5 Rewiring 772.5.6 Address Priority 782.5.7 Reference Data 79
12
Table of Contents
2.5.8 Multilingual Comments and
Display Texts 80
2.6 Online Mode 81
2.6.1 Connecting a PLC 812.6.2 Protecting the User Program. . . . 822.6.3 CPU Information 832.6.4 Loading the User Program into
the CPU 832.6.5 Block Handling 842.7 Testing the Program 87
2.7.1 Diagnosing the Hardware 872.7.2 Determining the Causeofa STOP. 872.7.3 Monitoring and Modifying
Variables 872.7.4 Forcing Variables 892.7.5 Enabling Peripheral O u t p u t s . . . . 902.7.6 STL Program Status 912.7.7 Monitoring and Controlling Data
Addresses 932.7.8 Debugging SCL Programs 93
3 SIMATIC S7 Program 96
3.1 Program Processing 96
3.1.1 Program Processing Methods . . . 963.1.2 Priority Classes 973.1.3 Specifications for Program
Processing 98
3.2 Blocks 100
3.2.1 Block Types 1003.2.2 Block Structure 1013.2.3 Block Properties 1033.2.4 Block Interface 105
3.3 Addressing Variables 107
3.3.1 Absolute Addressing of Variables 1073.3.2 Indirect Addressing 1083.3.3 Symbolic Addressing of Variables 108
3.4 Programming Code Blockswith STL 110
3.4.1 Structure of an STL Statement. . 1103.4.2 Programming STL Code Blocks
Incrementally 1103.4.3 Overview Window 1113.4.4 Programming Networks 1123.4.5 Source-File-Oriented Program-
ming of STL Code Blocks . . . . 113
3.5 Programming Code Blockswith SCL 117
3.5.1 Structure of an SCL Statement . . 1173.5.2 Programming SCL Code Blocks . 117
3.6 Programming Data Blocks . . . . 121
3.6.1 Programming Data BlocksIncrementally 121
3.6.2 Source-Oriented Data BlockProgramming 124
3.7 Variables and Constants 126
3.7.1 General Remarks ConcerningVariables 126
3.7.2 General Remarks RegardingData Types 126
3.7.3 Elementary Data Types 1273.7.4 Complex Data Types 1273.7.5 Parameter Types 130
Basic Functions 131
4 Binary Logic Operation« . . . . 132
4.1 Processing a Binary LogicOperation 132
4.2 Elementary Binary LogicOperations 134
4.2.1 ANDFunction 1354.2.2 ORFunction 1354.2.3 Exclusive OR Function 135
4.3 Negating the Result of theLogic Operation 137
4.4 Compound Binary LogicOperations 137
4.4.1 Processing Nesting Expressions . 1384.4.2 Combining AND Functions
AccordingtoOR 1404.4.3 Combining OR and Exclusive OR
Functions According to AND. . . 1404.4.4 Combining AND Functions
According to Exclusive OR. . . . 1404.4.5 Combining OR Functions and
Exclusive OR Functions 1414.4.6 Negating Nesting Expressions . . 141
5 Memory Functions 142
5.1 Assign 142
13
Table of Contents
5.2 SetandReset 142
5.3 RS Flipflop Function 144
5.3.1 Memory Functions with ResetPriority 144
5.3.2 Memory Function with SetPriority 144
5.3.3 Memory Function in a BinaryLogic Operation 144
5.4 Edge Evaluation 145
5.4.1 Positive Edge 1465.4.2 Negative Edge 1465.4.3 Testing a Pulse Memory Bit. . . 1475.4.4 Edge Evaluation in a Binary
Logic Operation 1475.4.5 Binary Sealer 148
5.5 Example of a Conveyor BeltControl System . 148
6 Move Functions 152
6.1 General Remarks on Loading
and Transferring Data 152
6.2 Load Functions 154
6.2.1 General Representation of aLoad Function 154
6.2.2 Loading the Contents ofMemory Locations 154
6.2.3 Loading Constants 1556.3 Transfer Functions 156
6.3.1 General Representation of aTransfer Function 156
6.3.2 Transferring to Various MemoryAreas 156
6.4 Accumulator Functions 157
6.4.1 Direct Transfers Between
Accumulators 157
6.5 Exchange Bytes in Accumulator 1. 158
6.6 System Functions for DataTransfer 159
6.6.1 Copying Memory Area 1606.6.2 Uninterrüptible Copying of
Variables 1606.6.3 Initializing a memory area. . . . 1616.6.4 Copying STRING Variables . . 1616.6.5 Reading from Load Memory . . 161
6.6.6 Writing into the Load Memory. . 162
7 Timer Functions 164
7.1 Programming a Timer 164
7.1.1 Starting a Timer 1647.1.2 Specifying the Time 1657.1.3 Resetting a Timer 1667.1.4 Enabling a Timer 1667.1.5 Checking a Timer 1667.1.6 Sequence of Timer Instructions . 1677.1.7 Clock Generator Example . . . . 167
7.2 Pulse Timers 168
7.3 Extended Pulse Timers 170
7.4 On-Delay Timers 172
7.5 Retentive On-Delay Timers . . . 174
7.6 Off-Delay Timers 176
7.7 IEC Timer Functions 178
7.7.1 Pulse Generation SFB 3 TP . . . 1787.7.2 On Delay SFB 4 TON 1797.7.3 Off Delay SFB 5 TOF 179
8 Counter Functions 180
8.1 Setting and Resetting Counters. . 180
8.2 Counting 181
8.3 Checking a Counter 182
8.4 Enabling a Counter 182
8.5 Sequence of Counter Instructions 183
8.6 IEC Counter Functions 184
8.6.1 Up Counter SFB 0 CTU 1848.6.2 Down Counter SFB 1CTD. . . . 1848.6.3 Up-Down Counter SFB 2 CTUD. 185
8.7 Parts Counter Example 185
Digital Functions 189J
9 Comparison Functions . . . . . 190
9.1 General Representation of aComparison Function 190
9.2 Description of the ComparisonFunctions 191
9.3 Comparison Function in a LogicOperation 192
14
Table of Contents
10 Arithmetic Functions 194
10.1 General Representation of anArithmetic Function 194
10.2 Calculating with Data TypeINT 195
10.3 Calculating with Data TypeDINT 196
10.4 Calculating with Data TypeREAL 197
10.5 Successive ArithmeticFunctions 198
10.6 Adding Constants toAccumulator 1 199
10.7 Decrementing andIncrementing 199
11 Math Functions 201
11.1 Processing a Math Function . . . 201
11.2 Trigonometrie Functions 202
11.3 Are Functions 202
11.4 Other Math Functions 202
12 Conversion Functions 204
12.1 Processing a ConversionFunction 204
12.2 Converting INT and DINTNumbers 205
12.3 Converting BCD Numbers. . . . 205
12.4 Converting REAL Numbers . . . 206
12.5 Other Conversion Functions . . . 207
13 Shift Functions 209
13.1 Processing a Shift Function . . . 209
13.2 Shifting 210
13.3 Rotating 212
14 Word Logic 214
14.1 Processing a Word LogicOperation 214
14.2 Description of the Word LogicOperations 216
Program Flow Control 217]
15 Status Bits 218
15.1 Description ofthe Status Bits. . . 218
15.2 Setting the Status Bits and theBinary Flags 220
15.3 Evaluating the Status Bit 222
15.4 Using the Binary Result 224
16 Jump Functions 226
16.1 Programming a Jump Function . . 226
16.2 Unconditional Jump 227
16.3 Jump Functions with RLO andBR 227
16.4 Jump Functions withCCOandCCl 228
16.5 Jump Functions with
OVandOS 230
16.6 Jump Distributor 230
16.7 Loop Jump 231
17 Master Control Relay 232
17.1 MCR Dependency 232
17.2 MCR Area 233
17.3 MCR Zone 233
17.4 Setting and Resetting I/O Bits . . 234
18 Block Functions 236
18.1 Block Functions for Code Blocks. 236
18.1.1 Block Calls: General 23718.1.2 CALL Call Statement 23718.1.3 UC and CC Call Statements . . . 23818.1.4 Block End Functions 23918.1.5 Temporary Local Data 23918.1.6 Static Local Data 242
18.2 Block Functions for Data Blocks . 244
18.2.1 Two Data Block Registers . . . . 24418.2.2 Accessing Data Addresses . . . . 24518.2.3 Open Data Block 24718.2.4 Exchanging the Data Block
Registers 24818.2.5 Data Block Length and Number . 24818.2.6 Special Points in Data Addressing 248
15
Table of Contents
18.3 System Functions for DataBlocks 250
18.3.1 Creating a Data Block in theWork Memory 250
18.3.2 Creating a Data Block in theLoad Memory 251
18.3.3 Deleting a Data Block 25218.3.4 Testing a Data Block 252
18.4 Null Operations 252
18.4.1 NOP Statements . . . . . . . . . 25218.4.2 Program Display Statements . . 253
19 Block Parameters 254
19.1 Block Parameters in General . . 254
19.1.1 Defming the BlockParameters 254
19.1.2 Processing the BlockParameters 254
19.1.3 Declaration of the BlockParameters 255
19.1.4 Declaration of the FunctionValue 256
19.1.5 Initializing Block Parameters . . 257
19.2 Formalparameters 257
19.3 Actual Parameters 260
19.4 "Passing On" Block Parameters. 263
19.5 Examples 264
19.5.1 Conveyor Belt Example 26419.5.2 Parts Counter Example 26519.5.3 Feed Example 265
Program Processing . 269[
20 Main Program . . . . . . . . . 270
20.1 Program Organization 270
20.1.1 Program Structure 27020.1.2 Program Organization 271
20.2 Scan Cycle Control 272
20.2.1 Process Image Updating 27220.2.2 Scan Cycle Monitoring Time . . 27420.2.3 Minimum Scan Cycle Time,
Background Scanning 27520.2.4 ResponseTime 27620.2.5 Start Information 276
20.3 Program Functions 277
20.3.1 Time 27820.3.2 Read System Clock 28020.3.3 Run-Time Meter 28020.3.4 Compressing CPU Memory . . . 28220.3.5 Waiting and Stopping 28220.3.6 Multiprocessing Mode 282
20.4 Communication viaDistributed I/O 283
20.4.1 Addressing Distributed I/O. . . . 28420.4.2 Configuring Distributed I/O . . . 28820.4.3 Special Functions for
Distributed I/O 29420.4.4 System Functions for
Distributed I/O . . . 298
20.5 Global Data Communication . . . 303
20.5.1 Fundamentals 30320.5.2 Configuring GD communication . 30520.5.3 System Functions for GD
Communication 307
20.6 S7 Basic Communication 307
20.6.1 Station-Internal S7 BasicCommunication 307
20.6.2 System Functions for DataInterchange within a Station . . . 309
20.6.3 Station-External S7 BasicCommunication 310
20.6.4 System Functions for Station-Ex-ternal S7 Basic Communication . 311
20.7 S7 Communication 313
20.7.1 Fundamentals 31320.7.2 Two-Way Data Exchange . . . . 31520.7.3 One-Way Data Exchange 31720.7.4 Transferring Print Data 31820.7.5 Control Functions 31820.7.6 Monitoring Functions 319
20.8 PtP Communication with S7-300C 322
20.8.1 Fundamentals 32220.8.2 ASCII Driver and 3964(R)
Procedure 32420.8.3 RK512 Computer Link 325
20.9 Configuration in RUN 328
20.9.1 Preparation of Modifications toConfiguration 328
16
Table of Contents
20.9.2 Changing the Configuration . . . 330
20.9.3 Loading the Configuration. . . . 330
20.9.4 CiR Synchronization Time. . . . 331
20.9.5 Effects on Program Execution . . 331
20.9.6 Controlling the CiR Procedure. . 332
21 Interrupt Handling 333
21.1 General Remarks 333
21.2 Time-of-Day Interrupts 334
21.2.1 Handling Time-of-Day Interrupts 33521.2.2 Configuring Time-of-Day
Interrupts with STEP 7 33621.2.3 System Functions for Time-of-
Day Interrupts 336
21.3 Time-Delay Interrupts 337
21.3.1 Handling Time-Delay Interrupts. 33821.3.2 Configuring Time-Delay
Interrupts with STEP 7 33921.3.3 System Functions for Time-
Delay Interrupts 339
21.4 Watchdog Interrupts 340
21.4.1 Handling Watchdog Interrupts. . 34021.4.2 Configuring Watchdog Interrupts
with STEP 7 341
21.5 Hardware Interrupts 342
21.5.1 Generating a Hardware Interrupt 34221.5.2 Servicing Hardware Interrupts. . 34321.5.3 Configuring Hardware Interrupts
with STEP 7 344
21.6 DPV1 Interrupts 344
21.7 Multiprocessor Interrupt 346
21.8 Synchronous Cycle Interrupts . . 347
21.8.1 Processing Synchronous CycleInterrupts 347
21.8.2 Isochrone Updating of ProcessImage 348
21.8.3 Programming of SynchronousCycle Interrupts with STEP 7 . . 349
21.9 Handling Interrupts 349
21.9.1 Disabling and Enabling Interrupts 34921.9.2 Delaying and Enabling Delayed
Interrupts 350
21.9.3 Reading Additional InterruptInformation 350
22 Restart Characteristics 353
22.1 General Remarks 353
22.1.1 Operating Modes 35322.1.2 HOLD Mode 35422.1.3 Disabling the Output Modules . . 35422.1.4 Restart Organization Blocks . . . 354
22.2 Power-Up 355
22.2.1 STOP Mode 35522.2.2 Memory Reset 35622.2.3 Retentivity 35622.2.4 Restart Parameterization 356
22.3 Types of Restart 357
22.3.1 START-UPMode 35722.3.2 Cold Restart 35722.3.3 Warm Restart . . 35922.3.4 Hot Restart 359
22.4 Ascertaining a Module Address. . 360
22.5 Parameterizing Modules 362
22.5.1 General Remarks onParameterizing Modules 362
22.5.2 System Functions for ModuleParameterization 363
22.5.3 Blocks for Data Record Transfer . 364
23 Error Handling 367
23.1 Synchronous Errors 367
23.2 Synchronous Error Handling . . . 369
23.2.1 Error Filters 369
23.2.2 Masking Synchronous Errors . . . 37023.2.3 Unmasking Synchronous Errors . 37023.2.4 Reading the Error Register . . . . 37023.2.5 Entering a Substitute Value. . . . 371
23.3 Asynchronous Errors 371
23.4 System Diagnostics 374
23.4.1 Diagnostic Events andDiagnostic Buffer 374
23.4.2 Writing User Entries in theDiagnostic Buffer 374
23.4.3 Evaluating Diagnostic Interrupts . 37523.4.4 Reading the System Status List. . 375
17
Table of Contents
Variable Handling 378
24 Data Types 379
24.1 Elementary Data Types 379
24.1.1 Declaration of ElementaryData Types 379
24.1.2 BOOL, BYTE, WORD,DWORD, CHAR 380
24.1.3 Number Representations . . . . 38124.1.4 Time Representations 383
24.2 Complex Data Types 384
24.2.1 D A T E A N D T I M E 38424.2.2 STRING 38524.2.3 ARRAY 38624.2.4 STRUCT 388
24.3 User-Defined Data Types . . . . 390
24.3.1 Programming UDTsIncrementally 390
24.3.2 Source-File-OrientedProgramming of UDTs 390
25 Indirect Addressing 392
25.1 Pointers 392
25.1.1 Area Pointer 39225.1.2 DBPointer 39225.1.3 ANY Pointer 394
25.2 Types of Indirect Addressingin STL 395
25.2.1 General . . 39525.2.2 Indirect Addresses 39525.2.3 Memory-Indirect Addressing . . 39625.2.4 Register-Indirect Area-Internal
Addressing 39625.2.5 Register-Indirect Area-Crossing
Addressing 39825.2.6 Summary 398
25.3 Working with Address Registers 399
25.3.1 Loading into an Address Register 39925.3.2 Transferring from an Address
Register 39925.3.3 Swap Address Registers 39925.3.4 Adding to the Address Register . 399
25.4 Special Features of IndirectAddressing 401
25.4.1 Using Address Register AR1. . . 40125.4.2 Using Address Register AR2. . . 40325.4.3 Restrictions with Static Local
Data 403
26 Direct Variable Access 405
26.1 Loading the Variable Address . . 405
26.2 Data Storage of Variables . . . . 406
26.2.1 Storage in Global Data Blocks . . 40626.2.2 Storage in Instance Data Blocks . 40726.2.3 Storage in the Temporary Local
Data 407
26.3 Data Storage when TransferringParameters 410
26.3.1 Parameter Storage in Functions . 41026.3.2 Storing Parameters in Function
Blocks 41226.3.3 "Variable" ANY Pointer . . . . . 412
26.4 Brief Description of the MessageFrame Example 414
Structured Control Language (SCL) . .42lj
27 Introduction, LanguageElements 422
27.1 Integration in SIMATIC 422
27.1.1 Installation 42227.1.2 Setting Up a Project 42227.1.3 Editing the SCL Source 42227.1.4 Completing the Symbol Table . . 42327.1.5 Compiling the SCL Program . . . 42427.1.6 Loading SCL Blocks 42427.1.7 Testing SCL Blocks 42427.1.8 Addresses and Data Types . . . . 42427.1.9 Data Type Views 426
27.2 Addressing 426
27.2.1 Absolute Addressing 42627.2.2 Symbolic Addressing 42627.2.3 Indirect Addressing in SCL . . . 427
27.3 Operators 429
27.4 Expressions 429
27.4.1 Arithmetic Expressions 42927.4.2 Comparison Expressions 43127.4.3 Logical Expressions 431
27.5 Value Assignments 432
18
Table of Contents
27.5.1 Assignment for ElementaryData Types 432
27.5.2 Assignment of DT and STRINGVariables 432
27.5.3 Assignment of Strucrures . . . . 43227.5.4 Assigning Fields 432
28 Control Statements 433
28.1 IF Statement 433
28.2 CASE Statement 434
28.3 FOR Statement 434
28.4 WHILE Statement 435
28.5 REPEAT Statement 435
28.6 CONTINUE Statement 436
28.7 EXIT Statement 436
28.8 RETURN Statement 436
28.9 GOTO Statement 436
29 SCL Blocks 438
29.1 SCL Blocks - General 438
29.2 Programming SCL Blocks. . . . 438
29.2.1 Function FC without a FunctionValue 439
29.2.2 Function FC with FunctionValue 439
29.2.3 Function Block FB 43929.2.4 Temporary Local Data 44029.2.5 Static Local Data 44129.2.6 Block Parameters 44129.2.7 Formalparameters 442
29.3 Calling SCL Blocks 442
29.3.1 Function FC without FunctionValue 443
29.3.2 Function FC with FunctionValue 443
29.3.3 Function Block with its OwnData Block 443
29.3.4 Function Block as Local Instance 44429.3.5 Actual Parameters 444
29.4 EN/ENO Mechanism 445
29.4.1 OK Variable 44529.4.2 ENO Output 44529.4.3 EN Input 446
30 SCL Functions 447
30.1 Timer Functions 447
30.2 Counter Functions 448
30.3 Math Functions 449
30.4 Shifting and Rotating 449
30.5 Conversion Functions 450
30.5.1 Implicit Conversion Functions . . 45030.5.2 Explicit Conversion Functions . . 450
30.6 Programming Your OwnFunctions with SCL 452
30.7 Programming Your OwnFunctions with STL 454
30.8 Brief Description of the SCLExamples 454
30.8.1 Conveyor Example 45430.8.2 Message Frame Example 45530.8.3 General Examples 456
31 IEC Functions 457
31.1 Conversion Functions 457
31.2 Comparison Functions 459
31.3 STRING Functions 460
31.4 Date/Time-of-Day Functions . . . 462
31.5 Numerical Functions 463
Appendix 465
32 S5/S7 Converter 466
32.1 General 466
32.2 Preparation 467
32.2.1 Checking Executability on theTarget System (PLC) 467
32.2.2 Checking Program ExecutionCharacteristics 467
32.2.3 Checking the Modules 46932.2.4 Checking the Addresses 469
32.3 Converting 470
32.3.1 Creating Macros 47032.3.2 Preparing the Conversion 47132.3.3 Starting the Converter 47132.3.4 Convertible Functions 472
32.4 Post-Editing 473
19
Table of Contents
32.4.1 Creating the STEP 7 Project. . . 47332.4.2 Unconvertible Functions . . . . 47432.4.3 Address Changes 47432.4.4 Indirect Addressing 47532.4.5 Access to "Excessively Long"
Data Blocks 47732.4.6 Working with Absolute Addresses 47732.4.7 Parameter Initialization 47732.4.8 Special Function Organization
Blocks 47732.4.9 Error Handling 477
33 Block Libraries 480
33.1 Organization Blocks 480
33.2 System Function Blocks 481
33.3 IEC Function Blocks 484
33.4 S5-S7 Converting Blocks . . . . 485
33.5 TI-S7 Converting Blocks . . . . 486
33.6 PID Control Blocks 487
33.7 Communication Blocks 487
33.8 Miscellaneous Blocks 487
34 STL Operation Overview . . . 488
34.1 Basic Functions 488
34.1.1 Binary Logic Operations . . . . 48834.1.2 Memory Functions 48934.1.3 Transfer Functions 48934.1.4 Timer Functions 48934.1.5 Counter Functions 489
34.2 Digital Functions 489
34.2.1 Comparison Functions 48934.2.2 Math Functions 49034.2.3 Arithmetic Functions 49034.2.4 Conversion Functions 49034.2.5 Shift Functions 49034.2.6 Word Logic Operations 490
34.3 Program Flow Control 491
34.3.1 Jump Functions 49134.3.2 Master Control Relay 49134.3.3 Block Functions 491
34.4 Indirect Addressing 491
35 SCL Statement and Function
Overview 492
35.1 Operators 492
35.2 Control Statements 492
35.3 Block Calls 492
35.4 SCL Standard Functions 493
35.4.1 Timer Functions 493
35.4.2 Counter Functions 49335.4.3 Conversion Functions 49335.4.4 Math Functions 49435.4.5 Shift and Rotate 494
Index 495
Abbreviations 503
20