Allen Bradley PLC programming course.
Enroll Today!
Siemens PLC programming course
Register for FREE!
Check out our FANUC Robotics course
Register Today!
Back to tutorials

Constant Value Calculation (Typed and Non-Typed) in SCL

By
Mohammadreza Aghajani
Learning Path
No items found.
Table of Contents

Introduction

Variables are the inseparable elements of PLC programming in every language defined in IEC 61131-3. Control engineers need to use different variables to proceed with their PLC programs. One of the practical variables that are widely used in mathematical operations are constants.

Most of the time, PLC programmers do not make a difference between the typed and non-typed constants, and they use a mixture of them in computation procedures. Unfortunately, doing so will deliver an undesirable result that is incorrect and unexpected.

In this tutorial, the different types of constants (typed and non-typed), the problem of using a mixture of different constant types in mathematical equations, and the two possible solutions that can resolve this issue will be discussed in detail.

Prerequisites

What you will need to follow along with this tutorial are:

Typed and non-typed constant interpretation

Data that have a fixed value and cannot change during the running of a program are called constants. Various program elements can read constants while the program is executing, but constants can't be overwritten. Depending on the type and format of data, standardized notations are used to represent the value of a constant. The typed and non-typed notations are distinguished from one another.

Mixing typed and non-typed constants in mathematical functions is not recommended as you would otherwise encounter undesired implicit conversions, and as a result, inaccurate values are obtained.

By exploring the following PLC programming example, a calculating process with a typed and a non-typed constant is explained.

After creating a new project and configuring an S7-1500 CPU for the PLC, in the TIA Portal software, double-click Add new block item on the left pant to start creating an SCL block.

In the pop-up window, select Function block as the desired block, name it FB_MathsFunctions, choose SCL from the language drop-down menu and press the OK button.

Figure 1.1: Creating an SCL function block

When the created function block opens, left-click the down arrow on the top of the window to open the Block interface. Scroll down to find Temp (Temporary) section and declare the Variable_DINT tag with the double integer data type.

Figure 1.2: Declaring a tag in the block interface

Type the following program code in the SCL editor: #Variable_DINT := INT#1 + 50000; In this mathematical calculation, the non-typed constant 50000 will be added to the typed constant INT#1.

Now, right-click over the SCL function block FB_MathsFunctions, select compile from the pop-up menu and then choose the item Software (only changes).

Figure 1.3: Typing program code and compiling it

The Inspector window appears with the Compile tab opened, indicating the block was successfully compiled, but it has one warning. The non-typed constant 50000 is underlined in yellow in the software to show that the constant value is beyond the data type INT's permitted range.

Figure 1.4: Constant value is outside the permitted range

For the SCL function block to be executed in each PLC cycle scan, the Main OB must call it. To do this, double-click Main OB on the left pane and then drag and drop the function block over Network 1. When the Call options window opens, it has only the Single instance data block option. Keep the name as it is and press the OK button.

Figure 1.5: Calling the function block by the Main OB

Now, the program is ready to be downloaded to the simulator. Left-click the Start simulation icon on the top toolbar to open the Load preview window. Press the Load button.

Figure 1.6: Downloading the program to the simulator - Load preview window

When the Load results window appears, press the Finish button and then left-click the Run button over the S7-PLCSIM Advanced software to bring the program into run mode.

Figure 1.7: Downloading the program to the simulator - Load results window

To see the result, double-click the SCL function block under the Program blocks folder in the Project tree and then press the eye-glasses icon in the SCL editor’s toolbar to make the program go online so you can monitor the block.

Figure 1.8: Bringing the program into online mode

The typed constant's data type determines the data type of the addition. The addition is done in the INT (integer) data type region.

Figure 1.9: Determination of the addition equation data type

In the initial stage, the non-typed constant 50000 is implicitly transformed into the integer data type. But the conversion yields a negative result (-15536).

Figure 1.10: Conversion of the non-typed constant into the integer data type

After that, this value is added to the typed constant (INT#1). The outcome is -15535. Given that the tag to which the addition result is to be written is defined with the data type DINT (double integer), the value -15535 is implicitly transformed to the data type DINT and stored in the tag Variable_DINT. However, the outcome is still negative.

We expected that the non-typed constant 50000 would be added to the typed constant one (INT#1) and deliver the result of 50001 (1 + 50000 = 50001). But it didn't happen. How to solve this problem?

Figure 1.11: Software result vs expected result

There are two ways to fix this issue. The first solution to avoid this undesirable outcome is to type both constants. When you type both constants, the computation operation is determined by the lengthier data type.

To apply this solution, left-click the Go offline button on the top toolbar to bring the program into offline mode. Then, modify your application code into: #Variable_DINT := INT#1 + DINT#50000; in the SCL function block FB_MathsFunctions. In this computation process, the typed constant DINT#50000 and the typed constant INT#1 will be added together.

Figure 1.12: Typing both constants in the program code

To compile the modified program, right-click over the Program blocks folder in the Project tree, select Compile from the pop-up menu and then choose Software (rebuild all).

Figure 1.13: Compiling the modified program - Typing both constants method

To download the modified program to the simulator, right-click over the Program blocks folder, select the Download to device from the pop-up menu and then choose Software (only changes). When the Load preview window opens, press the Load button.

Figure 1.14: Downloading the modified program to the simulator - Typing both constants method

To see the result, left-click the eye-glasses icon in the SCL editor’s toolbar to bring the program into online mode. At first, the constant INT#1 is transformed into the double integer (DINT) data type. Then, the addition of the two constants is performed in the DINT data type region: 1 + 50000 = 50001

Figure 1.15: Monitoring the modified program online - Typing both constants method

The second solution to resolve the mentioned issue is not to type both constants. If you do not type both constants, they are interpreted as the broadest available data type on the current CPU. It indicates the two constants are treated as LINT data types on an S7-1500 series CPU.

To apply this solution, once again, left-click the Go offline icon on the top toolbar and modify the application code in the SCL editor using the non-typed constants as follows: #Variable_DINT := 1 + 50000.

In this computation process, the non-typed constant 50000 and the non-typed constant 1 will be added together.

To compile the modified program, under the Program blocks folder, right-click over the SCL function block FB_MathsFunctions, select Compile from the pop-up menu and then choose Software (only changes).

Figure 1.16: Modifying and compiling the program code - Non-typed both constants method

This time to download the modified program to the simulator, left-click the Program blocks folder in the Project tree and then left-click the Download to device icon on the top toolbar to open the Load preview window. In the pop-up window, press the Load button.

Figure 1.17: Downloading the modified program to the simulator - Non-typed both constants method

To see the result, left-click the eye-glasses icon in the SCL editor’s toolbar to bring the program into the online mode to monitor it. The constant values 1 and 50000 are considered to be of the LINT data type, and the outcome of the addition is once more transformed into the DINT data type.

Figure 1.18: Monitoring the modified program online - Non-typed both constants method

Conclusion

This tutorial discussed constant value calculation in Structured Control Language (SCL). You learned constant variables are divided into two categories: typed and non-typed, and you got familiar with the differences between them.

You learned not to combine typed and non-typed constants in mathematical equations as you would otherwise face undesired implicit conversions, resulting in inaccurate values.

You learned in case of encountering this issue during programming, use two possible options to fix it. The first solution is to type both constants. Whenever you type both constants, the lengthier data type determines how the calculation is performed.

And there is also a second solution to not typing both constants to avoid the undesired and wrong result. When you don't type both constants, the processor interprets them as the broadest possible data type. Consequently, on a Siemens S7-1500 processor, these two constants are interpreted as LINT values.

Back to tutorials

Ready To Learn More?

Level-up your career with unlimited access to practical, in-depth technical courses taught by industry experts. Explore our courses