laitimes

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal

author:A sprouted potato

一、OB(Organization block) 组织块

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal

OB (Organization block) is an interface between the operating system and the user's program to execute a specific program.

  • At CPU boot
  • Round-robin program processing
  • When the loop or delay time arrives
  • When an external condition is triggered
  • When a malfunction, an error occurs

In the TIA Portal, the most commonly used OB block is the Main function (OB1), which calls FB and FC through the Main function, and these FB and FC can continue to nest down to call FB and FC. Except for the main program and the startup OB, the execution of other OBs is triggered according to various interrupt conditions (error, time, hardware, etc.), and the OB cannot be called by FB and FC.

Nested depth: Refers to the depth at which a block of program code, such as a function (FC) or function block (FB), can be called from an OB.

1. Start from the program loop OB or start the OB to call the program code blocks such as FC and FB, and the nesting depth is 16 layers;

2. Start from other interrupt OBs to call program code blocks such as FC and FB, and the nesting depth is 6 layers;

OB events

Each organization block has its own priority, and the arrival of a high-priority OB interrupts the execution of a low-priority OB during a run.

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal

注:V4.0 版本 CPU 以前,延时中断 OB + 循环中断 OB 数量 <=4

V4.0 CPU support is now available

V4.1 CPU support is now available

V4.2 CPU support is now available

For instructions on how to use each OB, please refer to the help file in the TIA Portal.

二、DB(Data block) 数据块

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal

definition

Data blocks (DB) can be divided into global data blocks and background data blocks. The data block is used to store user data and program intermediate variables, wherein the background data block is generated by the background function block FB, and the internal data structure is determined by the interface definition area of the FB;

Addressing method

In the TIA Portal, it can be addressed by absolute address or by symbol; To use absolute address addressing, you need to cancel the Optimized Block Access option (Optimized Block Access is used by default).

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal

Symbolic address addressing

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal

Absolute address: Addressing is composed of the address DB number + address offset, for example: DB100.dbx0.0

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal

三、FC(Function) 和 FB(Function Block)

definition

FC (Function): is a block of code without a memory area, commonly used to perform specific operations on a set of input values, such as standard and reusable operations (such as mathematical calculations) that can be performed using FC, or process functions (such as independent control using bitwise logic operations).

FB(Function Block)功能块:带背景数据块的FC--功能。

Block interface

FB and FC blocks have a block interface area, which can be used to define block interfaces. The types of interfaces that can be defined in the block interface area: Input, Output, InOut, Static, Temp, and Constant Note: FC or FB can be parameterized or unparameterized

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal
Introduction and application of OB, DB, FC, FB in Siemens TIA Portal

For FB or FC blocks with parameters, there are 2 concepts, tangible parameters and real parameters, as shown in the figure below.

Introduction and application of OB, DB, FC, FB in Siemens TIA Portal
  • Parameters: Input, Output, and InOut parameters defined in the block interface area; When a block is called, it appears as a pin on the block. Variables of type Input and InOut appear to the left of the block; Variables of type Output appear to the right of the block.
  • Arguments: The actual variables that are filled in for the parameters when calling a block with parameters.

Fourth, the difference and application of FC block and FB block

What is the difference between FB and FC, in what scenarios is FC used and in what scenarios is FB applied?

First of all, from the literal meaning of FC (Function) - function, FB (Function Block) - function block; There is a "block" between these two, which is what we often call the background data block (Background DB), FB uses the background data block as the storage area, and FC does not have an independent storage area, and uses the global DB or M area; This is the main difference between FB and FC;

Secondly, the FB local variables have STAT and TEMP, and FC does not have STAT because it does not have its own storage area, and the TEMP itself cannot set the initial value.

FB Block Pros:

1. Easy portability, for the controlled objects with different parameters of the same control logic, as long as different background DBs are used, the same FB block can be convenient

2. Multiple backgrounds, reduce repetitive work and improve efficiency

3. When calling multiple times, the parameters are easy to modify

4. There is an independent storage area

FC Block Advantages:

1. Small and flexible, easier to understand for non-multiple call programs

2. It does not occupy additional storage resources

FC Applications:

1. Since FC does not have a background storage area, FC is often used as a sub-program entry to keep the program introduction easy to understand and easy to copy; For example, all the programs in process section 1 are placed in FC1, and then the corresponding programs are called by FC1; The process section 1 programs are all placed in FC2, and then the corresponding programs are called by FC2;

2. It is used to perform functions that do not contain logical operations or common part of the program, such as: processing IO mapping, alarm program processing

3. The logical function block from the input to the output is "direct", that is, the function that does not need to use temporary variables in the middle, and only transmits parameters between the input and output; For example, convert 1 16-bit word into 16 bits output, or input input data in equal proportions.

FB Applications:

FB has a wide range of applications and can complete all the functions implemented by FC, but in general, FB is often used as a control function block of a certain class of the same object, and then FB is called in FC; For example, FB can be applied to the motor control block, and the start, stop, alarm, timing and other functions of the motor can be realized in the FB, and each different background DB can be regarded as an object; It can greatly improve the design efficiency of the program;

Frequently asked questions:

  • Why is the program confused after defining FB, FC blocks, multiple calls?

For program blocks that are called multiple times, it is recommended to replace the call with a different background DB; The FC instance needs to ensure that the storage address used is not duplicated, that is, the address in the block is not duplicated for each call.

  • Why is the FB or FC containing a timer or counter OK for a single call, but the timer or counter is chaotic when it is called multiple times?

For FB or FC that has been called multiple times, such as S7 timer or counter, you need to define a TIMER or Counter in the IN interface, and assign a different timer or counter number to each FB or FC call. If it is an IEC timer or counter, you need to define Block_DB in interface, and assign a different DB block to the IEC timer or counter every time FB or FC is called.