Moving, comparing and calculating
MOV, compare contacts, INC/DEC, integer and real arithmetic, conversions and BCD, with the traps of overflow and division. The tools of the level simulation (TP 2), the speed measurement (TP 1) and the anti-sway timing (TP 4).
30 min
On this page
Boxes (MOV, ADD, MUL…) run when power reaches their EN input and do nothing otherwise. They work on bytes, words (INT), double words (DINT) and REALs; the suffix of the mnemonic tells you which: MOVB, MOVW, MOVD, MOVR.
Move
MOVW +500, VW10 copies 500 into VW10. MOVW C0, VW12 copies a counter's current value; MOVW T32, VW100 a timer's. Moving a timer or counter value is how you read it into your data.
Compare
A comparison is a contact: it passes power when the comparison is true. LDW>= VW10, +300 starts a network with "VW10 ≥ 300"; AW<, OW= combine it. Byte comparisons (LDB) are unsigned, word and double-word comparisons signed, and LDR compares reals.
Drag an instruction onto the rung (or click it, then click a place). Drop below a contact to put it in parallel. Click an element to edit its address; Delete removes it.
No errors: the program compiles.
Turn the potentiometer SMB28 on the panel: LEVEL_HIGH lights above 200, LEVEL_LOW below 50.
Integer arithmetic
In SIMATIC STL, arithmetic is two-operand: +I IN1, OUT means OUT := OUT + IN1. In LAD, the ADD_I box has IN1, IN2 and OUT; when OUT is not IN1, Micro/WIN inserts a MOV first. Watch for:
- Overflow: an INT stops at +32767. An
+Ithat goes beyond it gives a wrong result and sets the overflow bit SM1.1. Use DINT (+D) for large values or clamp them. - Division:
/Ikeeps the integer quotient;DIVgives quotient and remainder in a double word. Dividing by zero sets SM1.3 and leaves OUT unchanged. - Order: compute in a scratch word, then copy the result, so that an intermediate value never reaches an output.
No errors: the program compiles.
Real numbers
For physics (speeds, T/2 of a pendulum) convert to REAL: ITD (INT → DINT) then DTR (DINT → REAL), compute with +R -R *R /R SQRT, and come back with ROUND (to the nearest) or TRUNC (towards zero), then DTI if it must fit a word. TP 4 computes this way from the potentiometer SMB29.
ExplorerGo deeper: derivations and open questions
Scaling a potentiometer. SMB28 gives 0–255. To express it as 0–125 % in integer arithmetic, multiply first and divide last: MUL 125 then DIV 255 keeps the precision; dividing first would give 0 or 1. In REAL arithmetic the order does not matter, but each conversion costs scan time.
BCD and the 7-segment display
A 7-segment display decodes BCD: each decimal digit on 4 bits. IBCD VW0 turns 1234 into 16#1234, one digit per nibble, ready for the display; BCDI does the reverse for a thumbwheel. SEG makes the 7-segment pattern of one hexadecimal digit when the display is driven segment by segment. The PLC-200 display (TP 3) takes BCD data on Q0.2–Q0.5 with digit latches on Q0.6–Q1.1.