Chapter 4
PLC Programming
Section 4.9
Math Instructions
Section 4.9.1
Math Instructions
The PLC’s math functions perform arithmetic
functions on values.
With conditional
arithmetic instructions
the input logic
determines when the
instruction executes.
Unconditional instructions execute with each scan.
The basic math instructions
are ADD, SUB, MUL, and
DIV.
Each of these instructions has three inputs. Namely,
Source A, Source B and Destination fields.
The Source A and Source B fields can be an
input rack location, file address, instruction
field or a fixed value.
For example:
Input Location I:1
File Address N7:5
Instruction Field C5:[Link]
Fixed Value 30
N7:5
30
The Destination fields can be an output
location, file address, or an instruction field.
For example:
Output location O:2
File Address N7:8
Instruction Field T4:[Link]
N7:5
30
T4:[Link]
When the CPT (compute)
instruction is executed, then
copy, arithmetic, logical, or
conversion operation residing
in the expression field of this
instruction is performed and
the result is sent to the
destination.
The main advantage of the compute
instruction is that it allows you to enter
complex expressions in one instruction.
Section 4.9.2
Addition Instruction
The ADD instruction adds Source A to Source B
and stores the result in the Destination.
Simulated ADD instruction rung.
Counter program that uses the ADD instruction.
Simulated counter program.
The arithmetic status bits for the SLC 500 are
found in word 0, bits 0 to 3 of the processor
status file S2.
Carry (C) - Address S2:0/0, is set to 1 when there is
a carry in the ADD instruction or a borrow in the SUB
instruction.
Overflow (O) -Address S2:0/1, is set to 1 when the
result is too large to fit in the destination register.
Zero (Z) - Address S2:0/2, is set to 1 when the result
of the subtract instruction is zero.
Sign (S) - Address S2:0/3, is set to 1 when the result
is a negative number.
Section 4.9.3
Subtraction Instruction
The SUBTRACT instruction subtracts Source B
from Source A and stores the result in the
Destination.
Simulated SUB instruction rung.
Vessel overfill alarm program.
Simulated vessel overfill alarm program.
Section 4.9.4
Multiplication Instruction
The MULTIPLY instruction multiplies Source A by
Source B and stores the result in the Destination.
Simulated MUL instruction rung.
MUL instruction program used to calculate the
product of two sources.
Simulated MUL instruction program used to
calculate the product of two sources.
The MUL
instruction
used as part
of a oven
temperature
control
program.
Section 4.9.5
Division Instruction
The DIVIDE instruction divides the value in
Source A by the value in Source B and stores the
result in the Destination address.
Simulated DIV instruction rung.
DIV instruction used to calculate the value that
results from dividing Source A by Source B.
Simulated DIV instruction program.
The Modulo (MOD)
instruction is part
of the ControlLogix
instruction set.
It is used to calculate the remainder after the value
stored in Source A is divided by that stored in Source B.
EXAMPLE
Simulated Modulo (MOD) instruction example.
Section 4.9.6
Other Word Level Math
Instruction
Square Root instruction.
Simulated square root instruction rung.
The negate (NEG) instruction changes the sign
of the source value and then places it in the
destination value.
Simulated negate instruction rung.
The clear (CLR) instruction sets all bits of
a word to zero.
Simulated clear instruction rung.
The convert to BCD (TOD) instruction is used
to convert 16-bit integers into binary-coded
decimal (BCD) values.
Simulated convert to BCD (TOD) instruction rung.
The convert from BCD (FRD) instruction is used
to convert binary-coded decimal (BCD) values to
integer values.
Simulated convert from BCD
(FRD) instruction rung.
Program for
converting
Celsius
temperature to
Fahrenheit.
Simulated Celsius to Fahrenheit program.
At times data must be scaled, or resized, before
it can be used by the PLC program or a field
device. The SLC 500 Scale data (SCL) and Scale
with Parameters (SCP) instructions are used to
perform this task.
SLC 500 SCL (Scale) instruction.
The number 100 stored at the source address, N7:0, is
multiplied by 25,000, divided by 10,000, and added to
127.
The result, 377, is placed in the destination
address, N7:1.
Scale analog input using the SCP instruction.
When executed, the SCP instruction places a number
between 0 and 1000 into N7:20 (destination) based on
the input signal (0 to 10V) coming from the transducer
into the analog input module.
Scale analog output using the SCP instruction.
The SCP directs analog output O:1.0 to provide a
4 to 20mA signal, which is scaled to the valve position
based on a number between 0 and 100.
Section 4.9.7
File Arithmetic Operations
The file arithmetic and logic (FAL) instruction
can combine an arithmetic operation with file
transfer.
The file add
function of the
FAL instruction is
used to perform
addition
operations on
multiple words.
The file subtract function of the FAL instruction
is used to perform subtraction operations on
multiple words.
The file multiplication function of the FAL
instruction is used to perform multiplication
operations on multiple words.
The file divide function of the FAL instruction is
used to perform division operations on multiple
words.