The text of a manual · part 10 of 11
MELSEC FX Family Programmable Logic Controllers Beginners Manual, pages 91 to 100
Pages 91 to 100 of 106
The original document is the PDF (969 KB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.
For the Mitsubishi FX2NC-32MT
Page 91
More Advanced Programming
5.3.2
Compare Instructions
Comparisons within logic operations In the CMP instruction described in the last section the result of the comparison is stored in three bit devices. Often, however, you only want to execute an output instruction or a logic operation on the basis of the result of a comparison, and you generally won’t want to have to use three bit devices for this. You can achieve this with the “load compare” instructions and the AND and OR bitwise logic comparisons. Comparison at the beginning of a logic operation Ladder Diagram
Instruction List
0
0 LD>=
D40
D50
���
���
���
>= D40 D50 ���
���
���
쐃 Compare condition 쐇 First compare value 쐋 Second compare value If the condition evaluates true the signal state after the comparison is set to “1”. A signal state of “0” shows that the comparison evaluated as false. The following comparisons are possible: –
Compare for "equals":
=
(value 1 = value 2)
The output of the instruction is only set to "1" if the values of both devices are identical. –
Compare for "greater than":
>
(value 1 > value 2)
The output of the instruction is only set to "1" if the first value is greater than the second value. –
Compare for "less than":
>
(value 1 < value 2)
The output of the instruction is only set to "1" if the first value is smaller than the second value. –
Compare for "not equal":
<>
(value 1 <> value 2)
The output of the instruction is only set to "1" if the two values are not equal. –
Compare for "less than or equal to":
(value 1 울 value 2)
<=
The output of the instruction is only set to "1" if the first value is less than or equal to the second value. –
Compare for "greater than or equal to": >=
(value 1 욷 value 2)
The output of the instruction is only set to "1" if the first value is greater than or equal to the second value. To compare 32-bit data prefix a D (for double word) to the compare condition: Ladder Diagram
Instruction List
0
0 LDD>
D> D10 D250
D10
D250
This "D" specifies 32-bit data The example above checks whether the contents of data registers D10 and D11 are greater than the contents of registers D250 and D251.
FX Beginners Manual
5 – 17
Page 92
Compare Instructions
More Advanced Programming
More examples: Ladder Diagram
0
Instruction List
>= C0 D20
0 LD>= 5 OUT
M12
C0 M12
D20
Relay M12 is set to "1" when the value of counter C0 is equal to or greater than the contents of D20. Ladder Diagram
Instruction List
T52 0
> D10 K-2500
Y003
0 LD> 5 AND 6 OUT
D10 T52 Y003
K-2500
Output Y003 is switched on when the contents of D10 is greater than -2,500 and timer T52 has finished running. Ladder Diagram
0
Instruction List
D< C200 K182547
M53
0 LDD< 9 OR 10 OUT
C200 M110 M53
K182547
M110
Relay M53 is set to "1" if either the value of counter C200 is less than 182,547 or relay M110 is set to "1". Compare as a logical AND operation Ladder Diagram
0
Instruction List
<= D40 D50 ���
���
���
0 LD 1 AND<= D40 ���
���
... D50 ���
쐃 Compare condition 쐇 First comparison value 쐋 Second comparison value An AND comparison can be used just like a normal AND instruction (see chapter 3). The comparison options are the same as those described above for a comparison at the beginning of an operation. Here too, you can also compare 32-bit values with an AND operation: Ladder Diagram
0
Instruction List
D= D30 D400
0 ANDD= D30
D400
This "D" specifies 32-bit data
5 – 18
MITSUBISHI ELECTRIC
Page 93
More Advanced Programming
Compare Instructions
Compare as a logical OR operation Ladder Diagram
Instruction List
0
0 LD 1 OR>=
... C20
K200
���
���
���
>= C20 K200 ���
���
���
쐃 Compare condition 쐇 First comparison value 쐋 Second comparison value An OR comparison can be used just like a normal AND instruction (see chapter 3). The comparison options are the same as those described above for a comparison at the beginning of an operation. Here too, you can also compare 32-bit values with an OR operation: Ladder Diagram
Instruction List
0
0 LD 1 ORD=
C200
... D10
D= C200 D10 This "D" specifies 32-bit data
FX Beginners Manual
5 – 19
Page 94
Math Instructions
5.4
More Advanced Programming
Math Instructions All the controllers of the MELSEC FX family can perform all four basic arithmetical operations and can add, subtract, multiply and divide integer numbers (i.e. non-floating-point numbers). These instructions are described in this section. The controller base units of the FX2N, FX2NC and FX3U series can also process floating-point numbers. This is done with special instructions that are documented in detail in the Programming Manual of the MELSEC FX series. After every addition or subtraction you should always program instructions to check the states of the special relays listed below to see whether the result is 0 or has exceeded the permitted value range. 쎲 M8020 This special relay is set to "1" if the result of an addition or subtraction is 0. 쎲 M8021 Special relay M8021 is set to "1" if the result of an addition or subtraction is smaller than -32,767 (16-bit operations) or -2,147,483,648 (32-bit operations). 쎲 M8022 Special relay M8022 is set to "1" if the result of an addition or subtraction is greater than +32,767 (16-bit operations) or +2,147,483,647 (32-bit operations). These special relays can be used as enable flags for continuing with additional math operations. In the following example the result of the subtraction operation in D2 is used as a divisor. Since dividing by 0 is impossible and causes an error the division is only executed if the divisor is not 0. Ladder Diagram
Instruction List
M8000 0
SUB D0 D1 D2 M8020
8
5 – 20
DIV D3 D2 D5
0 1 8 9
LD SUB LDI DIV
M8000 D0 M8020 D3
D1
D2
D2
D5
MITSUBISHI ELECTRIC
Page 95
More Advanced Programming
5.4.1
Math Instructions
Addition The ADD instruction calculates the sum of two 16-bit or 32-bit values and writes the result to another device. Ladder Diagram
Instruction List
0
0 ADD
ADD D0 D1 D2 ���
���
���
D0
D1
D2
���
���
���
쐃 First source device or constant 쐇 Second source device or constant 쐋 Device in which the result of the addition is stored The example above adds the contents of D0 and D1 and writes the result to D2. Examples Add 1,000 to the contents of data register D100: ADD K1000 D100 D102
1000
+
D 100 53
D 102 1053
The signs of the values are taken into account by the ADD instruction: ADD D10 D11 D12
D 10 5
+
D 11 -8
D 12 -3
You can also add 32-bit values by prefixing a "D" to the ADD instruction (DADD): DADD D0 D2 D4
D1 D0 65238
+
D3 D2 27643
D5 D4 92881
If you want you can also write the result to one of the source devices. However, if you do this remember that the result will then change in every program cycle if the ADD instruction is executed cyclically! D0 18
ADD D0 K25 D0
+
D0 43
25
The ADD instruction can also be executed in pulse-triggered mode. Then it is only executed when the signal state of the input condition changes from "0" to "1". To use this mode just add a "P" suffix to the ADD instructions (ADDP, DADDP). In the following example the constant value 27 is only added to the contents of D47 once, in the program cycle in which the signal state of relay M47 changes from "0" to "1": Ladder Diagram
Instruction List
M47 0
FX Beginners Manual
ADDP D47 K27 D51
0 LD 1 ADDP
M47 D47
K27
D51
5 – 21
Page 96
Math Instructions
5.4.2
More Advanced Programming
Subtraction The SUB instruction calculates the difference between two numerical values (contents of 16-bit or 32-bit devices or constants). The result of the subtraction is written to a third device. Ladder Diagram
Instruction List
0
0 SUB
SUB D0 D1 D2 ���
���
���
D0
D1
D2
���
���
���
쐃 Minuend (the subtrahend is subtracted from this value) 쐇 Subtrahend (this value is subtracted from the minuend) 쐋 Difference (result of the subtraction) In the example above the contents of D1 is subtracted from the contents of D0 and the difference is written to D2. Examples Subtract 100 from the contents of data register D11 and write the result to D101: SUB D100 K100 D101
D 100 247
–
D 101 147
100
The signs of the values are taken into account by the SUB instruction: SUB D10 D11 D12
D 10 5
–
D 11 -8
D 12 13
You can also subtract 32-bit values by prefixing a “D” to the SUB instruction (DSUB): DSUB D0 D2 D4
D1 D0 65238
–
D5 D4 37595
D3 D2 27643
If you want you can also write the result to one of the source devices. However, if you do this remember that the result will then change in every program cycle if the SUB instruction is executed cyclically! SUB D0 K25 D0
D0 197
–
D0 172
25
The SUB instruction can also be executed in pulse-triggered mode. Then it is only executed when the signal state of the input condition changes from "0" to "1". To use this mode just add a "P" suffix to the SUB instructions (SUBP, DSUBP). In the following example the contents of D394 is only subtracted from contents of D50 once, in the program cycle in which the signal state of relay M50 changes from "0" to "1": Ladder Diagram
Instruction List
M50 0
5 – 22
SUBP D50 D394 D51
0 LD 1 SUBP
M50 D50
D394
D51
MITSUBISHI ELECTRIC
Page 97
More Advanced Programming
5.4.3
Math Instructions
Multiplication The FX controllers’ MUL instruction multiplies two 16-bit or 32-bit values and writes the result to a third device. Ladder Diagram
0
Instruction List 0 MUL
MUL D0 D1 D2 ���
���
���
D0
D1
D2
���
���
���
쐃 Multiplicand 쐇 Multiplier 쐋 Device in which the result of the addition is stored The example above adds the contents of D0 and D1 and writes the result to D2. NOTE
When you multiply two 16-bit values the result can quite easily exceed the range that can be displayed with 16 bits. Because of this the product of multiplications is always written to two consecutive 16-bit devices (i.e. a 32-bit double word). When you multiply two 32-bit values the product is written to four consecutive 16-bit devices (64 bits, two double words). Always take the size of these device ranges into account when you are programming and take care not to create range overlaps by using the devices in the ranges to which the products are written! Examples Multiply the contents of D0 and D1 and store the product in D3 and D2: D0 1805
MUL D0 D1 D2 x
D1 481
D3 D2 868205
The signs of the values are taken into account by the MUL instruction. In this example the value in D10 is multiplied by the constant value -5: D 10 8
MUL D10 K-5 D20 x
D 21 D 20 -40
-5
You can also multiply 32-bit values by prefixing a "D" to the MUL instruction (DMUL): DMUL D0 D2 D4
D1 D0 65238 x
D3 D2 27643
D7
D6 D5 1803374034
D4
The MUL instruction can also be executed in pulse-triggered mode by adding a "P" suffix to the MUL instructions (MULP, DMULP). The following multiplication is only executed when input X24 switches from "0" to "1": Ladder Diagram
Instruction List
X24 0
FX Beginners Manual
MULP D25 D300 D26
0 LD 1 MULP
X24 D25
D300
D26
5 – 23
Page 98
Math Instructions
5.4.4
More Advanced Programming
Division The MELSEC FX family’s DIV instruction divides one number by another (contents of two 16-bit or 32-bit devices or constants). This is an integer operation and cannot process floating-point values. The result is always an integer and the remainder is stored separately. Ladder Diagram
0
Instruction List 0 DIV
DIV D0 D1 D2 ���
���
���
D0
D1
D2
���
���
���
쐃 Dividend 쐇 Divisor 쐋 Quotient (result of the division, dividend ÷ divisor = quotient) NOTES
The divisor should never be 0. Division by 0 is not possible and will generate an error. When two 16-bit values are divided the quotient is written to one 16-bit device and the remainder is written to the next device. This means that the result of a division always requires two consecutive 16-bit devices (= 32 bits). When you divide two 32-bit values the quotient is written to two 16-bit devices and the remainder is written to the next two 16-bit devices. This means that four consecutive 16-bit devices are always required for the result of a 32-bit division. Always take the size of these device ranges into account when you are programming and take care not to create range overlaps by using the devices in the ranges to which the results of the calculations are written! Examples Divide the contents of D0 by the contents of D1 and write the result to D2 and D3: DIV D0 D1 D2
D0 40
쐦
D1 6
D2 6
Quotient (6 x 6 = 36)
D3 4
Remainder (40 - 36 = 4)
The signs of the values are taken into account by the DIV instruction. In this example the counter value of C0 is divided by the value in D10: DIV C0 D10 D200
5 – 24
C0 36
쐦
D 10 -5
D 200 -7
Quotient
D 201 1
Remainder
MITSUBISHI ELECTRIC
Page 99
More Advanced Programming
Math Instructions
Division with 32-bit values: DDIV D0 D2 D4
D0 D1 65238
D3 쐦
D2 27643
D5
D4
Quotient
2 D7
D6 9952
Remainder
Adding a “P” suffix to the DIV instructions executes the instructions in pulse-triggered mode (DIV -> DIVP, DDIVPL -> DMULP). In the following example the counter value of C12 is only divided by 4 in the program cycle in which input X30 is switched on: Ladder Diagram
Instruction List
X30 0
5.4.5
DIVP C12 K4 D12
0 LD 1 DIVP
X30 C12
K4
D12
Combining math instructions In real life one calculation is seldom all you want to perform. The FX controllers allow you to combine math instructions to solve more complex calculations. Depending on the nature of the calculation you may have to use additional devices to store intermediate results. The following example shows how you could calculate the sum of the values in data registers D101, D102 and D103 and then multiply the result by the factor 4: Ladder Diagram
Instruction List
M101 0
ADD D101 D102 D200 M8022 ADD D200 D103 D200 M8021 M8022 MUL D200 K4 D104
0 1 8 9 10 17 18 19 20
LD M101 ADD D101 MPS ANI M8022 ADD D200 MPP ANI M8021 ANI M8022 MUL D200
D102
D200
D103
D200
K4
D104
–
First the contents of D101 and D102 are added and the result is stored in D200.
–
If (and only if) the sum of D101 and D102 does not exceed the permitted range it is then added to the value in D103.
–
If the sum of D101 through D103 does not exceed the permitted range it is multiplied by the factor 4 and the result is written to D104 and D105.
FX Beginners Manual
5 – 25
Page 100
Math Instructions
5 – 26
More Advanced Programming
MITSUBISHI ELECTRIC