PLC Math Instruction
PLC Math Instruction
PLC Math
Instructions
LE
Chapter Outline
10.1 Addition
10.2 Subtraction
10.3 Multiplication
10.4 Division
10.5 Advanced Math Instructions
10.6 LogixPro Simulator Examples terekhov igor/Shutterstock.com
Technical Terms
add (ADD) instruction
divide (DIV) instruction
P multiply (MUL) instruction
subtract (SUB) instruction
M
Learning Objectives
After completing this chapter, you will be able to:
• Program the add instruction in the PLC ladder logic diagram.
A
Introduction
I
n Chapter 9, you learned how to program the PLC counter instructions
and use them in industrial control systems. In this chapter, you will learn
how to use PLC math instructions such as add, subtract, multiply, and divide
to calculate the sum, difference, product, and quotient of the content of registers
in two PLC counter instructions. Examples are used to illustrate the use of
these math instructions in PLC ladder logic diagrams. The usual interval to
update the results of PLC arithmetic instructions is one scan period.
10.1 Addition
When parts on two separate conveyors are placed onto a single larger conveyor, the
total parts on the large conveyor may need to be calculated. This calculation can be
done with the add instruction. The add (ADD) instruction calculates the sum of
two operands (Source A and Source B). Figure 10‑1 displays an add instruction for
the Allen-Bradley SLC 500 series PLC. Add instructions in SLC 500 series PLCs
are displayed in block format.
Switch
I:0 ADD
0000 Add
0 Source A N7:0
LE
0<
Source B N7:1
0<
Dest N7:2
0<
0001 End
Goodheart-Willcox Publisher
Figure 10‑1. Add (ADD) instruction for an Allen-Bradley SLC 500 series PLC and ControlLogix Programmable
Automation Controllers (PACs). P
The add instruction has three operands. Two operands hold the source data
(Source A and Source B), which are added together. Both sources can be registers,
or one source can be a register while the other one is a number. The third operand
is the destination, which holds the sum of Source A and Source B. The destination
M
must be a register. In Figure 10‑1, Sources A and B are the content of integer
registers N7:0 and N7:1. The destination register is N7:2. Example 10-1 illustrates
how to use the add instruction to calculate the content of the accumulated register for
two PLC counter instructions. After a math, logic, or move instruction is executed,
the status bits in the status file are updated. The status bits S2:0/0 through S2:0/3
are in word S2:0 in the processor status file (S2). General descriptions of these bits
are listed in Table 10‑1.
A
Table 10-2 describes how status bits are affected when ADD instruction is carried out.
Example 10‑1
LE
Figure 10‑2 shows a section of a manufacturing plant with three conveyors. The number of parts in
conveyor A and the number of parts in conveyor B are added to get the number of parts on the main conveyor.
Figure 10‑3 displays the PLC ladder diagram for Example 10-1.
Counter B
input
Conveyor B
P
M
Main
conveyor
Counter A
A
input
S
Conveyor A
Goodheart-Willcox Publisher
The green pushbutton (Green_PB) represents the proximity switch input for counter A. The red push-
button (Red_PB) represents the proximity switch input for counter B. Pressing the green pushbutton
increments the content of the accumulated register in counter instruction zero (C5:0). Pressing the red
pushbutton increments the content of the accumulated register in counter instruction one (C5:1). The add
instruction in rung 0002 places the sum of the content of the accumulated registers in the integer register
N7:2. When the content of the accumulated register for either counter instruction is equal to 1000, the
reset (RES) instruction in rung 0003 enables, resetting both counter instructions to zero.
221
Green_PB Counter A
I:0 CTU
0000 Count up CU
1 Counter C5:0
Preset 1000< DN
Accum 0<
Red_PB Counter B
I:0 CTU
0001 Count up CU
2 Counter C5:1
Preset 1000< DN
Accum 0<
LE
Register N2 holds the number of parts in the main conveyor.
ADD
0002 Add
Source A C5:0.ACC
0<
Source B C5:1.ACC
0<
Dest N7:2
0<
Counter A
0003
C5:0
DN
C5:1
P C5:0
RES
Counter B
C5:1
RES
M
DN
0004 End
Goodheart-Willcox Publisher
10.2 Subtraction
A
A factory has three conveyors: A, B, and C. Conveyors A and B merge together into
the larger C. You want to know how many parts are coming off conveyor A, but
you are unable to place a sensor near the end of the conveyor. By using sensors on
conveyors B and C and the subtract instruction, you can calculate the number of
parts on conveyor A. The subtract (SUB) instruction calculates the difference
S
between two sources: Source A and Source B. Figure 10‑4 displays a subtract
instruction for the Allen-Bradley SLC 500 series PLC. Subtract instructions in the
SLC 500 series PLC are displayed in block format.
The subtract instruction has three operands. Two operands, Source A and Source B,
hold the source data. The content of Source B is subtracted from the content of Source A.
The result is placed in the destination. Both sources can be registers, or one source
can be a register while the other one is a number. The destination must be a register.
Table 10-3 describes how status bits are affected when SUB instruction is carried out.
In Figure 10‑4, Sources A and B are the content of integer registers N7:0 and N7:1.
The destination register is N7:2. Example 10-2 illustrates how to use the subtract instruc-
tion to calculate the content of an accumulated register for two counter instructions.
Switch
I:0 SUB
0000 Subtract
Source A N7:0
LE
0 0<
Source B N7:1
0<
Dest N7:2
0<
0001 End
Goodheart-Willcox Publisher
Figure 10‑4. Subtract (SUB) instruction for an Allen-Bradley SLC 500 series PLC and ControlLogix Programmable
Automation Controllers (PACs).
Example 10‑2
P
Figure 10‑5 shows a section of a manufacturing plant with three conveyors. It is not convenient to install
and maintain a proximity switch in conveyor B. Therefore, the number of parts on conveyor A are subtracted
from the number of parts on conveyor C to calculate the number of parts on conveyor B. Figure 10‑6
M
displays the PLC ladder diagram for Example 10-2.
Conveyor
B
A
Counter C
input
S
Conveyor
C
Counter A
input
Conveyor
A
Goodheart-Willcox Publisher
The green pushbutton (Green_PB) represents the proximity switch input for counter A. The red push-
button (Red_PB) represents the proximity switch input for counter C. Pressing the green pushbutton
increments the content of the accumulated register in counter instruction zero (C5:0). Pressing the red
pushbutton increments the content of the accumulated register in counter instruction one (C5:1). The subtract
instruction in rung 0002 places the difference of the content of the accumulated registers in the integer
register N7:2. When the content of the accumulated register for either counter instruction is equal to 1000,
the reset (RES) instruction in rung 0003 enables, resetting both counter instructions to zero.
Green_PB Counter A
I:0 CTU
0000 Count up CU
1 Counter C5:0
Preset 1000< DN
LE
Accum 0<
Red_PB Counter C
I:0 CTU
0001 Count up CU
2 Counter C5:1
Preset 1000< DN
Accum 0<
Dest
0<
Source B C5:0.ACC
0<
N7:2
M
0<
Counter A
C5:0 C5:0
0003 RES
DN
Counter C
C5:1
A
C5:1
RES
DN
0004 End
Goodheart-Willcox Publisher
10.3 Multiplication
When multiple parts are packed in containers, the number of parts per container,
the number of containers, and the multiply instruction will give you the total number
of parts. The multiply (MUL) instruction calculates the product of two sources:
Source A and Source B. Figure 10‑7 displays a multiply instruction for the Allen-
Bradley SLC 500 series PLC. Multiply instructions in SLC 500 series PLCs are
displayed in block format.
Table 10-4 describes how status bits are affected when MUL instruction is
carried out.
LE
Goodheart-Willcox Publisher
Switch
I:0 MUL
0000 Multiply
Source A N7:0
0
0<
Source B N7:1
0<
0001
P Dest N7:2
0<
End
Goodheart-Willcox Publisher
Figure 10‑7. Multiply (MUL) instruction for an Allen-Bradley SLC 500 series PLC and ControlLogix Programmable
M
Automation Controllers (PACs).
The multiply instruction has three operands. Two operands hold the source data
(Source A and Source B). The content of Source B is multiplied by the content of
Source A. The result is placed in the destination. Both sources can be registers, or one
source can be a register while the other one is a number. The destination must be
a register.
A
S
Example 10‑3
Figure 10‑8 displays the PLC ladder diagram for Example 10-3. Pressing the green pushbutton (Green_PB)
increments the content of the accumulated register in counter instruction zero (C5:0). Pressing the red push-
button (Red_PB) increments the content of the accumulated register in counter instruction one (C5:1). The
multiply instruction in rung 0002 places the result of the multiplication in the integer register N7:2. When
the content of the accumulated register for either of the counter instructions is equal to 100, the reset (RES)
instructions in rung 0003 enables, resetting both counter instructions to zero.
Green_PB Counter A
I:0 CTU
0000 Count up CU
1 Counter C5:0
DN
LE
Preset 100<
Accum 0<
Red_PB Counter B
I:0 CTU
0001 Count up CU
2 Counter C5:1
Preset 100< DN
Accum 0<
0002
P Multiply
Source A
Source B
MUL
C5:0.ACC
0<
C5:1.ACC
0<
M
Dest N7:2
0<
Counter A
C5:0 C5:0
0003 RES
DN
A
Counter B
C5:1 C5:1
RES
DN
0004 End
S
Goodheart-Willcox Publisher
In Figure 10‑7, Sources A and B are the content of integer registers N7:0 and
N7:1. The destination register is N7:2. (Notice that if both sources contain the same
number, then the multiply instruction acts like a square instruction.) Example 10-3
illustrates how to use the multiply instruction to calculate the content of accumulated
registers for two PLC counter instructions.
10.4 Division
Assume that a machine is counting the number of inches of steel that pass by. The
machine needs to calculate the length in feet or yards before it makes some engravings.
226 Programmable Logic Controllers: Hardware and Programming
The divide instruction is the perfect PLC instruction for this task. The divide (DIV)
instruction calculates the integer value that results from dividing Source A by
Source B. Figure 10‑9 displays a divide instruction for the Allen-Bradley SLC 500
series PLCs. Divide instructions in SLC 500 series PLCs are displayed in block format.
Switch
I:0 DIV
0000 Divide
0 Source A N7:0
0<
Source B N7:1
0<
Dest N7:2
LE
0<
0001 End
Goodheart-Willcox Publisher
Figure 10‑9. Divide (DIV) instruction for the Allen-Bradley SLC 500 series PLC and ControlLogix Programmable
Automation Controllers (PACs).
The divide instruction has three operands. Two registers hold the source data
(Source A and Source B). The content of Source A is divided by the content of Source B.
The integer quotient is placed in the destination. As with the other instructions,
P
both sources can be registers, or one source can be a register while the other one is
a number. The destination must be a register. Table 10-5 describes how status bits
are affected when DIV instruction is carried out.
In Figure 10‑9, Sources A and B are the content of integer registers N7:0 and
N7:1. The destination register is N7:2. Example 10-4 illustrates how to use the
divide instructionto calculate the content of the accumulated registers for two
counter instructions.
Example 10‑4
Figure 10‑10 displays the PLC ladder diagram for Example 10-4. Pressing the green pushbutton (Green_PB)
increments the content of the accumulated register in counter instruction zero (C5:0). Pressing the red
pushbutton (Red_PB) increments the content of the accumulated register in counter instruction one (C5:1).
The divide instruction in rung 0002 places the quotient in the integer register N7:2. When the content of the
accumulated register for either counter instructions is equal to 100, the reset (RES) instruction in rung 0003
enables, resetting both counter instructions to zero.
Green_PB Counter A
I:0 CTU
0000 Count up CU
1 Counter C5:0
Preset 100< DN
Accum 0<
Red_PB Counter B
I:0 CTU
0001 Count up CU
2 Counter C5:1
Preset 100< DN
Accum 0<
LE
Register N7:2 holds the quotient.
DIV
0002 Divide
Source A C5:0.ACC
0<
Source B C5:1.ACC
0<
Dest N7:2
0<
0003
C5:0
DN
C5:1
P Counter A
C5:0
RES
Counter B
C5:1
M
RES
DN
0004 End
Goodheart-Willcox Publisher
Instruction Name
ACS (source, destination) Arc Cosine
COS (source, destination) Cosine
ASN (source, destination) Arc Sine
SIN (source, destination) Sine
ATN (source, destination) Arc Tangent
TAN (source, destination) Tangent
XPY (source, source, destination) X to the Power of Y
LN (source, destination) Natural Log
LOG (source, destination) Log to the Base of 10
CPT (destination, expression) Compute
Goodheart-Willcox Publisher
LE
Table 10-6. List of advanced math instructions available for SLC 5/03, SLC 5/04, and
SLC 5/05 processors and ControlLogix Programmable Automation Controllers (PACs).
Compute
I:1 CPT
0000 Compute
0 Dest N7:0
1746-IB16 0<
0001
P Expression (TAN N7:1)*(COS N7:2)
End
M
Goodheart-Willcox Publisher
Figure 10‑11. Using the compute (CPT) instruction to solve an equation. Notice that the first entry is the
destination register.
Example 10‑5
Given the following equation, you can use the CPT instruction to solve it.
A
B = F8:1
C = F8:2
D = F8:3
Y = F8:4
Figure 10‑12 displays the PLC ladder diagram for Example 10-5. When the normally open contact I:1/0
is closed, the equation in the CPT instruction block is calculated and the result is placed in the floating point
register four (F8:4). A list of other commonly used math instructions is displayed in Table 10‑7.
229
Compute
I:1 XPY
0000 X to Power of Y
0 Source A 2.718
1746-IB16 2.718<
Source B F8:2
0.0<
Dest F8:5
0.0<
CPT
Compute
Dest F8:4
0.0<
Expression ((F8:0 * LN F8:1)* F8:5)+
LE
(TAN F8:3)
0001 End
Goodheart-Willcox Publisher
Example 10‑6
Figure 10‑13 shows a ladder logic diagram programmed with various math functions. The G-PB (I:1/0) and
R-PB (I:1/1) inputs are used to control counter up functions on rungs 000 and 001. Each time one of these in-
puts is closed, its corresponding counter up function increments its accumulated register (C5:0 or C5:1). These
counter up functions allow the user to select two numbers and store them in the counter accumulated registers
to use with the math functions.
Copyright Goodheart-Willcox Co., Inc.
230
The inputs at addresses I:1/2, I:1/3, I:1/4, I:1/5, and I:1/6 are used to activate the math functions. Each math
function has its own integer register (N7:0 through N7:4) where it stores the answer to its function. Actuating the
master switch (MSW) at address I:1/7 resets the accumulated registers (C5:0.ACC and C5:1.ACC) of both counters
to zero. Each math function holds its calculated answer until a new set of numbers is calculated.
LE
Preset 1000 DN
Accum 0
Dest
?
Source B C5:1.ACC
?
N7:1
?
M
I:1/7 I:1/4 MUL
004 Multiply
MSW Multiplication Source C5:0.ACC
?
Source B C5:1.ACC
?
Dest N7:2
?
A
I:1/7 C5:0
007 RES
MSW
C5:1
RES
008 End
Goodheart-Willcox Publisher
Figure 10‑13. PLC ladder logic diagram for Example 10-6. Using math functions for data manipulation.
Example 10‑7
Figure 10‑14 shows data conversion programming in a ladder logic diagram for TOD and FRD data conversion
functions and their corresponding input/output (I/O) configurations. Input module three (I:3) provides a 16-bit
integer value (0000000000000101) to the TOD function, which converts that value to a BCD value displayed
in output O:6. Closing the NO switch G-PB at address I:1/0 initiates the conversion of the I:3 input into BCD.
Output module six (O:6) displays the BCD value by energizing its individual outputs (O:6/0 through O:6/15),
which also feed a 7-segment display that shows the decimal form of the number (5).
LE
Dest O:6
5
002 End
P Goodheart-Willcox Publisher
Figure 10‑14. PLC ladder logic diagram and input/output (I/O) configuration for Example 10-7. Using PLC data
M
conversion functions.
The thumbwheel connected to input module five (I:5) is used to enter a 4-digit decimal number (21) that
is converted to BCD (00100001) in the individual inputs (I:5/0 through I:5/15). This BCD number is entered
into the FRD function. Closing the NO switch R-PB at address I:1/1 converts the I:5 BCD number to binary
and displays it on LEDs connected to output module four (O:4): 0000000000010101.
A
S
Summary
• The add, subtract, multiply, and • The multiply instruction calculates the product
divide instructions each have three of Source A and Source B and places this value
operands: two for source data and one for in the destination register.
the destination. • The divide instruction calculates the integer
• The add instruction calculates the sum of value that results from dividing Source A
Source A and Source B and places this value in by Source B and places this value in the
the destination register. destination register.
• The subtract instruction calculates the • Advanced math instructions, such as the
difference between Source A and Source B and compute (CPT) instruction, can be used to solve
places this value in the destination register. mathematical equations.
LE
Know and Understand
1. Based on the following ADD function block, 3. In the ladder logic diagram displayed, when
which of the following statements is false? SW1 is closed, the content of register T4:0 is
equal to _____.
ADD
Source A
I:1/0 CTU
Source B C5:0 Pre
LS1 150
Destination P
A. Source A can be either a number or a register.
B. Source B can be either a number or a register.
I:1/1
C5:0 ACC
CTU
100
C5:1 Pre
C. Destination can be either a number or 45
M
LS2
a register. C5:1 ACC
50
D. Source A, Source B, and Destination can
be registers.
I:3/1 ADD
2. In the ladder logic diagram displayed, when C5:0 ACC
SW1 is closed, the content of register N7:1 is SW1
C5:1 ACC
equal to _____. T4:0 Pre
A
I:1/1 ADD
T4: 0·Acc A. 200
SW1 120 B. 195
N7: 0
C. 150
35
S
N7: 1 D. 145
A. 156
B. 155
C. 246
D. 256
4. For the following SUB function block, which 8. Which status bit is not used in an
statement is true? arithmetic instruction?
A. Overflow (OV)
SUB B. Underflow (UN)
Source A C. Zero (Z)
D. Sign (S)
Source B
9. Status word S:13 contains the _____.
Destination A. most significant word of the 32-bit result of
MUL instruction
B. un-rounded quotient of a 32-bit DIV
A. Source A and Destination can be numbers. instruction
B. Source A, Source B, and Destination can C. least significant word of the 32-bit result of
LE
be numbers. MUL instruction
C. Source A, Source B, and Destination can D. None of the above.
be registers.
10. Status word S:14 contains the _____.
D. None of the above. A. remainder of a 32-bit DIV instruction
5. Binary subtraction is done _____. B. unrounded quotient of a 32-bit DIV
A. using the same method as decimal instruction
subtraction C. least significant word of a 32-bit result of
B. using the inverse method of MUL instruction
decimal subtraction D. None of the above.
C. by adding one to the decimal P 11. Which of the following Allen-Bradley PLC has
subtraction result
4-byte registers?
D. by subtracting one from the decimal A. SLC 503
subtraction result
B. SLC 505
6. Which of the following binary number C. PLC 5
M
multiplications is correct?
D. ControlLogix
A. 11101100 × 11 = 11000100
B. 11101100 × 11 = 01100100 12. Which of the following instruction is used to
multiply source by rate value, adds an offset to
C. 11101100 × 11 = 11000011
it, then stores the result in destination?
D. 11101100 × 11 = 11000001 A. SWP (Swap data)
7. In the following ladder logic diagram, when B. CPI (Compute)
A
SW1 is closed, the content of N7: 0 is _____. C. SCP (Scale with Parameters)
D. SCL (Scale Data)
I:3/1 CTU 13. What type of instructions are math
Count up instructions?
Counter C5:0 A. Input
PB1
S
Preset 10
Accum 9 B. Output
C. Hexadecimal (HEX)
I:1/1 MUL D. Binary coded decimal (BCD)
C5:0 acc
14. The FIL instruction is used to _____.
19
SW1
N7:0
A. copy data from one register to another
register
B. copy a constant value into a register
A. 224 C. copy a constant value into a group of registers
B. 190 D. None of the above.
C. 171
D. 180
LE
6. In a multiply instruction, what is the largest
17. After DIV instruction is carried out, _____.
number that can be held in the destination
A. overflow status (OV) bit is always cleared
register? (Hint: Think about the destination
B. sign status (S) bit is always cleared register.)
C. carry status (C) bit is always cleared
7. At least one of the _____ in a multiply
D. carry status (C) bit is always set instruction must be a register.
18. FRD instruction converts _____. 8. Can both sources in a divide instruction be
A. binary to HEX registers? Explain.
B. binary to BCD 9. How many status bits are available for math
C. BCD to binary P functions in Allen-Bradley programmable logic
D. BCD to HEX controllers? Name them.
19. What math instruction must you use if you want 10. Describe the use of the overflow bit (S2:0/1) in
to change the sign of source value to its opposite? an Allen-Bradley PLC add instruction.
A. SUB 11. Describe the use of the sign bit (S2:0/3) in an
M
B. MUL Allen-Bradley PLC subtract instruction.
C. NEG 12. Describe the SQR instruction.
D. SQR 13. Which instruction is used to erase a number
from an Allen-Bradley PLC memory?
20. After the following instruction is carried out,
the content of N7:2 would be _____. 14. Describe the DCD instruction.
15. True or False? Source values are inputs to
A
A. 11
B. 6
C. 5
D. 550
LE
T1 is temperature in degree Kelvin (°K): first your program should convert the data from
T1 = t1 + 273 BCD to binary. Then, use the equation in your
T 2 is temperature in degree Kelvin (°K): program to convert the temperature to Fahren-
T 2 = t 2 + 273 heit. Finally, convert the data to BCD and place
Assume that three counters provide the vari- it on the 7-segment display that is connected to
ables labeled R1, t1, and t2. output module six (O:6).
Your PLC program should calculate R2.
2. Write a program that will add the content of
two counters every 45 seconds and place the
result in an integer register. P
3. Four conveyors are delivering the same parts
in different packages. A package can hold 6,
8, 12, or 18 parts. Four proximity switches
are used to advance the values of counters.
M
Write a program that uses multiply and add
instructions to calculate the sum of parts.
A
S