Math Instructions Program logic controller Math Instructions Objective Program logic controller Math Instructions 2 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. 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. Program logic controller Math Instructions 3 The basic four mathematical functions performed by PLCs are: Addition The capability to add one piece of data to another. Subtraction capability to subtract one piece of data from another. Multiplication Division The capability to multiply one piece of data by another. The capability to divide one piece of data by another. The Compute/Math menu tab for the SLC 500 PLC and its associated RSLogix software. CPT (Compute)— Evaluates an expression and stores the result in the destination. ADD (Add)— Adds source A to source B 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 and destination. SQR (Square Root)— Calculates the square root of the source and places the integer result in the destination. Program logic controller NEG (Negate)— Changes the sign of the source and places it in the destination. TOD (To BCD)— Converts a 16bit integer source value to BCD and stores it in the math register or the destination. Math Instructions FRD (From BCD)— Converts a BCD value in the math register or the source to an integer and stores it in the destination. CPT (compute) 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. The main advantage of the compute instruction is that it allows you to enter quite complex expressions in one instruction. a ladder rung used to convert a Fahrenheit temperature to a Celsius temperature using a single RSLogix 5000 compute instruction. In this example a temperature of 140°F is programmed into N7:5 of the Expression and the computed value of 60°C appears in the Result. Program logic controller Math Instructions Addition Instruction Most math instructions take two input values, perform the specified arithmetic function, and output the result to an assigned memory location. The program of Next Figure 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. Program logic controller Math Instructions 6 Program logic controller Math Instructions 7 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. The program of next Figure 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. Program logic controller Math Instructions Program logic controller Math Instructions 9 Multiplication Instruction The multiply (MUL) instruction is an output instruction that multiplies two values and stores the result in the destination address. The program of next figure is an example of how the MUL instruction is used as part of an oven temperature control program. • 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 ±1 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. Program logic controller Math Instructions 10 Program logic controller Math Instructions 11 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. DIV instruction to calculate the that results dividing source source B. used value from A by Program logic controller Math Instructions 12 The program of figure is an example of how the DIV function is used as part of a program to convert Celsius temperature to Fahrenheit. • 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. • The following conversion formula forms the basis for the program: • In this example, a current temperature reading of 60°C is assumed. • The PLC processor carries out its internal operations using binary numbers and the FRD instruction is used to convert the 16-bit integer values from the thumbwheel switch into BCD values. • The MUL instruction multiplies the temperature (60°C) by 9 and stores the product (540) in address N7:0. Program logic controller Math Instructions 13 Other Word-Level Math Instructions Square root (SQR) instruction Negate (NEG) instruction This math function changes the sign of the source value from positive to negative Clear (CLR) instruction Program logic controller Math Instructions 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. 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 Scale data (SCL) and Scale with Parameters (SCP) instruction make conversions to analog input and output values to ensure correct interpretation and processing. As a result, data must be scaled, or resized, before it can be used by a PLC control algorithm or output to a field device. Program logic controller Math Instructions 15 y = mx + b Where: y is the output , m is the scaling rate, x is the input, b is the offset Scaling rate (m) = (scaled Max − scaled Min) / (input Max − input Min) Offset (b) = (scaled Min) − (input Min x m) The execution of the instruction can be summarized as follows: • A strain-gage pressure transducer is connected to input I:1.0. • The gage measures pressure from 0 to 1000 psi and provides an analog output of 0 to 10V. • The unscaled range is 0 to 32,767, and the output is loaded into N7:20. • 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. Program logic controller Math Instructions 16 The execution of the instruction can be summarized as follows: • A proportional control valve is connected to the PLC output O:1.0. • A 4 to 20 mA signal operates the valve from closed to 100% open. • The percent open is in location N7:21. • The PLC analog module provides a 4 to 20 mA output signal for a number between from 6,242 to 31,208. • The SCP directs analog output O:1.0 to provide a 4 to 20 mA signal, which is scaled to the valve position based on a number between 0 and 100. Program logic controller Math Instructions 17 Program logic controller Math Instructions 18