A Comprehensive Guide to Constants for Siemens PLC Programming - TIA Portal
Understanding constants and their usage in PLC programming are vital for those working with S7 controllers. This comprehensive tutorial will equip you with the knowledge and skills to use constants in your PLC programs per your needs.
You should learn about types of constants to enhance program readability and code understanding. For instance: typed and non-typed constants, Boolean-type constants, and symbolic constants. Also, you will discover the significance of constant declaration and its permitted data types to become a master in this field.
What you will need to follow along with this tutorial:
- The TIA Portal software should be installed on your computer. While version 16 is used in this tutorial, other versions of the TIA Portal are also compatible
- Understanding Data Blocks (DBs).
- Understanding SCL block creation.
During program runtime, constants assume unchangeable values, representing data that remains fixed. When the program is executed, constants can be accessed by different program elements. But, they retain their unmodifiable nature. The value of a constant is denoted using predefined notations determined by the data type and format. There is a recognized contrast between typed notation and non-typed notation.
Typed and Non-typed Constants
When using the non-typed notation, you just input the constant value without specifying its associated data type. The assignment of a data type to a non-typed constant occurs only when it is involved in its first logical or arithmetic operation. In Figure 1.1, you can observe illustrative examples that depict the utilization of the non-typed notation.
In the typed notation, you include the data type alongside the constant value for explicit specification. In Figure 1.2, you can observe an illustrative example that depicts the utilization of the typed notation.
Boolean-type Constants in FBD and LAD Programming
Consider CPUs of the S7-1500 series with firmware version 1.8 or higher and the S7-1200 series with firmware version 4.0 or higher. These are the only CPUs that allow the usage of Boolean constants as inputs for instructions in FBD and LAD.
But what about the earlier firmware version? Only internal function blocks (FBs) within the instructions allow the use of Boolean-type constants. The identification of these instructions is based on the occurrence of the "Call options" dialog. This dialog box opens when these instructions are inserted into a network. It is necessary to exclude using Boolean constants as inputs for instructions that are not part of the mentioned category.
The usage of Boolean constants is not allowed in S7-300 and 400 PLCs.
You have the opportunity to assign symbolic names to constants. It grants you to reference constant values by a chosen name in the program. Adhering to this approach improves the program's readability and ease of maintenance. Its importance becomes clear when constant values need changes. It ensures efficient code understanding and seamless updates.
When constructing a symbolic constant, it is composed of the following elements:
- Data type: The presence of a data type is a constant feature for symbolic constants. Non-typed notation is not permitted for the declaration of symbolic constants.
- Constant value: The constant value for symbolic constants can be chosen from the entire range of values allowed by the specified data type. For this purpose, you should be familiar with different data types in Siemens PLC programming.
From a point of view, you can consider two types of constants:
- Global constants: These constants are universally applicable across all CPU sections.
- Local constants: These constants are confined to a specific block, with no impact outside of it.
Global constants and local constants differ significantly, presenting the following distinctions between them:
- Validity scope: The validity of global constants extends throughout the entire CPU. Also, their names are uniquely identified within the CPU. Local constants possess validity solely within the block of their declaration. Also, their names are unique exclusively within that block.
- Valid character options: In the names of both global and local constants, digits, letters, and special characters are permitted and considered valid.
- Place of declaration: Within the PLC tag table, global constants are defined exclusively within the designated "User constants" tab. The definition of local constants takes place in the block interface.
- Visual depiction: The display of global constants includes the usage of quotation marks to represent them. For instance: "My_Constant2." Local constants representation involves using a number sign as a prefix. For example: #My_Constant1.
Imagine you have a local and global constant with identical symbolic names. Once you use this doubly specified name as the default value of a tag, the conflict can emerge. Within this situation, the local constant is employed automatically as the default choice.
The Challenge of Downloading Constant Declarations
When programs are downloaded to S7-300 and 400 CPUs, the constant declarations, both local and global, are not included in the transfer. When you transfer a program from a device, the declarations of constants may cease to be available for reference or use. What if you have defined and used constant values within your program, such as assigning a constant value to a variable? The actual constant declarations will not be available within the PLC after the download. The constant values will still be present and can be utilized within the program. But, the original constant declarations will not be accessible in the PLC's memory.
The Permitted data types assigned to constants
There are no limitations on the data types that can be used for constants, including both basic and derived types:
- Numbers represented in binary form
- Bit strings
- Numbers represented in floating-point format
- Data and time
- Character strings
All general rules governing the conversion of types, both explicit and implicit, are applicable in this context.
STEP 7 consistently opts for the data type which offers the highest achievable precision in all cases. What will happen if it is not possible to explicitly define the data type of a constant within an expression? The CPU defaults to using the data type with the highest precision available. Consider the expression presented in Figure 6.1. It showcases the combination of an integer tag with a non-typed constant in the floating-point format. In the S7-300 and 400 series, the right-hand side of the assignment is evaluated using the Real format for computation.
In the S7-1200 and 1500 series, calculations are carried out using the LReal data type to achieve the most possible precision. Therefore, the assignment to a Real tag in this context would be considered invalid or may trigger the issuance of a warning.
It is advisable to employ the typed notation to provide an exact definition of the data type associated with a constant. To understand it better, look at the expression presented in Figure 6.3.
Instances where constants are employed
Use in assignments, instructions, and expressions: In the context of instructions or assignments, constants can be employed as viable substitutes for tags. In the realm of SCL, you have the added capability of utilizing constants within expressions. Nevertheless, since constants cannot be explicitly written, their usage is limited to input values. Figure 7.1 provides visual representations of examples that highlight the potential uses of constants.
Use as a default value: When assigning a default value to a tag, constants can be used for this purpose. To achieve this, enter either the specific value or the symbolic name of the constant. Where? Into the designated "Default value" column provided in the block interface. One way to ensure compatibility is that the constant's data type must match the tag's data type. Or the constant's data type should be convertible to the tag's data type through implicit conversion, as confirmed by the IEC check.
Use as maximum String length: When determining the maximum length of a String, you can utilize local or global constants with the following data types: INT, SINT, LINT, DINT, ULINT, UDINT, and UINT. Figure 7.3 presents an example that demonstrates the utilization of a constant as the maximum length for a STRING.
Use as the limit for an Array: When determining the Array limit, you can utilize local or global constants with the following data types: INT, SINT, LINT, DINT, ULINT, UDINT, and UINT. Figure 7.4 shows an example that demonstrates the usage of a constant as the limit for an Array.
In conclusion, you learned about the basics of constants. You got familiar with two predefined notations for the value of a constant: typed and non-typed. You understood the challenges of implementing Boolean-type constants in S7-PLCs with different firmware. You figured out what elements comprise symbolic constants and why you should use these constants. You grasped definitions of local and global constants and the main differences between these two types. And finally, you became acquainted with some instances where constants are being employed.