1. The Relationship Between DWord, Word, Byte, and Bit
1. Bit (Bit)
1.1 Definition: A bit is the most basic data unit in PLC, representing a binary digit that can be either 0 or 1.
1.2 Size: 1 bit = 1 bit.
2. Byte (Byte)
2.1 Definition: A byte consists of 8 consecutive bits and is the smallest data storage unit in PLC.
2.2 Size: 1 byte = 8 bits.
2.3 Usage: Bytes are commonly used to store characters, small integers, and other data, such as ASCII character encoding.
3. Word (Word)
3.1 Definition: A word consists of 2 consecutive bytes and is a commonly used data unit in PLC.
3.2 Size: 1 word = 2 bytes = 16 bits.
3.3 Usage: Words are typically used to store integers, addresses, and other data. For example, in Siemens S7 series PLCs, a word can store a 16-bit integer.
4. DWord (DWord)
4.1 Definition: A DWord consists of 2 consecutive words and is a larger data unit in PLC.
4.2 Size: 1 DWord = 2 words = 4 bytes = 32 bits.
4.3 Usage: DWords are often used to store larger integers, floating-point numbers, or addresses. For instance, in Siemens S7 series PLCs, a DWord can store a 32-bit integer or a floating-point number.
5. Their Relationships
5.1 From largest to smallest: DWord > Word > Byte > Bit.
5.2 Specific relationships:
5.3 1 DWord = 2 words = 4 bytes = 32 bits.
5.4 1 word = 2 bytes = 16 bits.
5.5 1 byte = 8 bits.
6. Storage and Addressing
In PLC, data is typically stored in memory by bytes. For example:
6.1 Bit addressing: A specific bit in a byte can be accessed, such as I0.0 representing the 0th bit in input byte I0.
6.2 Byte addressing: A byte can be accessed directly, such as IB0 representing the 0th byte of the input byte.
6.3 Word addressing: Access to two consecutive bytes, such as IW0 representing the 0th word of the input byte.
6.4 DWord addressing: Access to four consecutive bytes, such as ID0 representing the 0th DWord of the input byte.
7. Example
7.1 Assume there is a storage unit with the following content:
7.1.1 DWord: 1100 1100 1100 1100 1100 1100 1100 1100 (32 bits)
7.1.2 Word 1: 1100 1100 1100 1100 (high 16 bits)
7.1.3 Word 2: 1100 1100 1100 1100 (low 16 bits)
7.1.4 Byte 1: 1100 1100 (high 8 bits)
7.1.5 Byte 2: 1100 1100 (next high 8 bits)
7.1.6 Byte 3: 1100 1100 (next low 8 bits)
7.1.7 Byte 4: 1100 1100 (low 8 bits)
7.1.8 Bit: Each bit in every byte, such as 1 or 0.
7.2 In PLC programming, taking DW1 as an example, it usually represents a “DWord” type variable.
7.2.1 Data structure of DW1
7.2.1.1 Number of bytes: DW1 represents a DWord, occupying 4 bytes.
7.2.1.2 Number of words: A DWord consists of 2 words.
7.2.1.3 Number of bits: A DWord contains 32 bits.
7.2.1.4 Representation of DW1
(1) Word representation: DW1 can be represented as 2 consecutive words MW0 (low word) and MW2 (high word).
Thus, DW1 = MW0 + MW2
(2) Byte representation: DW1 can be represented as 2 consecutive words, where the low word MW0 can be represented as MB0, MB1, thus MW0 = MB0 + MB1; where the high word MW2 can be represented as MB2, MB3, thus MW2 = MB2 + MB3.
Thus DW1 = MW0 + MW2,
thus DW1 = MB0 + MB1 + MB2 + MB3
(3) Bit representation: DW1 can be represented as 4 consecutive bytes MB0 to MB3, where MB0 is the low byte, containing 8 bits, represented as MB0.0 to MB0.7; MB1 is the next low byte, containing 8 bits, represented as MB1.0 to MB1.7; MB2 is the next high byte, containing 8 bits, represented as MB2.0 to MB2.7; MB3 is the high byte, containing 8 bits, represented as MB3.0 to MB3.7.
7.3 Assuming the value of DW1 is 0x12345678 (hexadecimal), its storage method is as follows:
7.3.1 Byte representation:
MB0 = 0x12, MB1 = 0x34,
MB2 = 0x56, MB3 = 0x78.
7.3.2 Word representation:
MW0 = 0x1234
MW1 = 0x5678
7.3.4 Bit representation:
DW1.0 = the least significant bit of 0x12
DW1.1 = the next least significant bit of 0x12
…
DW1.31 = the most significant bit of 0x78
Through this method, DW1 can be used to store larger integers or floating-point numbers, and supports various bit operations.