MOV, ADD and the rest: maths in ladder logic
Arithmetic in a rung is conditional, which changes how you have to think about it.
By the LADX engineering team
Short answer
Maths instructions in ladder sit on the output side, so they execute only while the rung is true. A MOV copies a source to a destination; ADD, SUB, MUL and DIV take two operands and a destination. Because they are conditional, an unconditional calculation needs an always-true rung, and integer division truncates rather than rounding.
Maths instructions live on the output side of a rung, which means they are conditional. That single fact accounts for most of the surprises.
Conditional by default
MOV(Setpoint, Target) behind a contact copies only while the contact conducts. Behind nothing, on an unconditional rung, it copies every scan.
Both are useful. Continuous scaling wants every scan. Capturing a value at a moment wants a condition and usually a one shot.
Integer division truncates
This catches everyone once. 5 / 10 in integer maths is 0. Scaling code written as (raw - offset) / span * range produces zero for almost every input, because the division happens first and throws away everything.
Multiply before you divide, or do the calculation in a REAL and convert at the end. The instruction order is the fix, not the data type alone.
Overflow is silent
An INT holds up to 32,767. Multiply two values that each look reasonable and the result wraps into a negative number with no error and no indication. If the operands can be large, use a DINT for the destination and the intermediate.
Where ladder stops being the right tool
Three nested calculations across six rungs is harder to read than four lines of Structured Text. Most platforms let you call an ST routine from ladder, and a calculation is exactly the thing worth moving.
Sources and further reading
- Rockwell Logix 5000 General Instructions referenceliterature.rockwellautomation.com
- Integer overflow explained (Wikipedia)en.wikipedia.org
Common questions
- Why is my PLC division returning zero?
- Integer division truncates. Dividing 5 by 10 in integer maths gives 0, not 0.5. Either multiply before you divide, so the intermediate value stays large, or use a REAL data type for the calculation and convert at the end.
- Does a MOV instruction run every scan?
- Only while its rung is true. A MOV on an unconditional rung runs every scan, which is usually what you want for continuous calculations. A MOV behind a condition runs only when that condition holds, which is what you want for latching a value.
Keep reading
- Instructions
Studio 5000 MSG instruction: path, setup and error codes
How to set up a Studio 5000 MSG instruction: message types, the communication path, connected and cached messages, and what each .ERR and .EXERR code means.
- Instructions
Sequencer instructions, and the state machine that replaced them
SQO, SQI and SQL, what they were for, why they are rare in new code, and what to write instead.
- Instructions
FIFO and LIFO: tracking parts that are already moving
Load, unload, and the shift register they compete with. Where a queue is the right model, and where it silently loses a part.