Fundamentals of PLC - Constants and Variables

 The essence of program design lies in the manipulation of various types of data. Data possesses unique attributes, and based on these attributes, it can be categorized into different data types. In previous articles, we introduced the basic data types of PLC, understanding concepts such as "bit," "integer," "word," "double word," and so on. Data types belong to abstract concepts; in programming, we don't directly manipulate data types but rather operate on instances of data.


Instances are the specific manifestations of data types, including two types: 'constants' and 'variables.' Today, this article will discuss the constants and variables used in STEP7 programming.

Constant refers to a quantity whose value cannot be altered during the program's runtime. Constants are stored in read-only memory, and any attempt to modify their values in the code will result in an error.

Constants can have different data types, which can be "byte", "word", or "double word". For example, B#16#10 represents a constant stored in the form of "byte" (occupying one byte) with a value of hexadecimal "10"; W#16#10 represents a constant stored in the form of "word" (occupying two bytes) with a value of hexadecimal "10"; DW#16#10 represents a constant stored in the form of "double word" (occupying four bytes) with a value of hexadecimal "10".

From the examples above, it can be seen that although the stored values of the constants are all "0x10", the amount of memory resources they occupy differs due to the different declared data types. Understanding this principle, in future program design, it is possible to adopt constants of different data types according to specific needs, in order to conserve memory resources and improve the efficiency of the program's operation.

Constants can also represent binary data, indicated by the prefix "2#". For example, "2#1010" represents the binary value "1010". Binary constants are particularly useful when performing bitwise "AND" operations.

Constants can be declared as integer types, which in the SAMITIC STEP7 platform are indicated by the prefix "L#". For example, "L#10" represents the decimal value of "10". The "L#" prefix can also be used to represent negative numbers, such as "L#-5" which represents the decimal value of "-5". Constants declared with "L#" occupy four bytes, totaling 32 bits.

The S7-1200/1500 series of PLCs support a wide range of data types, with integer data types being further subdivided into signed short integers (SINT), unsigned short integers (USINT), signed integers (INT), unsigned integers (UINT), signed double integers (DINT), unsigned double integers (UDINT), signed long integers (LINT), and unsigned long integers (ULINT). When declaring integer constants in S7-1200/1500, it is sufficient to add "#" after the data type, such as "SINT#10" or "INT#567".

Constants can be declared as real numbers (floating-point numbers) in the SAMITIC STEP7 platform without the need for a special prefix. It is sufficient to include a decimal point when writing the number, such as "10.0". The editor will automatically represent this value using scientific notation, as shown in the figure below:





In the Portal (TIA Portal or Both) platform, real number constants can indeed be represented using "REAL#".

In STEP7, constants can also represent time, indicated by "S5T#". S5 format time constants occupy 2 bytes and follow the format S5T#D_H_M_S_MS, where "D" represents days, "H" represents hours, "M" represents minutes, "S" represents seconds, and "MS" represents milliseconds. For example, S5T#1M5S represents one minute and five seconds. Time constants are typically used in conjunction with timers.

Moving on to variables















Variables, referred to as "variable" in English, are quantities whose values can be modified during the execution of a program. Variables can also be defined with different data types. Unlike constants, the storage area for variables needs to be explicitly defined when declaring them.

The storage areas in Siemens S7 series PLCs include: Input Process Image Area (I), Output Process Image Area (Q), Bit Storage Area (M), Timer Area (T), and Counter Area (C). For example, M0.1 represents the first bit of the zeroth byte in the Bit Storage Area operated in a bitwise manner. MB0 represents the zeroth byte of the Bit Storage Area, MW0 represents the zeroth word, and MD0 represents the zeroth doubleword.

This way of representing variables using the number of the storage area is called the absolute addressing of variables. Absolute addressing does not intuitively represent the actual physical signal meaning, making the program less readable. To improve program readability, the S7 series PLCs also support using symbolic names to represent variables. For example, M0.1 could be given a symbolic name "Switch_Open," which makes it clear that the variable is related to the open state of a switch.

The concepts of constants and variables have been introduced here. More detailed information about variables will be covered in subsequent articles.