You are on page 1of 18

Chapter 10

PLC Math Instructions

| Chapter Outline
10.1 Introduction
10.2 Addition
10.3 Subtraction
10.4 Multiplication
10.5 Division
10.6 Advanced Math Instructions
10.7 LogixPro Simulator Examples


Technical Terms
add (ADD) instruction multiply (MUL) instruction
subtract (SUB) instruction divide (DIV) instruction

| Learning Objectives
After completing this chapter, you w ill be able to:
• Program the add instruction in the PLC ladder logic diagram.
• Program the subtract instruction in the PLC ladder logic
diagram.
• Program the multiply instruction in the PLC ladder logic
diagram.
• Program the divide instruction in the PLC ladder logic diagram.

110.1 Introduction
In Chapter 9, you learned how to program the PLC counter instruc­
tions and use them in industrial control systems. In this chapter, you
w ill learn how to use PLC math instructions such as add, subtract, mul­
tiply, and divide to calculate the sum, difference, product, and quotient
of the content of registers in two PLC counter instructions.
The Allen-Bradley fixed SLC 500 PLC does not have the square or
the square root math instructions. The fixed SLC 500 does have many
other math instructions that can be used in programming. The usual
interval to update the results of PLC arithmetic instructions is one
scan period. In this chapter, examples are used to illustrate the use of
these math instructions in PLC ladder logic diagrams.

225
226 Proarammable Logic Controllers: Hardware and Programming

9 1 0 .2 Addition
When parts on t\ 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.
Add (ADD) instruction: The a d d (AD D ) in stru c tio n calculates the sum of two operands
Instruction that (Source A and Source B). Figure 10-1 displays an add instruction for
calculates the sum of
Source A and Source B. the Allen-Bradley SLC 500 series PLC. Add instructions in SLC 500
series PLCs are displayed in block format.
The add instruction has three operands. Two operands hold the
source data (Source A and Source B), which are added together. Either
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. The
destination must be a register. In Figure 10-1, sources A and B are the
content of integer registers N7:0 and N7:l. The destination register is
N7:2. Example 10-1 illustrates how to use the add instruction to cal­
culate the content of the accumulated register for two PLC counter
instructions. After a math 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). Descriptions of these bits
are listed in Table 10-1.

Example 10-1
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 con­
veyor. Figure 10-3 displays the PLC ladder diagram for Example 10-1.
The green pushbutton (Green_PB) represents the proximity
switch input for counter A. The red pushbutton (Red_PB) represents
the proximity switch input for counter B. Pressing the green push­
button 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:l).

Switch

0000 r
し Add
Source A N7 0
0<
Source B N7 1
0<
Dest N7 2
0<

0001 End

Figure 10-1. Add (ADD) instruction for the Allen-Bradley SLC 500 series PLC.
Chapter 10 PLC Math Instructions 227

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) instructions in rung 0003 enables. Then, both
counter instructions reset to zero.

Bit Address Description


Carry (C) S2:0/0 Set when there is a carry in the
add instruction or a borrow in
the subtraction instruction.
Overflow (〇) S2:0/l Set when the result is too large
to fit in the destination register.
Zero (Z) S2:0/2 Set when the result of the
subtract instruction is zero.
Sign ⑶ S2:0/3 Set when the result is a
negative number.

Table 10-1. Description of bits in the processor status file (S2).

Counter B
input
ConveyorB

Main
| conveyor

Conveyor A

Figure 10-2. Conveyor diagram for Example 10-1.


228 Programmable Logic Controllers: Hardware and Programming

Example 10-1
Green PB Counter A
0 ------CTU
0000 r Count up .■<cu>
J\

. .

Counter C5:0
Preset 1000<
Accum 0<

Red PB Counter B
0 ------CTU
0 001 rし Count up <\c u >
Counter C5:l
Preset 1000< (
\ DN
Accum 0<

Register N2 holds the number of parts in the main conveyor.


•ADD ■
0002 Add
Source A C 5 :0.ACC
0<
Source B C 5 :1.ACC
0<
Dest N7 :2
0<

Counter A
C5:0
0003 — (res )—

Counter B
C5:l
— (res )—

0004

Figure 10-3. PLC ladder logic diagram for Example 10-1.

|10_3 Subtraction
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.
By using sensors on conveyors B and C and the subtract instruction,
Subtract (SUB) you can calculate the number of parts on conveyor A. The s u b tr a c t
instruction: (SU B) in stru c tio n calculates the difference between two sources:
Instruction that calculates
the difference between Source A and Source B. Figure 10-4 displays a subtract instruction
Source A and Source B. for the Allen-Bradley SLC 500 series PLC. Subtract instructions in the
SLC 500 series PLC are displayed in block format.
Chapter 10 PLC Math Instructions 229

Switch
0 ------SUB ------
0000 r
し — Subtract
Source A N7 :0
0<
Source B N7:1
0<
Dest N7 :2
0<

0001 :E n d >

Figure 10-4. Subtract (SUB) instruction for the Allen-Bradley SLC 500 series PLC.

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 Source A. The result is placed in the destination.
Either 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.
In Figure 10-4, Sources A and B are the content of integer registers
N7:0 and N7:l. The destination register is N7:2. Example 10-2 illus­
trates how to use the subtract instruction to calculate the content of
accumulated register for two counter instructions.

Example 10-2
Figure 10-5 shows a section of a manufacturing plant with three
conveyors. It is not convenient to install and m aintain a proximity
switch in conveyor B. Therefore, the number of parts in conveyor A
are subtracted from the number of parts in conveyor C to calculate the
number of parts in conveyor B. Figure 10-6 displays the PLC ladder
diagram for Example 10-2.
The green pushbutton (Green_PB) represents the proximity
switch input for counter A. The red pushbutton (Red_PB) represents
the proximity switch input for counter C. Pressing the green push­
button 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:l).
The subtract instruction in rung 0002 places the difference of the con­
tent 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) instructions in rung 0003 enables.
Then, both counter instructions reset to zero.
230 Programmable Logic Controllers: Hardware and Programming

Conveyoi

Counter C
input

o
| G 〇nv©y〇r
C

Counter A
input

Conveyor
A

Figure 10-5. Conveyor diagram for Example 10-2.

H10.4 Multiplication
tipie parts ai
W hen multiple are packed in containers, the num ber
of parts per container, the num ber of containers, and the m ul­
tiply instruction w ill give you the total num ber of parts. The
Multiply (MUL) m u ltip ly (M U L) in s tr u c tio n calculates the product of two sources:
instruction: Source A and Source B. Figure 10-7 displays a multiply instruction
Instruction that calculates
the product of Source A
for the A llen-Bradley SLC 500 series PLC. Multiply instructions in
and Source B. SLC 500 series PLCs are displayed in block format.
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 des­
tination. Either 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.
In Figure 10-7, Sources A and B are the content of integer regis­
ters N7:0 and N7:l. The destination register is N7:2. (Notice that if
both sources contain the same number, then the multiply instruction
would act 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.
Chapter 10 PLC Math Instructions 231

Example 10-2

0000

0001

0002

0003

0004

Figure 10-6. PLC ladder logic diagram for Example 10-2.

Switch
1: 0 ------MUL
0000 Multiply
0 Source A N7 0
0<
Source B N7 1
0<
Dest N7 2
0<

0001 :E n d >

Figure 10-7. Multiply (MUL) instruction for the Allen-Bradley SLC 500 series PLC.
232 Programmable Logic Controllers: Hardware and Programming

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
pushbutton (Red_PB) increments the content of the accumulated register
in counter instruction one (C5:l). The multiply instruction in rung 0002
places the result of multiplication in the integer register N7:2.

E x am p le 10-3
Green PB Counter A
1:0 ------CTU------
0000 I r Count up < cu >
J L
i Counter C5 :0
Preset 100< <DN>
Accum 0<

PB Counter B
0 ------CTU------
r < cu >
0001 し Count up
Counter C5 :1
Preset 100< "dnV
Accum 0<

Register N7:2 holds the result.


MUL ■
0002 Multiply
Source A C5:0.ACC
0<
Source B C5 :1.ACC
0<
Dest N7:2
0<

Counter A
C5:0
0003 — (res )—

Counter B
C5:l
— (r e s )—

0004 End

Figure 10-8. PLC ladder logic diagram for Example 10-3.


Chapter 10 PLC Math Instructions 233

When the content of the accumulated register for either of the coun­
ter instructions is equal to 100, the reset (RES) instructions in rung 0003
enables. Then, both counter instructions reset to zero.

10.5 Division
Let's 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. The divide instruction is the
perfect PLC instruction for this task. The d iv id e (DIV) in struction Divide (DIV)
calculates the integer value that results from dividing Source A by instruction:
Instruction that calculates
Source B. Figure 10-9 displays a divide instruction for the Allen-
the quotient that results
Bradley SLC 500 series PLCs. Divide instructions in SLC 500 series from dividing Source A by
PLCs are displayed in block format. Source B.
The divide instruction has three registers. 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, either 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.
In Figure 10-9, Sources A and B are the content of integer registers
N7:0 and N7:l. The destination register is N7:2. Example 10-4 illus­
trates how to use the divide instruction to 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

Switch
1:0 ----- DIV-----
0000 卜 Divide
Source A N7 :0
0<
Source B N7 :1
0<
Dest N7:2
0<

0001 くEnd ^y-

Figure 10-9. Divide (DIV) instruction for the Allen-Bradley SLC 500 series PLC.
234 Programmable Logic Controllers: Hardware and Programming

Example 10-4
Green一 PB Counter A
1:0 CTU --------------
-------------

0000 I r < cu >


」 L Count up
l Counter C5:0
Preset 100< DN
Accum 0<

Red PB Counter B
0 ------CTU------
0001 r Count up .. (\ CU
L
Counter C5 :1
___ DN
Preset 100<
Accum 0<

Register N7:2 holds the quotient.


DIV-
0002 Divide
Source A C5 0.ACC
0<
Source B C5 l.ACC
0<
Dest N7:2
0<

Counter A
C5:0
0003 — (res )--

Counter B
C5:l
— (res )—

0004 End

Figure 10-10. PLC ladder logic diagram for Example 10-4.

the red pushbutton (Red_PB) increments the content of the accumu­


lated register in counter instruction one (C5:l). The divide instruction
in rung 0002 places the quotient in the integer register N7:2.
When the content of accumulated register for either counter
instructions is equal to 100, the reset (RES) instruction in rung 0003
enables. Then, both counter instructions reset to zero.

_ 1 0 .6 Advanced Math Instructions


There are several advanced math instructions that are available
in the Allen-Bradley SLC 5/03, SLC 5/04, and SLC 5/05 processors.
Chapter 10 PLC Math Instructions 235

Table 10-2 lists most of these advanced math instructions. Advanced


math instructions are used to perform mathematical computations.
The compute (CPT) instruction may be used to solve mathematical
equations. Figure 10-11 illustrates that the first entry in this instruction
is the destination register. The second entry in the compute instruc­
tion must be the equation. In Figure 10-11, the destination register is
the integer register N7:0. The tangent of integer register N7:l is mul­
tiplied to the cosine of integer register N7:2. Example 10-5 illustrates
how you can use the CPT instruction to solve an equation.

Instruction N am e
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) Com pute

Table 10-2. List of advanced math instructions available for S しC


5/03, SLC 5/04, and SLC 5/05 processors.

Compute
1: 1 CPT ■
0000 — 3 Compute
0 Dest N7:0
1746-IB16 0<
Expression (TAN N7:l)* (COS N7:2)

0001 : En d >

Figure 10-11. Using the compute (CPT) instruction to solve an equation. Notice that
the first entry is the destination register.
236 Programmable Logic Controllers: Hardware and Programming

Example 10-5
Given the following equation, you can use the CPT instruction to
solve it.
Y = (A x (In B) x ec) + (tan D)
You must first assign floating point register locations for the
parameters in the equation. Note that integer registers such as N7:0
hold integer numbers. Floating point registers such as F8:0 hold a real
number that has both integer and fractional part. These assigned reg­
isters are listed below.
A = F8:0
B = F8:l
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 1: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 instruc­
tions is displayed in Table 10-3.

Example 10-5

XPY
0000 X To Power of Y
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)+(TAN
F8:3)

0001 End

Figure 10-12. PLC ladder logic diagram for Example 10-5.


Chapter 10 PLC Math Instructions 237

Function N am e Description
CLR (register) Clear Clears the memory location
DDV (source, destination) Double Divide Performs a double divide by dividing
32-bit da ta in S:13 & S:14 by a 16-bit source
DCD (source, destination) Decode 4 to 1 of 16 Decodes a 4-bit number (0-15) in the
source and turns the corresponding bit(s)
on in the destination
NEG (source, destination) Negate Reverses the sign from positive to
negative or from negative to positive
TOD (source, destination) Convert to BCD Converts a 16-bit integer value to BCD
FRD (source, destination) From BCD to Binary Converts a BCD value to a binary value
SQR (source, destination) Square Root Finds the square root of a value

Table 10-3. Other commonly used math instructions.

10.7 LogixPro Simulator Examples


1

Example 10-6
Figure 10-13 shows a ladder logic diagram programmed with
various math functions. The G-PB (1:1/0) and R-PB (1:1/1) inputs are
used to control counter up functions on rungs 000 and 001. Each time
one of these inputs is closed its corresponding counter up function
increments its accumulator register (C5:0 or C5:l). These counter up
functions allow the user to select two numbers and store them in the
counter accumulator registers to use with the math functions.
The inputs at addresses 1:1/2,1:1/3,1:1/4,1:1/5 and 1: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 1:1/7 resets the accu­
mulated registers (C5:0.acc and C5:l.acc) of both counters to zero. Each
math function holds its calculated answer until a new set of numbers
is calculated.
238 Programmable Logic Controllers: Hardware and Programming

Figure 10-13. PLC ladder logic diagram for Example 10-6. Using math functions for data manipulation.
Chapter 10 PLC Math Instructions 239

Example 10-7
Figure 10-14 shows data conversion programming in a laaaer logic
diagram for TOD and FRD data conversion functions and their corre­
sponding input/output (I/O) configurations. Input module three (1:3)
provides a 16-bit integer value (0000000000000101) to the TOD function,
which converts that value to a BCD value displayed in output 0 :6 . Clos­
ing the NO switch G-PB at address 1:1/0 initiates the conversion of the
1:3 input into BCD. Output module six (0:6) displays the BCD value by
energizing its individual outputs (0:6/0 through 0:6/15), which also feed
a 7-segment display that shows the decimal form of the number (5).
The thumbwheel connected to input module five (1:5) is used to
enter a 4-digit decimal number (21) that is converted to BCD (00100001)
in the individual inputs (1:5/0 through 1:5/15). This BCD number is
entered into the FRD function. Closing the NO switch R-PB at address
1:1/1 converts the 1:5 BCD number to binary and displays it on LEDs
connected to output module four (0 :4 ): 0000000000010101.

E xam p le 7
L.Ju

I/O Sim ulator II BCD Sim ulator II interger


^

-TOD-
i ]
t J E

0:2 l:3 0:4


Ea
Ea
令國 EH E
] 000 To BCD
c

Ga
lill ■<f<ye ii Ea BI Source
l

ia l
3
S
E
I

s
l

l
pa <rb- m o o a
Ea
CD Ga Dest
El ca ■<S^o-IBl nig Q
E
Ea
i

ml
el
Ea
El
P?i -6^0- d -8-E3
Ea
a

I l
l

E
pa -<To- pa E3
a

I
!
Convert from BCD to binary
PT3 -<^<yera ca tz
办k ふ '^ K ^ul

^
I
I

E
l

I 1 :1/1
|

m m h®- E9 Ea
001
l

i
l

l
画 EIH 画 PI3

iB
Ka
fiEl -<So ESI EO 财
Dl
Ka
nil <fo- EDI 皿 Da
En KQ
Ka
m ■<To- KOI ED m
l

E
K

m -6'o- m EB EB 002 ■<End:


ED
_ B1 田 KS Ea ES
III ^5*o- m KD IQ
iH ■6^0Da iH Em

F ig ure 10-14. PLC ladder logic diagram and input/output (I/O) configuration for Example 10-7. Using
PLC data conversion functions.
240 Programmable Logic Controllers: Hardware and Programming

Summary
The add, subtract, multiply, and divide instructions each have
three operands: two for source data and one for the destination.
The add instruction calculates the sum of Source A and Source B
and places this value in the destination register.
The subtract instruction calculates the difference between Source A
and Source B and places this value in the destination register.
The multiply instruction calculates the product of Source A and
Source B and places this value in the destination register.
The divide instruction calculates the integer value that results
from dividing Source A by Source B and places this value in
the destination register.
Advanced math instructions, such as the compute (CPT)
instruction, can be used to solve mathematical equations.
1

Review Questions
1 . How many operands does an add instruction have?
2. Should the destination of an add instruction be a register? Why?
3. Can both sources in an add instruction be numbers? Why?
4. Can both sources in an add instruction be registers? Why?
5. Can both sources in a subtract instruction be numbers? Why?
6. Can both sources in a divide instruction be registers? Why?
7. In a multiply instruction, what is the largest number that can
be held in the destination register? (Hint: Think about the
destination register.)
8. Can the destination for a subtract instruction be a number? Why?

Complete each o f the following sentences with the correct word(s).


9. An add instruction h a s operands.
10. T h e in an add instruction must be a register.
1 1 . At least one of th e in a multiply instruction must be a
register.
12. T h e in a subtract instruction must be a register.
13. In a subtract instruction, Sou rce is deducted from Source

Specify if the following statements are true or false


14. Source A and Source B in an add instruction can be registers.
15. The destination location for a subtract instruction must be a
register.
Chapter 10 PLC Math Instructions 241

Using the timer instructions, draw the PLC ladder logic diagram fo r the
following problems.
16. Use the compute (CPT) instruction to implement the following
equation:
Ri _ R 2

where, Tl _ ^
Rx represents the resistance value for temperature
R2 represents the resistance value for temperature t2
T\ is temperature in degree Kelvin, = ta +273
T2 is temperature in degree Kelvin, T2 = t2 +273
Assume that three counters provide the variables labeled Rlr tlr
and t2. Your PLC program should calculate R2.
17. Write a program that w ill add the content of two counters every
45 seconds and place the result in an integer register.
18. 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. Write a
program that uses multiply and add instructions to calculate the
sum of parts.
19. Write a program using add, subtract, multiply, and divide
instructions to calculate the value of the following equation:
Y= [p (U /A ,)] + [p (L2/A2)]
242 Programmable Logic Controllers: Hardware and Programming

w e lc o m e to
P it
Learn,ng
Thな C-

M a k e T h e L e a rn in g P it S to p N u m b e r O n e for PLC a n d L a d d e r L o gic S im u la to rs, P ro g ra m m a b le


S o ftw a re S im u la tio n s, C o u rs e M ate ria ls, C u rricu lu m a n d S u p p o rt F o r T ra d e s and T e c h n o lo g y
Training.

P r o fe ss o r B ill's PLC S im u la to rs an d S o ftw a re S im u la tio n s:


PLC T rainin g .... H o w To Get S ta rte d 1

L o q ixP ro .... A llen B ra d le y R S L o g ix S im u la to r UU^m asM X^vm uM k


T o d a y 's M o s t A d v a n c e d P LC T r a in in g S im u la t o r !

L o a ix P ro C D / K e v Edition .... N ow A va ila b le!


A lle n B r a d le y R S L o g ix T r a in in g A n y w h e r e , A n y t im e !

L o g ix P rQ / R S L o g lx .... D o c u m e n ta tio n a n d S tu d e n t E x e rc is e s
T o o ls a n d R e s o u r c e s f o r A lle n B r a d le y R S L o g ix T r a in in g !

P S IM .... S tu d e n t E x e rc is e s and D o c u m e n ta tio n

The Learning Pit is a website offering PLC tutorials, basic electronics information, and related content.

You might also like