Different Blocks structures are available when programming a PLC, these blocks include Functions FCs, function blocks FBs, and data blocks DBs. These blocks are very handy tools that you can use to better design your PLC logic and make your code more readable and easy to follow and debug
In previous articles, we discussed the FCs and FBs. In this article, we will discuss the Data Block DBs, more specifically the Global Data Block.
Contents:
- What is a data block DB?
- Types of data blocks.
- What is a global data block?
- Creating a global data block?
- Working with Global data blocks.
- Example simulation.
What is a Data Block?
A data block DB is a memory area that is used to save the values of the parameters that are written during the execution of the PLC program.
Opposite to the code block, the data block DB contains only variable declarations. It doesn’t have any networks or instructions like an FC or an FB has. The structure of the DB is defined by how many variables you have declared inside the data block.
Types of Data Blocks in PLC
There are two types of data blocks:
- Global Data blocks
- Instance data blocks
- ARRAY data blocks
Global Data Blocks
As the name suggests, the global data block is globally declared for the whole PLC logic. It is not assigned to a specific code block. You can access the values of a global data block from any code block anywhere in your PLC logic. A global data block contains only static tags.
The structure of the global data block can be freely defined. In the declaration table for data blocks, you declare the data elements that are to be contained in the global data block.
Instance data Blocks
The instance data block is assigned directly to a function block FB, whether this function block is internally defined in the PLC like Timers and Counters or user-defined Function blocks FBs.
The structure of an instance data block cannot be freely defined but is instead determined by the interface of the function block. The instance data block contains exactly those block parameters and tags that are declared in the Function block interface.
However, you can define instance-specific values in the instance data block; for example, start values for the declared tags.
ARRAY Data Blocks
Available only for S7-1500 CPUs, ARRAY data blocks are global data blocks that consist of an ARRAY. This ARRAY can be based on any data type.
For example, an ARRAY of a PLC data type (UDT) is possible. The DB contains no other elements besides the ARRAY. Because of their flat structure, ARRAY data blocks facilitate access to the ARRAY elements and their transfer to called blocks.
The “Move operations” section of the “Instructions” task card offers options for addressing ARRAY DBs.
In this article, we will take about the global data block, and we will discuss the other two types in separate articles.
What is Global Data Block?
Data blocks are used to store PLC program data. That means they contain variable data that is used by the user program. Global data blocks store data that can be used by all other blocks.
The maximum size of data blocks varies depending on the CPU. You can define the structure of global data blocks any way you, please.
You also have the option of using PLC data types (UDT) as a template for creating global data blocks.
Every function block FB, function FC, or organization block OB can read the data from a global data block or can itself write data to a global data block. This data remains in the data block even after the data block is exited. See picture 1.
Picture 1 – Accessing global data block
As you can see from the previous picture, a global data block can be accessed from any code block inside the PLC program, whereas the instance data block can only be accessed by the associated function block.
Creating a Global Data Block
You create a global data block the same way you create a function FC or a function block FB. From the add a new block to your project tree. See picture 2.
Picture 2 – Creating a global data block
Let’s declare some variables inside the Global data block.
You do that by clicking the add new under name section writing the variable name that you want and then choosing the variable data type. See picture 3.
Picture 3 – Variables declaration in a global data block
Working with a Global Data Block
- Declaring a tag.
We already showed how to declare a tag/variable in picture 3.
- Defining a start value
The start value of a tag is a value defined by you, which the tag assumes after a CPU startup. The value must match the data type of the tag and should not exceed the range of the data type. See picture 4.
The tag takes the defined value at startup, provided it was not declared as retentive.
Picture 4 – Defining the start value of your tags
So, if I set the Tank1Level start value to any value other than zero, this value will be applied the next time the PLC is restarted. See picture 5.
Picture 5 – Defining a start value for your variables
- Retaining of variables in global data blocks
To prevent data loss in the event of power failure, you can mark the data as retentive. This data is stored in a retentive memory area.
The options for setting the retain depend on the type of data block and the type of block access that is set. See picture 6.
Picture 6 -. Retain option in global data blocks
As you see in picture 6, the Tank2Level variable is set to be a retained value, which means even if the PLC stopped or there was a power failure, the Tank2Level will have the same data stored when the PLC is on again. It will not be reset to the start value.
- Accessibility to/from HMI
In a global data block, you can define if a variable can be visible from the HMI tag tables or not. You can also define if this variable can be read or written from the HMI. See picture 7.
Picture 7 – Accessibility from HMI
The default setting for any declared variable in a global data block is that it can be accessed, read, and writable from HMI. If you want to disable this feature for a certain variable, you have to uncheck the accessibility option for that variable.
Example Simulation
So far we created a global data block and declared some variables inside.
Now we will try to run a simulation of the program and see if we can better understand what a global data block is.
Two PLC simulations are provided below.
Testing the Start Values of a Variable
Check the following animation explaining the start value of a variable inside a global data block.
Animation 1
Animation 1 Explanation:
- The start values of the tank level parameters are zero, you can see in the video they are being changed by the simulation screen.
- When the PLC is restarted, powered off then powered on again, you see that the values will be reset to the start values which are zero.
- After that, the start values were changed to 500, 32654, and -356 respectively, and when the PLC is restarted the values were changed to the new start values.
- Notice that when we changed the start values, we had to download our logic again to the PLC; you need to do that each time you make a change to your logic.
Testing the Retain Option of Variables in Global DB
Check the following animation explaining the retain option of a variable inside a global data block.
Animation 2
Animation 2 Explanation:
- First, you will notice that the Retain property of Tank2Level is now active.
- You see in the video the values of the 3 tanks are being changed.
- When the PLC is stopped and then started again, the Tank1Level and Tank3Level are reset back to the start value which is 0, but Tank2Level retained its value of -22938
Conclusion
A global data block can be accessed from anywhere and by any block that exists in the PLC program. You can declare as many variables as you want inside a global database.
The best practice technique is to create separate data blocks for the different sections of your logic, to make it very easy to follow your logic. For example a separate data block for all the variables that need to be read or written by an HMI.
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now