Compare Instructions

5.3 Compare Instructions

Checking the status of bit devices like inputs and relays can be achieved with basic logic instructions because these devices can only have two states, “0” and “1”. However, you will also often want to check the contents of word devices before doing something – for example switching on a cooling fan when a specified setpoint temperature is exceeded. The controllers of the MELSEC FX family provide a number of different ways to compare data.

5.3.1 The CMP instruction

CMP compares two numerical values, which can be constants or the contents of data regis- ters. You can also compare the current values of timers and counters. Depending on the result of the comparison (greater than, less than or equal) one of three bit devices is set.

Ladder Diagram Instruction List

0 LD

0 CMP D0 K100 M0

1 CMP

D0 K100 M0

쐃 Input condition 쐇 First value to be compared 쐋 Second value to be compared 쐏 First of three consecutive relays or outputs, which are set (signal status “1”) depending on

the result of the comparison:

1. Device 1: ON if Value 1 > Value 2

2. Device 2: ON if Value 1 = Value 2

3. Device 3: ON if Value 1 < Value 2 In this example the CMP instruction controls relays M0, M1 and M2. M0 is “1” if the contents

of D0 is greater than 100; M1 is “1” if the contents of D0 is precisely 100 and M2 is “1” if D0 is less than 100. The state of the three bit devices is maintained even after the input condition has been switched off because their last state is stored.

FX Beginners Manual

Compare Instructions More Advanced Programming

To compare 32-bit data you just use DCMP instead of CMP: Ladder Diagram

Instruction List

0 LD

0 DCMP D0 D2 M0

1 DCMP D0 D2 M0

In the example above the contents of D0 and D1 are compared with the contents of D2 and D3. The handling of the three bit devices indicating the result of the comparison is exactly the same as for the 16-bit version of the instruction.

Application example

It is easy to create a two-point control loop with the CMP instruction: Ladder Diagram

Instruction List M8000

0 LD

M8000

0 CMP D20 K22 M20

8 RST Y000

10 LD

M22

Y0001 M22

11 SET

10 SET Y000

In this example the CMP instruction is executed cyclically. M8000 is always “1” when the PLC is executing the program. Register D20 contains the value for the current room temperature. Constant K22 contains the setpoint value of 22°C. Relays M20 and M22 show when the tem- perature goes higher or lower than the setpoint. If the room is too warm output Y0 is switched off. If the temperature is too low M22 switches output Y0 on again. This output could be used to control a pump for adding hot water, for example.

MITSUBISHI ELECTRIC

More Advanced Programming Compare Instructions

5.3.2 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 oper- ation 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 >= D40 D50

0 LD>=

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 se-

cond 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 D> D10 D250

0 LDD>

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

Compare Instructions More Advanced Programming

More examples: Ladder Diagram

Instruction List

0 LD>=

C0 D20

0 >= C0 D20

M12

5 OUT

M12

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 LD>

D10 K-2500

0 > D10 K-2500

Y003

5 AND

T52

Y003 Output Y003 is switched on when the contents of D10 is greater than -2,500 and timer T52 has

6 OUT

finished running. Ladder Diagram

Instruction List

0 LDD<

C200 K182547

0 D< C200 K182547

M53

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 Instruction List

0 LD

0 <= D40 D50

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 begin-

ning of an operation. Here too, you can also compare 32-bit values with an AND operation: Ladder Diagram

Instruction List

0 D= D30 D400

0 ANDD= D30 D400

This "D" specifies 32-bit data

MITSUBISHI ELECTRIC

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 begin-

ning 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

Math Instructions More Advanced Programming