Compare (CMP)

This information applies to the
CompactLogix
5370,
ControlLogix
5570,
Compact GuardLogix
5370,
GuardLogix
5570,
Compact GuardLogix
5380,
CompactLogix
5380,
CompactLogix
5480,
ControlLogix
5580, and
GuardLogix
5580 controllers. Controller differences are noted where applicable.
Define the CMP expression using operators, tags, and immediate values. Use parentheses ( ) to define sections of more complex expressions.
The advantage of the CMP instruction is that it allows complex expressions in one instruction.
When evaluating the expression all non-REAL operands will be converted to REAL before the calculations are performed if any of the following conditions is true.
  • Any operand in the expression is REAL.
  • The expression contains SIN, COS, TAN, ASIN, ACOS, ATAN, LN, LOG, DEG or RAD.
There are rules for allowable operators in safety applications. See
Valid Operators.
Available Languages
Ladder Diagram
RSL5K_CMP Ladder Diagram_v31
Function Block
This instruction is not available in function block.
Structured Text
This instruction is not available in structured text.
Operands
These are the operands for the CMP instruction.
IMPORTANT:
Unexpected operation may occur if:
  • Output tag operands are overwritten.
  • Members of a structure operand are overwritten.
  • Except when specified, structure operands are shared by multiple instructions.
There are data conversion rules for mixing numeric data types within an instruction. See Data Conversions.
Ladder Diagram
The following is the Ladder Diagram operand.
Operand
Data Type
CompactLogix 5370, ControlLogix 5570, Compact GuardLogix 5370, and GuardLogix 5570 controllers
Data Type
CompactLogix 5380, CompactLogix 5480, ControlLogix 5580, Compact GuardLogix 5380, and GuardLogix 5580 controllers
Format
Description
Expression
SINT
INT
DINT
REAL
String type
SINT
INT
DINT
LINT
USINT
UINT
UDINT
ULINT
REAL
LREAL
String type
immediate tag
An expression consisting of tags and/or immediate values separated by operators
Formatting expressions
For each operator used in an expression, one or two operands (tags or immediate values) must be provided. Use the following table to format operators and operands within an expression.
For operators that operate on:
Use this format:
Example
One operand
operator(operand)
ABS(tag)
Two operands
operand_a operator operand_b
tag_b + 5
tag_c AND tag_d
(tag_e**2) MOD (tag_f / tag_g)
Determine the order of operation
The instructions performs operations in the expression are in a prescribed order, not necessarily the order they appear. The order of operation can be specified by grouping terms within parentheses, forcing the instruction to perform an operation within the parentheses ahead of their operations.
Operations of equal order are performed from left to right.
Order
Operation
1
( )
2
ABS, ACOS, ASIN, ATAN, COS, DEG, BCD_TO, IsINF, IsNAN, LN, LOG, RAD, SIN, SQRT, TAN, TO_BCD, TRUNC
3
**
4
- (negate), NOT, !
5
*, /, MOD
6
- (subtract), +
7
AND
8
XOR
9
OR
10
<, <=, >, >=, =, <>
11
&&
12
^^
13
||
Using strings in an expression
To use strings of ASCII characters in an expression, follow these guidelines:
  • An expression can compare two string tags.
  • ASCII characters cannot be entered directly into the expression.
  • The following operands are permitted:
    Operator
    Description
    =
    Equal
    <
    Less than
    <=
    Less than or equal
    >
    Greater than
    >=
    Greater than or equal
    <>
    Not equal
  • Strings are equal if their characters match.
  • ASCII characters are case-sensitive. Uppercase A ($41) is not equal to lowercase a ($61).
  • The hexadecimal values of the characters determine if one string is less than or greater than another string.
  • When the two strings are sorted as in a telephone directory, the order of the strings determine which one is greater.
CMP strings
Affects Math Status Flags
Controllers
Affects Math Status Flags
CompactLogix 5380, CompactLogix 5480, ControlLogix 5580, Compact GuardLogix 5380, and GuardLogix 5580 controllers
No
CompactLogix 5370, ControlLogix 5570, Compact GuardLogix 5370, and GuardLogix 5570 controllers
The CMP instruction affects the math status flags if the expression contains an operator (for example, +, −, *, /) that affects the math status flags.
Major/Minor Faults
None specific to this instruction. See Index Through Arrays for array-indexing faults.
Execution
Ladder Diagram
Condition/State
Action Taken
Prescan
N/A.
Rung-condition-in is false
Set Rung-condition-out to Rung-condition-in
Rung-condition-in is true
Set Rung-condition-out to Rung-condition-in
if expression evaluates to false
Rung-condition-out is cleared to false
Postscan
N/A
Example
Ladder Diagram
RSL5K_CMP Ladder Diagram Example_v31
If value_1 is equal to value_2, light_a is set to true. If value_1 is not equal to value_2, light_a is cleared to false.
Provide Feedback
Have questions or feedback about this documentation? Please submit your feedback here.