• 沒有找到結果。

Using blocks to structure your program

Programming concepts 5

5.1 Guidelines for program design

5.1.2 Using blocks to structure your program

By designing FBs and FCs to perform generic tasks, you create modular code blocks. You then structure your program by having other code blocks call these reusable modules. The calling block passes device-specific parameters to the called block.

A Calling block

B Called (or interrupting) block

① Program execution

② Operation that calls another block

③ Program execution

④ Block end (returns to calling block)

When a code block calls another code block, the CPU executes the program code in the called block. After execution of the called block is complete, the CPU resumes the execution of the calling block.

长沙工控帮教育科技有限公司整理

Programming concepts

5.1 Guidelines for program design

Processing continues with execution of the instruction that follows after the block call.

You can nest the block calls for a more modular structure.

① Start of cycle

② Nesting depth

2% )% )&

)% )&

)%

'%

)&

'%

'% '%

Creating reusable code blocks

Use the "Add new block" dialog under "Program blocks" in the Project navigator to create OBs, FBs, FCs, and global DBs.

When you create code block, you select the programming language for the block. You do not select a language for a DB because it only stores data.

5.1.2.1 Organization block (OB)

Organization blocks provide structure for your program. They serve as the interface between the operating system and the user program. OBs are event driven. An event, such as a diagnostic interrupt or a time interval, will cause the CPU to execute an OB. Some OBs have predefined start events and behavior.

The cyclic OB contains your main program. You can include more than one cyclic OB in your user program. During RUN mode, the cyclic OBs execute at the lowest priority level and can be interrupted by all other types of program processing. (The startup OB does not interrupt the cyclic OB because the CPU executes the startup OB before going to RUN mode.)

长沙工控帮教育科技有限公司整理

Programming concepts 5.1 Guidelines for program design

After finishing the processing of the cyclic OBs, the CPU immediately executes the cyclic OB again. This cyclic processing is the "normal" type of processing used for programmable logic controllers. For many applications, the entire user program is located in a single cyclic OB.

You can create other OBs to perform specific functions, such as startup tasks, for handling interrupts and errors, or for executing specific program code at specific time intervals. These OBs interrupt the execution of the program cycle OBs.

Use the "Add new block" dialog to create new OBs in your user program.

Depending on their respective priority levels, one OB can interrupt another OB. Interrupt handling is always event-driven. When such an event occurs, the CPU interrupts the scanning of the user program and calls the OB that was configured to handle that event.

After finishing the execution of the interrupting OB, the CPU resumes the execution of the user program at the point of interruption.

The CPU determines the order for handling interrupt events by a priority assigned to each OB.

Each event has a particular servicing priority. Several interrupt events can be combined into priority classes. For more information, refer to the PLC concepts chapter section on

execution of the user program (Page 35).

Creating an additional OB within a class of OB

You can create multiple OBs for your use program, even for the cyclic and startup OB classes. Use the "Add new block" dialog to create a OB. Enter the name for your OB and provide an OB number greater than 200.

If you create multiple cyclic OBs for your user program, the CPU executes each cyclic OB in numerical sequence, starting with the main cycle OB (default; OB 1). For example: after first cyclic OB (OB1) finishes, the CPU executes the second cyclic OB (such as OB 2 or OB 200).

长沙工控帮教育科技有限公司整理

Programming concepts

5.1 Guidelines for program design

Configuring the operation of an OB

You can modify the operational parameters for an OB. For example, you can configure the time parameter for a time-delay OB or for a cyclic OB.

5.1.2.2 Function (FC)

A function (FC) is a fast-executing code block that typically performs a specific operation on a set of input values. The FC stores the results of this operation in memory locations.

Use FCs to perform the following tasks:

● To perform standard and reusable operations, such as for mathematical calculations.

● To perform technological functions, such as for individual controls using bit logic operations.

An FC can also be called several times at different points in a program. This reuse simplifies the programming of frequently recurring tasks.

Unlike a function block (FB), an FC does not have an associated instance data block (DB).

The FC uses the local data stack for the temporary data used to calculate the operation. The temporary data is not saved. To store data permanently, assign the output value to a global memory location, such as M memory or to a global DB.

5.1.2.3 Function block (FB)

A function block (FB) is a code block whose calls can be programmed via block parameters.

FBs have variable memory that is located in a data block (DB), or "instance" DB. The instance DB provides a block of memory that is associated with that instance (or call) of the FB and stores data after the FB finishes. You can associate different instance DBs with different calls of the FB. The instance DBs allow you to use one generic FB to control multiple devices. You structure your program by having one code block make a call to an FB and an instance DB. The CPU then executes the program code in that FB, and stores the block parameters and the static local data in the instance DB. When the execution of the FB finishes, the CPU returns to the code block that called the FB. The instance DB retains the values for that instance of the FB.

Reusable code blocks with associated memory

You typically use an FB to control the operation for tasks or devices that do not finish their operation within one scan cycle. To store the operating parameters so that they can be quickly accessed from one scan to the next, each FB in your user program has one or more instance DBs. When you call an FB, you also open an instance DB that stores the values of the block parameters and the static local data for that call or "instance" of the FB. The instance DB stores these values after the FB finishes.

长沙工控帮教育科技有限公司整理

Programming concepts 5.1 Guidelines for program design

By designing the FB for generic control tasks, you can reuse the FB for multiple devices by selecting different instance DBs for different calls of the FB.

An FB stores the input (IN), output (OUT), and in/out (IN_OUT) parameters in an instance DB.

Assigning initial values

If the input, output, or in/out parameters of a function block (FB) are not assigned with values, the values stored in the instance data block (DB) will be used. In some cases, you must assign parameters.

You can assign initial values to the parameters in the FB interface. These values are transferred to the associated instance DB. If you do not assign parameters, the values currently stored in the instance DB will be used.

Using a single FB with multiple instance DBs

The following figure shows an OB that calls one FB three times, using a different data block for each call. This structure allows one generic FB to control several similar devices, such as motors, by assigning a different instance data block for each call for the different devices.

Each instance DB stores the data (such as speed, ramp-up time, and total operating time) for an individual device. In this example, FB 22 controls three separate devices, with DB 201 storing the operational data for the first device, DB 202 storing the operational data for the second device, and DB 203 storing the operational data for the third device.

'%

'%

'%

)%'%

)%'%

)%'%

2%

)%

5.1.2.4 Data block (DB)

You create data blocks (DB) in your user program to store data for the code blocks. All of the program blocks in the user program can access the data in a global DB, but an instance DB stores data for a specific function block (FB).

Your user program can store data in the specialized memory areas of the CPU, such as for the inputs (I), outputs (Q), and bit memory (M). In addition, you can use a data block (DB) for fast access to data stored within the program itself. You can define a DB as being read-only.

长沙工控帮教育科技有限公司整理

Programming concepts

5.1 Guidelines for program design

The data stored in a DB is not deleted when the data block is closed or the execution of the associated code block comes to an end. There are two types of DBs:

● A global DB stores data for the code blocks in your program. Any OB, FB, or FC can access the data in a global DB.

● An instance DB stores the data for a specific FB. The structure of the data in an instance DB reflects the parameters (Input, Output, and InOut) and the static data for the FB. (The Temp memory for the FB is not stored in the instance DB.)

Note

Although the instance DB reflects the data for a specific FB, any code block can access the data in an instance DB.