FX
/FX
/FX
Series Programmable Controllers
3G
3U
3UC
Programming Manual - Basic & Applied Instruction Edition
Program examples
The program examples below are provided to obtain the absolute value of a negative binary value.
1. Obtaining the absolute value of a negative value using NEG instruction
M8000
RUN monitor
M 0
2. Obtaining the absolute value by SUB (subtraction) instruction
Even if NEG instruction is not used, D30 always stores the absolute value of the difference.
X000
M 10
M 11
M 12
Negative value expression and absolute value (reference)
In PLCs, a negative value is expressed in 2's complement.
When the most significant bit is "1", it is a negative value, and its absolute value can be obtained by NEG
instruction.
(D 10) = 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
(D 10) = 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
(D 10) = 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
(D 10) = −1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
(D 10) = −2
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
(D 10) = −32767
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
(D 10) = −32768
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
292
FNC 44
D 10
M 0
BON
FNC 29
D10 → D10
D 10
NEGP
FNC 10
D 10
D 20
CMP
FNC 21
D 10
D 20
SUB
FNC 21
D 20
D 10
SUB
10 Arithmetic and Logical Operation (+, −, ×, ÷) – FNC 20 to FNC 29
In BON (ON bit check) instruction, M0 turns ON
K 15
when the bit 15 (b15 among b0 to b15) of D10 is "1".
NEGP instruction is executed for D10 only when M0
turns ON.
(D 10) > (D 20) (D10) = (D 20) (D 10) < (D 20)
M 10
M 10 = ON
In the case of "D10 ≥ D20",
D 30
D10 − D20 → D30.
In the case of "D10 < D20",
D 30
D20 − D10 → D30.
(D 10) + 1 = 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
(D 10) + 1 = 2
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
(D 10) + 1 = 32767
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
(D 10) + 1 = −32768
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
The absolute value can be obtained up to 32767.
10.10 FNC 29 – NEG / Negation
M 11 = ON
M 12 = ON