You are on page 1of 14

Application on Control Technology

Programming Example in Ladder Logic


Using FBs, FCs, and DBs

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

Note

The application examples are not binding and do not claim to be


complete regarding the circuits shown, equipping and any eventuality.
The application examples do not represent customer-specific solutions.
They are only intended to provide support for typical applications. You
are responsible in ensuring that the described products are correctly
used. These application examples do not relieve you of the responsibility
in safely and professionally using, installing, operating and servicing
equipment. When using these application examples, you recognize that
Siemens cannot be made liable for any damage/claims beyond the
liability clause described. We reserve the right to make changes to these
application examples at any time without prior notice. If there are any
deviations between the recommendations provided in these application
examples and other Siemens publications - e.g. Catalogs - then the
contents of the other documents have priority.

Warranty, liability and support


We do not accept any liability for the information contained in this
document.
Any claims against us - based on whatever legal reason - resulting from the
use of the examples, information, programs, engineering and performance
data etc., described in this application example shall be excluded. Such an
exclusion shall not apply in the case of mandatory liability, e.g. under the
German Product Liability Act (Produkthaftungsgesetz), in case of intent,
gross negligence, or injury of life, body or health, guarantee for the quality
of a product, fraudulent concealment of a deficiency or breach of a
condition which goes to the root of the contract (wesentliche
Vertragspflichten). However, claims arising from a breach of a condition
which goes to the root of the contract shall be limited to the foreseeable
damage which is intrinsic to the contract, unless caused by intent or gross
negligence or based on mandatory liability for injury of life, body or health
The above provisions does not imply a change in the burden of proof to
your detriment.
Copyright 2006 Siemens A&D. It is not permissible to transfer or
copy these application examples or excerpts of them without first
having prior authorization from Siemens A&D in writing.
For questions about this document please use the following e-mail address:
mailto:csweb@ad.siemens.de

V1.0

2006/08/01

2/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Table of Contents
Table of Contents ......................................................................................................... 3
Description ................................................................................................... 4
Objectives .................................................................................................... 4
Summary of the Sample Program................................................................ 4
1
1.1

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

1.2

1.3
2
2.1

2.2
2.3

Sample Program for Using FBs, FCs, and DBs ........................................... 8


OB1................................................................................................................... 8
Network 1: Set-Reset Flip Flop to Set Automatic Mode ............................... 8
Network 2: Switching on the Gasoline Engine ............................................. 8
Network 3: Switching on the Diesel Engine ................................................. 9
Network 4: Controlling the Fan for the Gasoline Engine .............................. 9
Network 5: Controlling the Fan for the Diesel Engine ................................ 10
FB1 ................................................................................................................. 10
Network 1: Set-Reset Flip Flop to Switch on an Engine ............................ 11
Network 2: Monitoring the Engine Speed................................................... 12
FC1 ................................................................................................................. 12
Network 1: Controlling the Engine Fan ...................................................... 12
Reference Information.................................................................................. 13
Description: Function Block (FB) .................................................................... 13
Application.................................................................................................. 13
Function Blocks and Instance Data Blocks ................................................ 13
Description: Function (FC).............................................................................. 13
Application.................................................................................................. 13
Description: Data Block (DB) .......................................................................... 14
Shared Data Blocks in the User Program .................................................. 14

Reference to Automation and Drives Service & Support


This entry is from the internet application portal of Automation and Drives
Service & Support. The documentation has the entry ID 23330722. Click
the link below to directly display the download page of this document.
http://support.automation.siemens.com/WW/view/en/23330722
All entries referenced in this document are designated by their entry ID and
addressed via the above path.

V1.0

2006/08/01

3/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Using FBs, FCs, and DBs


Description
This program controls the operation of a gasoline engine and a diesel
engine. The actual engine operation is controlled in a reusable Engine
Function Block (FB) (Networks 2 and 3), with associated Data Blocks (DB)
1 and 2, for the gasoline engine and diesel engine, respectively. An engine
fan operation is controlled in a reusable Function (FC) (Networks 4 and 5),
where DB information is not needed because FCs cannot access DB
information.
Objectives

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

The sample program provides an example of the following tasks:

Using Function Blocks (FB) to accomplish specific, repetitive tasks that


require memory for storage of data used and generated by the task.

Using Data Blocks (DB) for storage of data used and generated by the
FB task.

Using Functions (FC) to accomplish specific, repetitive tasks that do not


require memory for storage of data used and generated by the task.

Summary of the Sample Program


OB1 contains 2 different calls for FB1, one FB1 call that uses DB1
(gasoline engine) parameters and one FB1 call that uses DB2 (diesel
engine) parameters. There are also two calls for FC1, one for the gasoline
engine fan and one for the diesel engine fan. With the controller in RUN,
the input to the required FB box must be powered for the required gasoline
or diesel engine FB to be executed.
Table 1-1 contains a summary of the gasoline engine program operation:

OB1 and FB1 interconnections which impact the necessary steps to


bring the motor to a run condition

Two views of DB1: one containing the gasoline engine initial parameter
values and one containing the gasoline engine initial and actual
parameter values

DB2 diesel engine parameter values (The diesel engine operates


exactly the same as the gasoline engine.)

OB1 and FC1 interconnections which impact the necessary steps to


bring the motor fan to a run condition.

Table 1-2 displays the Symbol Table containing all of the global symbols
used in the program.

V1.0

2006/08/01

4/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs


Table 1-1 Summary of Gasoline Engine LAD Programming

OB1, Network 1
FB1 (Engine), Network 1

OB1, Network 2

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

FB1 (Engine), Network 2

The outputs of the Gasoline Engine instance of FB1 are set as follows:
#Engine_On output is set to 1.
#Preset_Speed_Reached is set to 1 when the following parameters are set as specified:
"Automatic Mode" is set to 0.
#Switch_On is set to 1.
#Switch_Off is set to 0.
#Failure is set to 1.
#Actual_Speed is set to a value greater than the #Preset_Speed.
DB1 (Gasoline) - Gasoline Engine Initial Values

DB1 (Gasoline) - Gasoline Engine New Actual_Speed Value

DB2 (Diesel) - Diesel Engine Initial Values

V1.0

2006/08/01

5/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

OB1, Network 4

FC1, Network 1

The #Fan_On output of the Gasoline Engine Fan FC1 is set to 1 when the following
parameters are set as specified:

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

#Engine_On is set to 1.
#Timer_Function is set to 1.
When #Engine_On is set to 0, the #Fan_On output of the Gasoline Engine Fan FC1 is set to
0 after 4 seconds have elapsed.
Table 1-2: FB_FC_DB Program Symbol Table

V1.0

2006/08/01

6/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Global symbols appear in quotation marks ("name") and are valid in the
entire program. "Block-specific" symbols are represented with a hash
symbol before the name (#name) and are only valid within the FB or FC.

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

Note

V1.0

2006/08/01

7/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Sample Program for Using FBs, FCs, and DBs

1.1

OB1

Network 1: Set-Reset Flip Flop to Set Automatic Mode


Figure 1-1: OB1 - Network 1

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

In this example, the Set-Reset is used to control "Automatic Mode" (Q 4.2).


If the signal state is "1" at the "Automatic On" (I 0.5 ) input and "0" at
"Manual On" (I 0.6), memory bit "Automatic Mode" (Q 4.2) is set (signal
state is 1). Otherwise, if the signal state at the "Automatic On" (I 0.5 )
input is "0" and at "Manual On" (I 0.6) is "1", memory bit "Automatic Mode"
(Q 4.2) is reset (signal state is 0). If both signal states are "0", nothing is
changed. If both signal states change to "1" at exactly the same time, the
reset instruction dominates because of the order; "Automatic Mode" (Q 4.2)
is reset (signal state is 0).
Network 2: Switching on the Gasoline Engine
Figure 1-2: OB1 - Network 2

This is the call for the function block FB1 ("Engine") with the data for a
gasoline engine (data block "Gasoline" DB1).

V1.0

2006/08/01

8/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Network 3: Switching on the Diesel Engine

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

Figure 1-3: OB1 - Network 3

This is the call for the function block FB1 ("Engine") with the data for a
diesel engine (data block "Diesel" DB2).
Network 4: Controlling the Fan for the Gasoline Engine
Figure 1-4: OB1 - Network 4

This is the call for the function FC1 to control the gasoline engine fan.
When the gasoline engine is turned off, FC1 causes a 4-second time delay
before the gasoline engine fan is turned off.

V1.0

2006/08/01

9/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Network 5: Controlling the Fan for the Diesel Engine


Figure 1-5: OB1 - Network 5

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

This is the call for the function FC1 to control the diesel engine fan. When
the diesel engine is turned off, FC1 causes a 4-second time delay before
the diesel engine fan is turned off.

1.2

FB1
This function block contains a memory function for controlling the engine
and a comparator for monitoring the speed.
We want to use the same function block to control two different engines.
We therefore want to assign all the "engine-specific" signals as function
block parameters. The signals that are scanned are the inputs of the
function block ("in" declaration); the signals that are modified are the
outputs ("out" declaration). One of the local variables, #Preset_Speed, is
engine-specific, but because it is a fixed value, it can be stored as static
data in the engine data ("stat" declaration). This is known as a "static local
variable".
Figure 1-6: Function Block Variable Detail View

When the function block is processed (block call) in the organization block
OB1, we will assign the function block parameters the specific signals for
each engine.

V1.0

2006/08/01

10/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

The operating mode (here "automatic mode") is not engine-specific; it is


valid for the entire control program. This signal is therefore not one of the
function block parameters; it is scanned directly into the function block.
Network 1: Set-Reset Flip Flop to Switch on an Engine
Figure 1-7: FB1 Network 1

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

In this example, the Set-Reset is used to switch on an Engine.


If the signal state is "1" at the #Switch_On input and "0" at
"Automatic_Mode", the output variable #Engine_On is set (signal state is
1).
Note

The signal "Automatic_Mode" is negated; this is represented by a


normally closed contact (a normally closed contact is "closed" when the
relevant signal state is "0").
Otherwise, if the signal state at the #Switch_Off input is "1" or at the
#Failure input is "0", memory bit #Engine_On is reset (signal state is 0).

Note

#Failure is a "zero-active" signal, which usually has the signal state "1" (if
there is no error) and signal state "0" when an error occurs. The required
function is obtained if we program a normally closed contact for the signal
#Failure.
If both the set and reset signal states are "0", nothing is changed. If both
signal states change to "1" at exactly the same time, the reset instruction
dominates because of the order; #Engine_On is reset (signal state is 0).

V1.0

2006/08/01

11/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Network 2: Monitoring the Engine Speed


Figure 1-8: FB1 Network 2

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

A comparator monitors the engine speed. It checks whether the actual


speed is greater than or equal to the preset speed. If this is the case, the
comparator sets the variable #Preset_Speed_Reached to "1".

1.3

FC1
You can program a function (FC) when the algorithm (the control function)
does not have to store any of its own data. In contrast to a function block
(FB), a function has no instance data block.
This example shows a simple function: a fan starts when the engine is
switched on. When the engine is switched off again, the fan continues to
run for another 4 seconds and then stops.

Network 1: Controlling the Engine Fan


Figure 1-9: FC1 Network 1

The box "S_OFFDT" is taken from the Program Elements catalog and
labeled #Timer_Function. The variable #Timer_Function is started with the
input parameter #Engine_On. This variable #Timer_Function with the data
type TIMER stands for a timer function which is assigned a timer address
(for example, T1) later on when it is called in OB1.
The timer function provides a switch-off delay, with a timer delay of 4
seconds. You can also assign the time delay as an input parameter with the
data type S5TIME so that a different follow-on time can be configured for
the fan in each engine.

V1.0

2006/08/01

12/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

Reference Information

2.1

Description: Function Block (FB)


FBs belong to the blocks that you program yourself. A function block is a
block with memory." It is assigned a data block as its memory (instance
data block). The parameters that are transferred to the FB and the static
variables are saved in the instance DB. Temporary variables are saved in
the local data stack.
Data saved in the instance DB are not lost when execution of the FB is
complete. Data saved in the local data stack are, however, lost when
execution of the FB is completed.

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

Application
An FB contains a program that is always executed when the FB is called by
a different logic block. Function blocks make it much easier to program
frequently occurring, complex functions.
Function Blocks and Instance Data Blocks
An instance data block is assigned to every function block call that transfers
parameters.

2.2

Description: Function (FC)


FCs belong to the blocks that you program yourself. An FC is a logic block
without memory." Temporary variables belonging to the FC are saved in
the local data stack. This data is then lost when the FC has been executed.
To save data permanently, functions can also use shared data blocks.
Since an FC does not have any memory of its own, you must always
specify actual parameters for it. You cannot assign initial values for the
local data of an FC.

Application
An FC contains a program section that is always executed when the FC is
called by a different logic block. You can use functions for the following
purposes:

V1.0

To return a function value to the calling block (example: math functions)

To execute a technological function (example: single control function


with a bit logic operation).

2006/08/01

13/14

Using FBs, FCs, and DBs

Using FBs, FCs, and, DBs

2.3

Description: Data Block (DB)


In contrast to logic blocks (FBs or FCs), Data Blocks (DBs) do not contain
STEP 7 instructions. They are used to store user data, in other words, data
blocks contain variable data with which the user program works. Shared
data blocks are used to store user data that can be accessed by all other
blocks. Instance data blocks are assigned to a specific FB for its storage
memory.
The size of DBs can vary. Refer to the description of your CPU for the
maximum possible size.
You can structure shared data blocks in any way to suit your particular
requirements.

Shared Data Blocks in the User Program

Copyright Siemens AG 2006 All rights reserved


23330722_Using_FB_FC_DB.doc

If a logic block (FC, FB, or OB) is called, it can occupy space in the local
data area (L stack) temporarily. In addition to this local data area, a logic
block can open a memory area in the form of a DB. In contrast to the data
in the local data area, the data in a DB are not deleted when the DB is
closed (in other words, after the corresponding logic block has been
executed).
Each FB, FC, or OB can read the data from a shared DB or write data to a
shared DB. This data remains in the DB after the DB is exited.
An FB can open and use a shared DB and an instance DB at the same
time. Figure 2-1 shows the different methods of access to data blocks.
Figure 2-1 Shared and Instance DBs

V1.0

2006/08/01

14/14

You might also like