Many users have questions about data types during the study of PLCs. This article will provide an interpretation of the basic data types in PLCs.
The manual explains data types as follows:
“Data types are used to specify the size of data elements and how to interpret the data. Each instruction parameter supports at least one data type, while some parameters support multiple data types. By hovering the cursor over the instruction parameter field, you can see the data types supported by the given parameter.”
According to the manual’s explanation, in programming, you need to assign a data type to the data used in the program, ensuring it meets the data size (length) requirements so that the program can perform calculations. Therefore, you must understand what type of data you are using and its length, which allows you to accurately perform logical operations, mathematical calculations, and data transmission in the program. Additionally, you should check which data types can be used with the instructions you are using, as some data type conversions may be necessary based on the instructions.
1. What are the basic data types?
Taking Siemens TIA S7-1200 as an example, the basic data types of 1200 include: binary numbers, integers, floating-point numbers, timer data, time and date, strings, etc. Below, we will mainly interpret the binary numbers, integers, and floating-point numbers that are easily confused:
1. Binary Numbers (BOOL, BYTE, WORD, DWORD, LWORD)
(1) BOOL: Boolean, represents a 1-bit value, which means either 0 or 1.
(2) BYTE: A byte is a bit string occupying 8 bits, for example, 2#0 to 2#1111_1111, corresponding to hexadecimal 16#00—16#FF. For example, IB2, MB10, DB1.DBB4
(3) WORD: A word is a bit string occupying 16 bits, for example, 2#0 to 2#1111 1111 1111 1111, corresponding to hexadecimal 16#0—16#FFFF. For example, MW10, DB2.DBW1.
(4) DWORD: A double word is a bit string occupying 32 bits, for example, 2#0 to 2#1111 1111 1111 1111 1111 1111 1111 1111, corresponding to hexadecimal 16#0—16#FFFFFFFF. For example, MD10, DB2.DBD1.
2. Integers (SINT, USINT, INT, UINT, DINT, UDINT, LINT, ULINT), where S represents short, U represents unsigned, and D represents double.
(1) SINT and USINT
These two are both short integers occupying 8 bits, for example, MB0, DB1.DBB0. However, their value ranges are different.
SINT: Signed short integer, value range -128—127, 2#0—2#0111 1111, with the highest bit as the sign bit.
USINT: Unsigned short integer, value range 0—255, 2#0—2#1111 1111
(2) INT and UINT
These two are both integers occupying 16 bits, for example, MW0, DB1.DBW0. However, their value ranges are different.
INT: Signed integer, value range -32768—32767, 2#0—2#0111 1111 1111 1111, with the highest bit as the sign bit.
UINT: Unsigned integer, value range 0—65535, 2#0—2#1111 1111 1111 1111
(3) DINT and UDINT
These two are both double integers occupying 32 bits, for example, MD0, DB1.DBD0. However, their value ranges are different.
DINT: Signed integer, value range -2,147,483,648—2,147,483,647, 2#0—2#0111 1111 1111 1111 1111 1111 1111 1111, with the highest bit as the sign bit.
UDINT: Unsigned integer, value range 0—4,294,967,295, 2#0—2#1111 1111 1111 1111 1111 1111 1111 1111
3. Floating-point Numbers
Real (or floating-point) numbers are represented as 32-bit single precision (Real) or 64-bit double precision (LReal). The precision of single precision floating-point numbers is up to 6 significant digits, while the precision of double precision floating-point numbers is up to 15 significant digits.
When entering floating-point constants, you can specify up to 6 digits (Real) or 15 digits (LReal) to maintain precision.
In summary: The above data types are the most commonly used data types in programming. Through the introduction above, you should have a certain understanding of them. Although different data types may occupy the same number of bytes, their data is actually different. For example, INT and WORD are both 16 bits, but they contain different data content. In programming, you must pay attention to this distinction. Generally speaking, WORD is often used for logical calculations, while INT is commonly used for mathematical calculations. In TIA S7-1200 using SCL programming, the distinction is quite strict.
2. How to Define Data Types
In programming, we generally need to define the data types first. For example, defining data types in a data block. Below is an example of defining data types in a DB block, showing how different data type addresses vary.
Create a data block as follows and include different data types:
From the above image, you cannot see the offset. This is because the DB block in TIA defaults to “optimized block access,” which only shows the symbolic address without the offset. You can remove the “optimized block access” option in the block properties.
Now let’s look at the addresses of different data types:
What can everyone find from the above image?
(1) BOOL type, although it only has one bit, it still occupies one byte.
(2) SINT, USINT, and BYTE all occupy 1 byte, but their initial value formats are different. Similarly, WORD and INT both occupy 2 bytes, so in data type classification, WORD is classified as a binary number, generally used for logical operations, while INT is used for mathematical operations.
(3) REAL occupies 4 bytes, and when performing mathematical calculations, special attention should be paid. For example, in the article I previously wrote about conversion instructions, the square root instruction used REAL type data for calculations, so when selecting data, I used DINT data (as shown in the figure below). Not paying attention can easily lead to calculation errors.