# PLC Math Instructions

Most PLCs have arithmetic function capabilities. Basic PLC math instructions include add, subtract, multiply and divide to calculate the sum, difference, product, and quotient of the content of word registers. The PLC is capable of doing many arithmetic operations per scan period for fast updating of data. This article covers the basic mathematical instructions performed by PLCs and their applications.

# PLC Math Instructions

Math instructions, like data manipulation instructions, enable the programmable controller to take on more of the qualities of a conventional computer. The PLC’s math functions capability allows it to perform arithmetic functions on values stored in memory words or registers.

For example, assume you are using a counter to keep track of the number of parts manufactured, and you would like to display how many more parts must be produced in order to reach a certain quota. This display would require the data in the accumulated value of the counter to be subtracted from the quota required.

Other applications include combining parts counted, subtracting detected defects, and calculating run rates. Depending on what type of processor is used, various math instructions can be programmed. The basic four mathematical functions performed by PLCs are:

• Addition: The capability to add one piece of data to another.
• Subtraction: The capability to subtract one piece of data from another.
• Multiplication: The capability to multiply one piece of data by another.
• Division: The capability to divide one piece of data by another.

Math instructions use the contents of two words or registers and perform the desired function. The PLC instructions for data manipulation (data transfer and data compare) are used with the math symbols to perform math functions. Math instructions are all output instructions.

Figure 1 shows the Compute/Math menu tab for the SLC 500 PLC and its associated RSLogix software. The commands can be summarized as follows:

• CPT (Compute): Evaluates an expression and stores the result in the destination.
• SUB (Subtract): Subtracts source B from source A and stores the result in the destination.
• MUL (Multiply): Multiplies source A by source B and stores the result in the destination.
• DIV (Divide): Divides source A by source B and stores the result in the math register.
• SQR (Square Root): Calculates the square root of the source and places the integer result in the destination.
• NEG (Negate): Changes the sign of the source and places it in the destination.
• TOD (To BCD): Converts a 16-bit integer source value to BCD and stores it in the math register or the destination.
• FRD (From BCD): Converts a BCD value in the math register or the source to an integer and stores it in the destination.

Figure 2 shows the CPT (compute) instruction used with SLC 500 controllers.

When CPT 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 execution time of a CPT instruction is longer than that of a single arithmetic operation and uses more instruction words.

Most math instructions take two input values, perform the specified arithmetic function, and output the result to an assigned memory location. For example, the ADD instruction performs the addition of two values stored in the referenced memory locations. How these values are accessed depends on the controller.

Figure 3 shows the ADD instruction used with the SLC 500 controllers. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the rung will be true.
• The value stored at the source A address, N7:0 (25), is added to the value stored at the source B address, N7:1 (50).
• Source A and source B can be either values or addresses that contain values, but A and B cannot both be constants.

The program of Figure 4 illustrates how the ADD instruction can be used to add the accumulated counts of two up-counters. This application requires a pilot light to come on when the sum of the counts from the two counters is equal to or greater than 350. The operation of the program can be summarized as follows:

• Source A of the ADD instruction is addressed to store the accumulated value of counter C5:0.
• Source B of the ADD instruction is addressed to store the accumulated value of counter C5:1.
• The value at source A is added to the value at source B, and the result (answer) is stored at destination address N7:1.
• Source A of the GEQ (greater than or equal) instruction is addressed to store the value of the destination address N7:1.
• Source B of the GEQ instruction contains the constant value of 350.
• The GEQ instruction and PL1 output will be true whenever the accumulated values in the two counters are equal to or greater than the constant value 350.
• A reset button is provided to reset the accumulated count of both counters to zero.

When performing math functions, care must be taken to ensure that values remain in the range that the data table or file can store; otherwise, the overflow bit will be set. The arithmetic status bits for the SLC 500 controller are found in word 0, bits 0 to 3 of the processor status file S2 ( Figure 5 ).

After an instruction is executed, the arithmetic status bits in the status file are updated. The description of each bit can be summarized as follows:

• 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.

## Subtraction Instruction

The SUB (subtract) instruction is an output instruction that subtracts one value from another and stores the result in the destination address. When rung conditions are true, the subtract instruction subtracts source B from source A and stores the result in the destination.

Figure 6 shows the SUB instruction used with the SLC 500 controllers. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the rung will be true.
• The value stored at the source B address, N7:05 (322), is subtracted from the value stored at the source A address, N7:10 (520).
• Source A and source B can be either values or addresses that contain values, but A and B cannot both be constants.

## PLC Math Instructions

The program of Figure 7 shows how the SUB function can be used to indicate a vessel overfill condition. This application requires an alarm to sound when a supply system leaks 5 lb or more of raw material into the vessel after a preset weight of 500 lb has been reached. The operation of the program can be summarized as follows:

• When the start button is pressed, the fill solenoid (rung 1) and filling indicating light (rung 2) are turned on and raw material is allowed to flow into the vessel.
• The vessel has its weight monitored continuously by the PLC program (rung 3) as it fills.
• When the weight reaches 500 lb, the fill solenoid is de-energized and the flow is cut off.
• At the same time, the filling pilot light indicator is turned off and the full pilot light indicator (rung 3) is turned on.
• Should the fill solenoid leak 5 lb or more of raw material into the vessel, the alarm (rung 5) will energize and stay energized until the overflow level is reduced below the 5-lb overflow limit.

## Multiplication Instruction

The multiply (MUL) instruction is an output instruction that multiplies two values and stores the result in the destination address. Figure 8 shows the MUL instruction used with the SLC 500 controllers. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the rung will be true.
• The data in source A (constant 20) will be multiplied by the data in source B (accumulated value of counter C5:10).
• The resultant answer is placed in the destination N7:2.
• Similar to previous math instructions, source A and B in multiplication instructions can be values (constants) or addresses that contain values, but A and B cannot both be constants.

The program of Figure  9 is an example of how MUL instruction calculates the product of two sources. The operation of the program can be summarized as follows:

• When input switch SW is closed the MUL instruction is executed.
• The value stored in source A, address N7:1 (123), is then multiplied by the value stored in source B, address N7:2(61).
• The product (7503) is placed into destination word N7:3.
• As a result, the equal instruction becomes true, turning output PL1 on.

The program of Figure 10 is an example of how the MUL instruction is used as part of an oven temperature control program. The operation of the program can be summarized as follows:

• The PLC calculates the upper and lower deadband, or off/on limits, about the set-point.
• Upper and lower temperature limits are set automatically at 61 percent regardless of the set-point value.
• Set-point temperature is adjusted by means of the thumbwheel switch.
• The analog thermocouple interface module is used to monitor the current temperature of the oven.
• In this example, the set-point temperature is 400°F.
• Therefore, the electric heaters will be turned on when the temperature of the oven drops to less than 396°F and stay on until the temperature rises above 404°F.
• If the set-point is changed to 100°F, the deadband remains at 61 percent, with the lower limit being 99°F and the upper limit being 101°F.
• The number stored in word N7:1 represents the upper temperature limit, and the number stored in word N7:2 represents the lower limit.

## Division Instruction

The divide (DIV) instruction divides the value in source A by the value in source B and stores the result in the destination and math register. Figure 11 shows an example of the DIV instruction. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the rung will be true.
• The data in source A (the accumulated value of counter C5:10) is then divided by the data in source B (the constant 2).
• The result is placed in the destination N7:3.
• If the remainder is 0.5 or greater, a roundup occurs in the integer destination.
• The value stored in the math register consists of the unrounded quotient (placed in the most significant word) and the remainder (placed in the least significant word).
• Some PLCs support the use of floating-decimal as well as integer (whole number) values. As an example, 10 divided by 3 may be expressed as 3.333333 (floating-decimal notation) or 3 with a remainder of 1.

The program of Figure 12 is an example of how the DIV instruction calculates the integer value that results from dividing source A by source B. The operation of the program can be summarized as follows:

• When input switch SW is closed the DIV instruction is executed.
• The value stored in source A, address N7:0 (120), is then divided by the value stored in source B, address N7:1 (4).
• As a result, the equal instruction becomes true, turning output PL1 on.

The program of Figure  13 is an example of how the DIV function is used as part of a program to convert Celsius temperature to Fahrenheit. The operation of the program can be summarized as follows:

• The thumbwheel switch connected to the input module indicates Celsius temperature.
• The program is designed to convert the recorded Celsius temperature in the data table to Fahrenheit values for display.
• Next, the DIV instruction divides 5 into the 540 and stores the answer (108) in address N7:1.
• Finally, the ADD instruction adds 32 to the value of 108 and stores the sum (140) in address O:13.
• Thus 60°C 5 140°F.

## Other Word-Level Math Instructions

The program of Figure 14 is an example of the square root (SQR) instruction. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the SQR instruction is executed.
• The number whose square root we want to determine (144) is placed in the source.
• The function calculates the square root and places it (12) in the destination.
• If the value of the source is negative, the instruction will store the square root of the absolute (positive) value of the source at the destination.

The program of Figure  15 is an example of the negate (NEG) instruction. This math function changes the sign of the source value from positive to negative. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the NEG instruction is executed.
• The positive value 101 stored at the source address N7:52 is negated to 2101 and stored in destination address N7:53.
• Positive numbers will be stored in straight binary format, and negative numbers will be stored as 2’s complement.

The program of Figure 16 is an example of the clear (CLR) instruction. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the CLR instruction is executed.
• Upon execution it sets all bits of a word to zero.
• In this example it changes the value of all bits stored in the destination address N7:22 to 0.

The convert to BCD (TOD) instruction is used to convert 16-bit integers into binary-coded decimal (BCD) values. This instruction could be used when transferring data from the processor (which stores data in binary format) to an external device, such as an LED display, that functions in BCD format.

The program of Figure 17 is an example of the TOD instruction. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the TOD instruction is executed.
• The binary bit pattern at the source address N7:23 is converted into a BCD bit pattern of the same decimal value at the destination address O:20.
• The source displays the value 10, which is the correct decimal value; however, the destination displays the value 16.
• The processor interprets all bit patterns as binary; therefore the value 16 is the binary interpretation of the BCD bit pattern.
• The bit pattern for 10 BCD is the same as the bit pattern for 16 binary.

The convert from BCD (FRD) instruction is used to convert binary-coded decimal (BCD) values to integer values. This instruction could be used to convert data from a BCD external source, such as a BCD thumbwheel switch, to the binary format in which the processor operates.

The program of Figure 18 is an example of the FRD instruction. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the FDR instruction is executed.
• The BCD bit pattern stored at the source address I:30 is converted into a binary bit pattern of the same decimal value at the destination address, N7:24.

The scale data (SCL) instruction is used to allow very large or very small numbers to be enlarged or reduced by the rate value. When rung conditions are true, this instruction multiplies the source by a specified rate. The rounded result is then added to an offset value and placed in the destination.

The program of Figure 19 is an example of the SCL instruction. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the SCL instruction is executed.
• 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.

You can use SCL instruction to scale data from your analog module and bring it into the limits prescribed by the process variable or another analog module. For instance, you can use the SCL instruction to convert a 4 – 20 mA input signal to a PID process variable, or to scale an analog input to control an analog output.

## File Arithmetic Operations

File arithmetic functions include file add, file subtract, file multiply, file divide, file square root, file convert from BCD, and file convert to BCD. The file arithmetic and logic (FAL) instruction can combine an arithmetic operation with file transfer. The arithmetic operations that can be implemented with the FAL are ADD, SUB, MULT, DIV, and SQR. The file add function of the FAL instruction can be used to perform addition operations on multiple words.

The program of Figure 20 is an example of the file add function of the FAL instruction. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the rung goes true and the expression tells the processor to add the data in file address N7:25 to the data stored in fi le address N7:50 and store the result in file address N7:100.
• The rate per scan is set at All, so the instruction goes to completion in one scan.

The program of Figure 21 is an example of the file subtract function of the FAL instruction. The operation of the logic rung can be summarized as follows:

• When input switch SW is closed the rung goes true and the processor subtracts a program constant (255) from each word of file address N10:0 and stores the result at the destination fi le address, N7:255.
• The rate per scan is set at 2, so it will take 2 scans from the moment the instruction goes true to complete its operation.