Tia Portal – Different Instances of Calling a Function Block

In a previous article, we discussed what is a function block FB, how it works in a PLC program, and how to create and use one. In this article, we will talk about data block instances of different function block types in Siemens Tia Portal and when to use each type.

Contents:

  • What is a function block FB?
  • Different options of data instances.
  • Single Instance.
  • Parameter Instance.
  • Multi-instance.

What is a Function Block?

A function block or an FB is simply a block that contains code logic. You use this FB to achieve specific functionality through the pieces of code written inside.

When calling a function block into your code you will be asked to assign a data block also called data instance to be associated with this FB, to save the values of the FB parameters. Not all parameters inside an FB are saved in the data instance, but we will get to that later.

When calling a function block, you have 3 different options for associating a data block instance with this function call. These different options will depend on where you are calling your FB.

So, in a nutshell. A function block FB is basically a function FC with a dedicated data block DB, this data block is used to store the values of the function block parameters.

Different Options for Data Instances

We have 3 different options for a data instance of a function block, these options are:

  • Single Instance.
  • Parameter Instance.
  • Multi-Instance.

The three different call data instances come from the 3 different call methods:

  • You can call a function block FB inside the main OB1 which will give you the option of:
    • Single instance.
  • You can call the function block FB inside a function FC, which will give you two options
    • Single instance
    • Parameter instance
  • You can call the function block inside another function block, which will give you the three available options for creating a data instance
    • Single instance
    • Parameter instance
    • Multiple instances

Single Data Instance

First, let’s start by creating a function block FB, as we mentioned before, we create a function block by clicking add a new block and choosing the type of block we want. See picture 1.

Create a function block
Picture 1 – Creating a function block FB

Now, let’s call the ReusableFB that we created inside our main OB1. See picture 2.

Calling the FB in the main OB
Picture 2 – Calling the FB inside the main OB1

As you see from the previous picture, when calling an FB inside the main OB1 you will be asked to assign a data instance to be associated with this FB call. In this case, there will be only one option, which is the single instance.

After choosing a single instance option, a data block will be created and associated with the FB call. See picture 3.

The data block associated with the FB
Picture 3 – Single instance created

The created single instance will be used to store the values of some of the FB parameters. Like the inputs, outputs, In Out, and static parameters.

Other parameters of the FB will not be stored, like temp and constants. See pictures 4 and 5.

PLC Data is saved inside the data instance
Picture 4 – Data is saved inside the data instance
Data saved from the FB into the data instance
Picture 5 – Data saved from the FB into the data instance.

Now, let’s create a simple logic inside the FB to help us understand the data instances better. This logic will add a constant value of 15 to a static variable and then move the result to the output. See picture 6.

Create a simple logic in Tia
Picture 6 – Create a simple logic

Now, go back to the main OB1 and notice how your FB call is now. See picture 7.

Update the FB call after each change
Picture 7 – Update the FB call after each change

Any change you make to the logic inside the FB, will result in the need for updating the Function block call, so that the changes you made can be applied.

You can update the block call by right click the FB call and pressing the update block call option or by recompiling your PLC code. See picture 8.

Siemens Tia Portal - Updating the FB call
Picture 8 – Updating the FB call

After updating the block call, the changes you made in the FB code will be applied and employed in the block call. As you see in picture 9.

The FB now expects an input signal of type bool to be provided and the FB will give an output of type int.

PLC Inputs and outputs are now associated with the FB call
Picture 9 – Inputs and outputs are now associated with the FB call

Let’s simulate our code and see how the PLC will behave. See the next animation showing a simple simulation of the PLC logic so far.

Function block instances in PLC programming using Siemens TIA Portal

As you see from the animation, whenever the start signal is TRUE, the function will be executed and the output keeps changing. And once the start signal is no longer available the Output will remain at the last value recorded.

The use of the data instance here is that the values of the static variable and the output variable are saved inside the single instance, so when the start signal comes back again, the function will continue from the last recorded values.

Very Important Note

You should never use the same single instance for two different calls of an FB. See the next animation.

TIA Portal Function Block Instances

As you see from the animation, we have two different FB calls, but both calls are associated with the same single instance, which is why even when the start2 signal was FALSE, the Output2 value was changing with Output1.

As you would expect the change in the data instance of the 1st call will also be affected in the 2nd call because they have the same memory block. See picture 10.

Data Instance in PLC
Picture 10 – Never use the same data instance with different FB calls

If you used the same data instance with different FB calls, then your function block is no longer reusable. Even if the inputs/outputs parameters are different for each different FB call. As you saw from the last video (animation) both calls had the same results even though 2nd call doesn’t even have an enable input signal.

Another very important note

We said before that if you call your FB from a higher-level FC, you will have two options for the associated data instance; these options are the single instance and the parameter instance. See picture 11.

Single instance with an FB called from an FC
Picture 11 – Using a single instance with an FB called from an FC

If that happened, and you will call an FB inside an FC, You should never use a single instance for your FBs. To know why that is. See picture 12

Tia Portal FB inside an FC
Picture 12 – Calling the higher-level FC more than once

As you see from picture 12, when you call the higher-level FC in your logic more than one time, you will not be asked to assign a data block, because FCs don’t need one.

But you know that there is a called FB inside the FC, this FB has a single instance associated with it. So now the 3 FC calls have the same data instance for the FB call. So, your function FC is no longer reusable.

What to do?  The best option for when you need to call an FB inside an FC is to use the parameter instance.

Parameter Instance

As we said before, if you called an FB inside an FC you shouldn’t choose the single instance but rather the parameter instance is better for your reusability purposes.

A parameter instance will save the data instance of the FB called into the In Out area of the FC block interface. Allowing you to enter a new data instance for each FC call. See pictures 13 and 14.

PLC Assign parameter instance when calling an FB inside an FC
Picture 13 – Assign parameter instance when calling an FB inside an FC
FC call in PLC
Picture 14 – Each FC call will need a new data instance

As you see from the previous picture, whenever you call the FC inside your program, it will ask for a data instance for the reusable FB inside the FC.

But, using this way you will have to create the data instance yourself. See picture 15.

Create a new data instance in Siemens PLC
Picture 15 – Create a new data instance

To create a new data instance, you do the same as creating an FC or an FB, but this time you choose the DB option. And make sure that you select the DB type to be the same as the FB called.

Now, your FC can be reused as many times as you want, you just have to create an instance for each call. See picture 16.

PLC Assign the DB to the FC call
Picture 16 – Assign the DB to the FC call

Multi-instance Database

A multi-instance DB simply means that the DB of the called FB will be stored inside the DB of the higher-level calling FB.  This option is only available if you call an FB from another FB.

Let’s create another FB to use it as a higher-level FB.

After creating this HigherLevelFB, call it from the main OB1, and off-course the only option of calling will be a single instance as shown before. See picture17.

Understanding Function Block Instances
Picture 17 – Call the HigherLevelFB from the main OB1

Now, call the ReusableFB from the HigherLevelFB. And choose the Multi-instance option. See picture 18.

multi-instance DB in PLC
Picture 18 – Assign multi-instance DB

When you choose the Multi-instance option, the created DB will be stored inside the static parameters of the calling FB. See picture 19.

Example of calling two instances of the same Function Block in Siemens TIA Portal
Picture 19 – Instances are saved inside static parameters

You can call the ReusableFB many times, each time you call it the multi-instance will be stored inside the static parameter. See picture 20.

Instance data block
Picture 20 – Calling the ReusableFB many times

As you can see the data instance of the lower-level FB will be saved inside the data instance of the higher-level FB. It is best for better program structure and easy-to-read logic.

Downloads:

Conclusion

Creating function blocks inside your code will require associating a data block with each FB call you to make in your logic. This data block or also called data instance has different options depending on the type of block that is calling your FB.

Be careful when choosing the type of data instance, as some options may not be suitable for your case as we showed before. And sometimes this can lead to problems in your logic and your function can no longer be reusable.

Using multi-instance can help better organize your program structure, as all called FBs will store their DBs inside the main calling FB.

If you liked this article, then please subscribe to our YouTube Channel for PLC and SCADA video tutorials.

You can also follow us on Facebook and Twitter to receive daily updates.

Don't Miss Our Updates
Be the first to get exclusive content straight to your email.
We promise not to spam you. You can unsubscribe at any time.
Invalid email address

Leave a Comment