Learn › Instructions
Math and move instructions: ADD, SUB, MUL, DIV, CPT and MOV
Math and move instructions are outputs that work with numbers instead of bits. They calculate a result or copy a value into a DINT or REAL tag, every scan their rung is true.
The basics
All of these are output instructions, so they go in the last column of a rung, where a coil would go. While the rung is true they run every scan and write their result to the Dest operand. While the rung is false they do nothing at all: the Dest keeps whatever value it had. That's different from an OTE coil, which turns its bit off when the rung goes false.
Sources can be numbers you type, DINT or REAL tags, BOOL tags (which read as 0 or 1) or timer and counter words like T1.ACC. The Dest must be something that can hold a number: a DINT tag, a REAL tag, or a timer or counter's .PRE or .ACC. Writing to a preset is how a program changes a timer's time or a counter's target while it runs.
ADD, SUB, MUL and DIV
| Instruction | Operands | Result |
|---|---|---|
ADD | Source A, Source B, Dest | Dest = A + B |
SUB | Source A, Source B, Dest | Dest = A − B |
MUL | Source A, Source B, Dest | Dest = A × B |
DIV | Source A, Source B, Dest | Dest = A ÷ B |
MOD | Source A, Source B, Dest | Dest = remainder of A ÷ B |
There are also single-source instructions: NEG (Dest = −Source), ABS (absolute value) and SQRT (square root; like a real PLC it takes the absolute value first, so the square root of −16 gives 4).
| Enable |
|----] [------[MUL A Rate B 60 Dest PerMinute]-----|
The "every scan" trap
Because math runs on every scan the rung is true, a rung like XIC Button → ADD Total 1 Total doesn't add 1 per press. It adds 1 on each scan the button is held, which in the Playground is around 60 a second. When you want something to happen once per event, put a one-shot in front of it (see ONS, OSR and OSF):
| PE1 PE1_ONS |
|----] [------[ONS]------[ADD A Boxes B 1 Dest Boxes]-------|
Calculations that should always be up to date, like scaling a reading, are the opposite: give them a rung with nothing in front of the instruction, so it's always true and recalculates every scan.
Integers versus REALs
The data type matters, and the rules follow Allen-Bradley Logix controllers:
- If both sources are integers, the maths is integer maths.
DIV 7 2gives 3, not 3.5: integer division throws the fraction away (it truncates toward zero, so −7 ÷ 2 gives −3).MOD 7 2gives the remainder, 1. - If either source is a REAL, the result is a REAL.
DIV 7.0 2gives 3.5. - Writing a REAL into a DINT rounds it, with halves going to the nearest even number: 2.5 becomes 2, 3.5 becomes 4, 2.7 becomes 3. So
DIV 7.0 2into a DINT stores 4. - DINTs are 32-bit. They hold whole numbers from −2,147,483,648 to 2,147,483,647. A result outside that range wraps around, just like on a PLC: adding 1 to the largest value gives the most negative one.
If you need a fraction, make sure at least one source is a REAL (a REAL tag, or a number with a decimal point like 2.0) and that the Dest is a REAL. Getting 0 from DIV Part Total when you expected 0.25 is the most common surprise in PLC maths.
Dividing by zero
With integers, dividing by zero (or MOD by zero) has no answer, so the instruction does nothing and Dest keeps its old value. With REALs, the result follows floating-point rules: a positive number divided by zero is infinity, a negative one is minus infinity, and 0 ÷ 0 is NaN ("not a number"). You can test for those with IsINF and IsNAN (see Compare instructions). Either way, it's good practice to guard a division with a NE Divisor 0 condition.
CPT: Compute
CPT evaluates a whole expression and writes the result to Dest, which saves chaining several math boxes. It has two operands: Dest and the Expression.
| |
|-------------------------[CPT Dest Fahrenheit Expr Celsius * 9.0 / 5.0 + 32]---|
Expressions use + - * /, MOD, ** for powers, parentheses, and functions like ABS, SQRT, SIN, COS, TAN, LN, LOG, DEG, RAD and TRN (truncate a REAL to a whole number). The same integer and REAL rules apply inside the expression: Celsius * 9 / 5 with a DINT Celsius is integer maths and loses the fraction, which is why the example uses 9.0 and 5.0. Expressions are checked when you press Run, and the program won't start if one can't be understood.
MOV and CLR
MOV copies Source into Dest every scan its rung is true. It's the everyday way to load a value: set a timer preset from a recipe, store a reading at a moment in time, or reset a total to a starting number.
| SlowMode |
|----] [------[MOV Src 8000 Dest Fill.PRE]-----|
| SlowMode |
|----]/[------[MOV Src 4000 Dest Fill.PRE]-----|
CLR is the shortcut for moving zero: it has a single Dest operand and sets it to 0 while the rung is true.
There are also bitwise instructions for working with the individual bits of integers: AND, OR, XOR, NOT, MVM (masked move), BTD (bit field distribute) and SWPB (swap bytes). You'll mostly meet these when packing and unpacking status words.
Try it in the Playground
- In the Playground, add DINT tags
A,BandQ, and a REAL tagR. - Add a rung with just a
DIVin the last column: Source AA, Source BB, DestQ. Add another withDIV A B R. - Run, then type 7 into
Aand 2 intoBin the Tags panel.Qshows 3 andRshows 3 too, because both sources are integers. - Change the second rung's Source B to
2.0. NowRshows 3.5. Try settingBto 0 and see what each Dest does.
Key points
- Math and moves are outputs: they run every scan while true and leave Dest alone while false.
- Use a one-shot when the calculation should happen once per event.
- Integer in, integer out: put a REAL in the sources when you need fractions.
- Writing a REAL to a DINT rounds half to even; DINT results wrap at 32 bits.