ch-11 - Math Functions
ch-11 - Math Functions
Math Instructions
Chapter Objectives
Most PLCs have arithmetic function capabilities. After completing this chapter, you will be able to:
Basic PLC math instructions include add, sub- 11.1 Analyze and interpret math instructions as they apply
tract, multiply and divide to calculate the sum, to a PLC program
difference, product, and quotient of the content 11.2 Create PLC programs involving math instructions
of word registers. The PLC is capable of doing 11.3 Apply combinations of PLC arithmetic functions to
many arithmetic operations per scan period for processes
fast updating of data. This chapter covers the
basic mathematical instructions performed by
PLCs and their applications.
226
Figure 11-1 Compute/Math menu tab. Figure 11-3 SLC 500 ADD instruction.
ADD
ADD
Source A C5:0.ACC
250
Reset Source B C5:1.ACC
Destination N7:1
PL1
GEQ
GREATER THAN OR EQUAL
Source A N7:1
Source B 350
Reset C5:0
RES
C5:1
RES
• The GEQ instruction and PL1 output will be true Input Ladder logic program
whenever the accumulated values in the two counters L1 SW
SUB
are equal to or greater than the constant value 350. SUBTRACT
SW
• A reset button is provided to reset the accumulated Source A N7:10
count of both counters to zero. 520
Source B N7:05
When performing math functions, care must be taken 322
Destination N7:20
to ensure that values remain in the range that the data
198
table or file can store; otherwise, the overflow bit will be
set. The arithmetic status bits for the SLC 500 controller Figure 11-6 SLC 500 SUB (subtract) instruction.
are found in word 0, bits 0 to 3 of the processor status
file S2 (Figure 11-5). After an instruction is executed, the
arithmetic status bits in the status file are updated. The • The answer (198) is stored at the destination ad-
description of each bit can be summarized as follows: dress, N7:20.
• Source A and source B can be either values or ad-
Carry (C)—Address S2:0/0, is set to 1 when there is
dresses that contain values, but A and B cannot both
a carry in the ADD instruction or a borrow in the SUB
be constants.
instruction.
Overflow (O)—Address S2:0/1, is set to 1 when the The program of Figure 11-7 shows how the SUB func-
result is too large to fit in the destination register. tion can be used to indicate a vessel overfill condition.
Zero (Z)—Address S2:0/2, is set to 1 when the result This application requires an alarm to sound when a supply
of the subtract instruction is zero. system leaks 5 lb or more of raw material into the ves-
sel after a preset weight of 500 lb has been reached. The
Sign (S)—Address S2:0/3, is set to 1 when the result
operation of the program can be summarized as follows:
is a negative number.
• When the start button is pressed, the fill solenoid
(rung 1) and filling indicating light (rung 2) are turned
11.3 Subtraction Instruction on and raw material is allowed to flow into the vessel.
The SUB (subtract) instruction is an output instruction that • The vessel has its weight monitored continuously by
subtracts one value from another and stores the result in the PLC program (rung 3) as it fills.
the destination address. When rung conditions are true, the • When the weight reaches 500 lb, the fill solenoid is
subtract instruction subtracts source B from source A and de-energized and the flow is cut off.
stores the result in the destination. Figure 11-6 shows the
• At the same time, the filling pilot light indicator is
SUB instruction used with the SLC 500 controllers. The
turned off and the full pilot light indicator (rung 3)
operation of the logic rung can be summarized as follows:
is turned on.
• When input switch SW is closed the rung will be true. • Should the fill solenoid leak 5 lb or more of raw
• The value stored at the source B address, N7:05 material into the vessel, the alarm (rung 5) will en-
(322), is subtracted from the value stored at the ergize and stay energized until the overflow level is
source A address, N7:10 (520). reduced below the 5-lb overflow limit.
1
Fill
Start Fill solenoid
solenoid
Start
Stop Fill
solenoid Full Filling
2
Stop
GEQ Full
3 GREATER THAN OR EQUAL Filling
Source A Ι:012
Source B 500
Ι:012 Full
Source B 500
Destination N7:1
Alarm
GEQ
5 GREATER THAN OR EQUAL
Source A N7:1
Source B 5
11.4 Multiplication Instruction • The data in source A (constant 20) will be multi-
plied by the data in source B (accumulated value of
The multiply (MUL) instruction is an output instruction counter C5:10).
that multiplies two values and stores the result in the des-
• The resultant answer is placed in the destination
tination address. Figure 11-8 shows the MUL instruction
N7:2.
used with the SLC 500 controllers. The operation of the
logic rung can be summarized as follows: • Similar to previous math instructions, source A and
B in multiplication instructions can be values (con-
• When input switch SW is closed the rung will be stants) or addresses that contain values, but A and B
true. cannot both be constants.
Input Ladder logic program The program of Figure 11-9 is an example of how
L1 SW MUL instruction calculates the product of two sources.
MUL
MULTIPLY
The operation of the program can be summarized as
SW
Source A 20 follows:
Source B C5:10.ACC • When input switch SW is closed the MUL instruc-
tion is executed.
Destination N7:2
• The value stored in source A, address N7:1 (123),
is then multiplied by the value stored in source B,
Figure 11-8 SLC 500 MUL (multiply) instruction. address N7:2(61).
PL1
EQU
EQUAL
Source A N7:3
7503
Source B 7503
Figure 11-9 MUL instruction used to calculate the product of two sources.
• The product (7503) is placed into destination word of the DIV instruction. The operation of the logic rung
N7:3. can be summarized as follows:
• As a result, the equal instruction becomes true, turn- • When input switch SW is closed the rung will be true.
ing output PL1 on.
• The data in source A (the accumulated value of
The program of Figure 11-10 is an example of how the counter C5:10) is then divided by the data in source B
MUL instruction is used as part of an oven temperature (the constant 2).
control program. The operation of the program can be • The result is placed in the destination N7:3.
summarized as follows: • If the remainder is 0.5 or greater, a roundup occurs
• The PLC calculates the upper and lower deadband, in the integer destination.
or off/on limits, about the set-point. • The value stored in the math register consists of the
• Upper and lower temperature limits are set automati- unrounded quotient (placed in the most significant
cally at 61 percent regardless of the set-point value. word) and the remainder (placed in the least signifi-
• Set-point temperature is adjusted by means of the cant word).
thumbwheel switch. • Some PLCs support the use of floating-decimal as
• The analog thermocouple interface module is used well as integer (whole number) values. As an exam-
to monitor the current temperature of the oven. ple, 10 divided by 3 may be expressed as 3.333333
(floating-decimal notation) or 3 with a remainder
• In this example, the set-point temperature is 400°F.
of 1.
• Therefore, the electric heaters will be turned on when
the temperature of the oven drops to less than 396°F The program of Figure 11-12 is an example of how the
and stay on until the temperature rises above 404°F. DIV instruction calculates the integer value that results
• If the set-point is changed to 100°F, the deadband from dividing source A by source B. The operation of the
remains at 61 percent, with the lower limit being program can be summarized as follows:
99°F and the upper limit being 101°F. • When input switch SW is closed the DIV instruc-
• The number stored in word N7:1 represents the tion is executed.
upper temperature limit, and the number stored in • The value stored in source A, address N7:0 (120),
word N7:2 represents the lower limit. is then divided by the value stored in source B,
address N7:1 (4).
11.5 Division Instruction • The answer, 30, is placed in the destination address
The divide (DIV) instruction divides the value in source A N7:5.
by the value in source B and stores the result in the desti- • As a result, the equal instruction becomes true, turn-
nation and math register. Figure 11-11 shows an example ing output PL1 on.
SUB PL2
Ι:013 SUBTRACT
Source A Ι:012
400
Thermocouple Source B N7:0
input 4
Low limit Destination N7:2
396
PL1
LES
LESS THAN
Source A Ι:013
0
Source B N7:2
396
PL 2
GRT
GREATER THAN
Source A Ι:013
0
N7:1
Source B 404
Heater
Figure 11-10 The MUL instruction used as part of a temperature control program.
Input Ladder logic program The program of Figure 11-13 is an example of how
L1 SW the DIV function is used as part of a program to convert
DIV
DIVIDE
Celsius temperature to Fahrenheit. The operation of the
SW
Source A C5:10.ACC program can be summarized as follows:
Source B 2 • The thumbwheel switch connected to the input
module indicates Celsius temperature.
Destination N7:3
• The program is designed to convert the recorded
Celsius temperature in the data table to Fahrenheit
Figure 11-11 SLC 500 DIV (divide) instruction. values for display.
SW
SW PL1
DIV
DIVIDE
Source A N7:0
120
Source B N7:1
4
Destination N7:5
30
PL1
EQU
EQUAL
Source A N7:5
30
Source B 30
Figure 11-12 DIV instruction used to calculate the value that results
from dividing source A by source B.
L1
Input Ladder logic program Output
L2
• Next, the DIV instruction divides 5 into the 540 and
stores the answer (108) in address N7:1.
MUL • Finally, the ADD instruction adds 32 to the value of
1 MULTIPLY
Source A Ι:012 108 and stores the sum (140) in address O:13.
2 Ι:012 60 O:013
Source B 9 • Thus 60°C 5 140°F.
3
Thumbwheel Destination N7:0 LED
switch
540
Display
11.6 Other Word-Level Math
DIV Instructions
0 6 0 DIVIDE 14 0
Source A N7:0 The program of Figure 11-14 is an example of the square
Celsius 540 Fahrenheit root (SQR) instruction. The operation of the logic rung
Source B 5
can be summarized as follows:
Destination N7:1
108 • When input switch SW is closed the SQR instruc-
ADD tion is executed.
ADD
Source A N7:1 • The number whose square root we want to deter-
108 mine (144) is placed in the source.
Source B 32
• The function calculates the square root and places it
Destination O:013
140 (12) in the destination.
• If the value of the source is negative, the instruction
Figure 11-13 Program for converting Celsius temperature will store the square root of the absolute (positive)
to Fahrenheit.
value of the source at the destination.
• The following conversion formula forms the basis Input Ladder logic program
for the program: L1 SW
SQR
(5 )
9 × C 1 32
F 5 __ SW SQUARE ROOT
Source A N7:101
144
• In this example, a current temperature reading of Destination N7:105
60°C is assumed. 12
Figure 11-15 SLC 500 NEG (negate) instruction. Figure 11-17 SLC 500 TOD (convert to BCD) instruction.
The program of Figure 11-15 is an example of the Figure 11-17 is an example of the TOD instruction. The
negate (NEG) instruction. This math function changes operation of the logic rung can be summarized as follows:
the sign of the source value from positive to negative.
• When input switch SW is closed the TOD instruc-
The operation of the logic rung can be summarized as
tion is executed.
follows:
• The binary bit pattern at the source address N7:23 is
• When input switch SW is closed the NEG instruc- converted into a BCD bit pattern of the same deci-
tion is executed. mal value at the destination address O:20.
• The positive value 101 stored at the source address • The source displays the value 10, which is the
N7:52 is negated to 2101 and stored in destination correct decimal value; however, the destination
address N7:53. displays the value 16.
• Positive numbers will be stored in straight binary • The processor interprets all bit patterns as binary;
format, and negative numbers will be stored as 2’s therefore the value 16 is the binary interpretation of
complement. the BCD bit pattern.
The program of Figure 11-16 is an example of the clear • The bit pattern for 10 BCD is the same as the bit
(CLR) instruction. The operation of the logic rung can be pattern for 16 binary.
summarized as follows:
The convert from BCD (FRD) instruction is used to con-
• When input switch SW is closed the CLR instruc- vert binary-coded decimal (BCD) values to integer values.
tion is executed. This instruction could be used to convert data from a BCD
• Upon execution it sets all bits of a word to zero. external source, such as a BCD thumbwheel switch, to the
• In this example it changes the value of all bits stored binary format in which the processor operates. The program
in the destination address N7:22 to 0. of Figure 11-18 is an example of the FRD instruction. The
operation of the logic rung can be summarized as follows:
The convert to BCD (TOD) instruction is used to
convert 16-bit integers into binary-coded decimal • When input switch SW is closed the FDR instruc-
(BCD) values. This instruction could be used when tion is executed.
transferring data from the processor (which stores data • The BCD bit pattern stored at the source address
in binary format) to an external device, such as an LED I:30 is converted into a binary bit pattern of the same
display, that functions in BCD format. The program of decimal value at the destination address, N7:24.
Figure 11-19 SLC 500 SCL (scale) instruction. #N7:25 #N7:50 #N7:100
N7:25 25 + N7:50 50 = N7:100 75
234 + 22 256
The scale data (SCL) instruction is used to allow very 1256 + 456 1712
large or very small numbers to be enlarged or reduced by N7:28 77 + N7:53 100 = N7:103 177
the rate value. When rung conditions are true, this instruc-
Figure 11-20 SLC 500 file add function of the FAL
tion multiplies the source by a specified rate. The rounded
instruction.
result is then added to an offset value and placed in the
destination. The program of Figure 11-19 is an example
of the SCL instruction. The operation of the logic rung
can be summarized as follows: data in file address N7:25 to the data stored in file
• When input switch SW is closed the SCL instruc- address N7:50 and store the result in file address
tion is executed. N7:100.
• The number 100 stored at the source address, N7:0, • The rate per scan is set at All, so the instruction
is multiplied by 25,000, divided by 10,000, and goes to completion in one scan.
added to 127. The program of Figure 11-21 is an example of the file
• The result, 377, is placed in the destination address, subtract function of the FAL instruction. The operation of
N7:1. the logic rung can be summarized as follows:
You can use SCL instruction to scale data from your • When input switch SW is closed the rung goes true
analog module and bring it into the limits prescribed by the and the processor subtracts a program constant
process variable or another analog module. For instance, (255) from each word of file address N10:0 and
you can use the SCL instruction to convert a 4–20 mA
input signal to a PID process variable, or to scale an ana-
log input to control an analog output.
Input Ladder logic program
L1
11.7 File Arithmetic Operations SW
FAL
SW FILE ARITH/LOGICAL EN
File arithmetic functions include file add, file subtract, Control R6:5 DN
file multiply, file divide, file square root, file convert from Length 4
BCD, and file convert to BCD. The file arithmetic and Position 0 ER
logic (FAL) instruction can combine an arithmetic opera- Mode 2
Destination #N7:255
tion with file transfer. The arithmetic operations that can
be implemented with the FAL are ADD, SUB, MULT, Expression
DIV, and SQR. #N10:0 – 255
The file add function of the FAL instruction can be
#N10:0 #N7:255
used to perform addition operations on multiple words.
The program of Figure 11-20 is an example of the file add N10:0 680 – 255 = N7:255 425
950 695
function of the FAL instruction. The operation of the logic 20 –235
rung can be summarized as follows: N10:3 100 = N7:258 –155
• When input switch SW is closed the rung goes true Figure 11-21 SLC 500 file subtract function of the FAL
and the expression tells the processor to add the instruction.
Expression Expression
#N7:330 * N7:23 #F8:20 / #F8:100
stores the result at the destination file address, • The rate per scan is set at All, so the instruction
N7:255. goes to completion in one scan.
• The rate per scan is set at 2, so it will take 2 scans The program of Figure 11-23 is an example of the file
from the moment the instruction goes true to com- divide function of the FAL instruction. The operation of
plete its operation. the logic rung can be summarized as follows:
The program of Figure 11-22 is an example of the file • When input switch SW is closed the rung goes true
multiply function of the FAL instruction. The operation of and the data in file address F8:20 is divided by the
the logic rung can be summarized as follows: data in file address F8:100, with the result stored in
• When input switch SW is closed the rung goes true element address F8:200.
and the data in file address N7:330 is multiplied by • The mode is Incremental, so the instruction operates
the data in element address N7:23, with the result on one set of elements for each false-to-true transi-
stored at the destination file address N7:500. tion of the instruction.
1. Explain the function of math instructions as applied destination if N7:3 contains a value of 15 and N7:4
to the PLC. contains a value of 4?
2. Name the four basic math functions performed by 9. With reference to the instruction of Figure 11-28,
PLCs. what is the value of the number stored at N7:20 if
3. What standard format is used for PLC math N7:3 contains a value of 2345?
instructions? 10. With reference to the instruction of Figure 11-29,
4. Would math instructions be classified as input or what will be the value of each of the bits in word
output instructions? B3:3 when the rung goes true?
5. With reference to the instruction of Figure 11-24, 11. With reference to the instruction of Figure 11-30,
what is the value of the number stored at source B what is the value of the number stored at N7:101?
if N7:3 contains a value of 60 and N7:20 contains a 12. With reference to the instruction of Figure 11-31,
value of 80? list the values that will be stored in file #N7:10
6. With reference to the instruction of Figure 11-25, when the rung goes true.
what is the value of the number stored at the desti-
nation if N7:3 contains a value of 500?
NEG
7. With reference to the instruction of Figure 11-26, NEGATE
what is the value of the number stored at the des- Source N7:3
tination if N7:3 contains a value of 40 and N7:4 Destination N7:20
contains a value of 3?
Figure 11-28 Instruction for Question 9.
8. With reference to the instruction of Figure 11-27,
what is the value of the number stored at the CLR
CLEAR
ADD Destination B3:3
ADD 0000111100001111
Source A N7:3
Source B N7:4
Destination N7:20 Figure 11-29 Instruction for Question 10.
Figure 11-27 Instruction for Question 8. Figure 11-31 Instruction for Question 12.
S2 S2
CTU
COUNT-UP COUNTER CU
Counter C5:1
DN
Preset 350
Reset Accumulated 0
ADD
ADD
Source A C5:0.ACC
Source B C5:1.ACC
Destination N7:1
PL1
GEQ
GREATER THAN OR EQUAL
Source A N7:1
Source B 350
Reset C5:0
RES
C5:1
RES
1
Fill
Start Fill solenoid
solenoid
Fill Filling
Stop solenoid Full Filling
2
Full
GEQ Full
Ι:012
3 GREATER THAN OR EQUAL
Source A Ι:012
Weight (All 16 bits) Alarm
transducer Source B 500
Full
SUB
4 SUBTRACT
Source A Ι:012
Source B 500
Destination N7:1
Alarm
GEQ
5 GREATER THAN OR EQUAL
Source A N7:1
Source B 5
d. Assume that the vessel is filled to a weight of c. Assume that the set-point temperature is 600°F
510 lb. State the value of the number stored in and the thermocouple input module indicates a
each of the following words for this condition: temperature of 608°F. What is the status (energized
(1) I:012 or not energized) of each of the following outputs?
(2) N7:1 (1) PL1
e. With the vessel filled to a weight of 510 lb, state (2) PL2
the status of each of the logic rungs (true or (3) Heater
false). 4. With reference to the Celsius to Fahrenheit con-
3. Answer the following with reference to the tem- version program shown in Figure 11-35, state the
perature control program shown in Figure 11-34. value of the number stored in each of the following
a. Assume that the set-point temperature is 600°F. words for a thumbwheel setting of 035:
At what temperature will the electric heaters be a. I:012
turned on and off? b. N7:0
b. Assume that the set-point temperature is 600°F c. N7:1
and the thermocouple input module indicates a d. O:013
temperature of 590°F. What is the value of the
number stored in each of the following words at 5. Design a program that will add the values stored
this point? at N7:23 and N7:24 and store the result in N7:30
(1) I:012 whenever input A is true, and then, when input B is
(2) I:013 true, will copy the data from N7:30 to N7:31.
(3) N7:0 6. Design a program that will take the accumulated
(4) N7:1 value from TON timer T4:1 and display it on a
(5) N7:2 4-digit, BCD format set of LEDs. Use address
PL1
LES
LESS THAN
Source A Ι:013
0
Source B N7:2
396
PL 2
GRT
GREATER THAN
Source A Ι:013
0
N7:1
Source B 404
Heater
O:023 for the LEDs. Include the provision to • Multiply the value in N7:3 by 25 and store the
change the preset value of the timer from a set of result in N7:4.
4-digit BCD thumbwheels when input A is true. • Divide the value in N7:4 by 35 and store the
Use address I:012 for the thumbwheels. result in F8:0.
7. Design a program that will implement the follow- 8. a. There are three part conveyor lines (1-2-3) feed-
ing arithmetic operation: ing a main conveyor. Each of the three conveyor
• Use a MOV instruction and place the value 45 in lines has its own counter. Construct a PLC pro-
N7:0 and 286 in N7:1. gram to obtain the total count of parts on the
• Add the values together and store the result in N7:2. main conveyor.
• Subtract the value in N7:2 from 785 and store the b. Add a timer to the program that will update the
result in N7:3. total count every 30 s.
Figure 11-36 Program for Problem 9. Figure 11-37 Program for Problem 10.