LadderLogicOnline

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

InstructionOperandsResult
ADDSource A, Source B, DestDest = A + B
SUBSource A, Source B, DestDest = A − B
MULSource A, Source B, DestDest = A × B
DIVSource A, Source B, DestDest = A ÷ B
MODSource A, Source B, DestDest = 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]-----|
Scale a per-second rate to a per-minute rate.

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]-------|
Adds exactly 1 each time a box breaks the photo eye beam.

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 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]---|
Always true, so Fahrenheit is recalculated every scan.

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]-----|
Switch a timer between an 8 second and a 4 second preset. Timer presets and accumulators are in milliseconds.

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

  1. In the Playground, add DINT tags A, B and Q, and a REAL tag R.
  2. Add a rung with just a DIV in the last column: Source A A, Source B B, Dest Q. Add another with DIV A B R.
  3. Run, then type 7 into A and 2 into B in the Tags panel. Q shows 3 and R shows 3 too, because both sources are integers.
  4. Change the second rung's Source B to 2.0. Now R shows 3.5. Try setting B to 0 and see what each Dest does.

Key points