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.
Now, let’s call the ReusableFB that we created inside our main OB1. See picture 2.
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 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.
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.
Now, go back to the main OB1 and notice how your FB call is now. See picture 7.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
Now, call the ReusableFB from the HigherLevelFB. And choose the Multi-instance option. See picture 18.
When you choose the Multi-instance option, the created DB will be stored inside the static parameters of the calling FB. See picture 19.
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.
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.
Read Next:
- Advanced PLC Training Course
- Industrial Automation Solution
- Safety Door PLC Interlock Logic
- Two-Hand Press PLC Program
- Muting Functions of Safety PLC