Browse
(440) 299-7778
Menu
(440) 299-7778
One-year parts and labor warranty30-day money-back guaranteeContact

The text of a manual · part 13 of 39

Series 90-30/20/Micro PLC CPU Instruction Set Reference Manual: Chapter 6 - Math Functions, pages 106 to 113

Pages 106 to 113 of 337, from Series 90-30/20/Micro PLC CPU Instruction Set Reference Manual

The original document is the PDF (4.4 MB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.

For the GE Fanuc IC692CPU211D / IC692MDR541C, Fanuc IC692CPU211D / IC692MDR541C, GE IC692CPU211D / IC692MDR541C and GE Fanuc IC693CPU311X

Page 106

Chapter

Math Functions

6 This chapter describes the math functions of the Series 90-30/20/Micro Instruction Set:

†

Abbreviation

Function

Description

ADD

Addition

Add two numbers.

Page 6-2

SUB

Subtraction

Subtract one number from another.

6-2

MUL

Multiplication

Multiply two numbers.

6-2

DIV

Division

Divide one number by another, yielding a quotient.

6-2

MOD

Modulo Division

Divide one number by another, yielding a remainder.

6-7

SQRT

Square Root

Find the square root of an integer or real value.

6-9

SIN, COS, TAN, ASIN, ACOS, ATAN

Trigonometric Functions †

Perform the appropriate function on the real value in input IN.

6-11

LOG, LN EXP, EXPT

Logarithmic/Exponential Functions †

Perform the appropriate function on the real value in input IN.

6-13

RAD, DEG

Radian Conversion †

Perform the appropriate function on the real value in input IN.

6-15

Trigonometric Functions, Logarithmic/Exponential Functions, and Radian Conversion functions are only available on the model 35x and 36x series CPUs, Release 9.00 or later, and on all releases of CPU352 and CPU37x.

Note Division and modulo division are similar functions that differ in their output; division finds a quotient, while modulo division finds a remainder.

GFK-0467M

6-1

Page 107

6

Standard Math Functions (ADD, SUB, MUL, DIV) Math functions include addition, subtraction, multiplication, and division. When a function receives power flow, the appropriate math function is performed on input parameters I1 and I2. These parameters must be the same data type. Output Q is the same data type as I1 and I2.

Rules for Math Functions Sign of Result

Standard math rules for signed number arithmetic apply to determining the sign of the result.

Addition

The ADD instruction uses the formula I1 + I2 = Q.

Subtraction

The SUB instruction uses the formula I1 – I2 = Q.

Multiplication

The MUL instruction uses the formula I1 x I2 = Q.

Division

The DIV instruction uses the formula I1 ÷ I2 = Q. For INT and DINT types. DIV rounds down to a whole number quotient (any remainder is discarded) for the INT or DINT types; it does not round to the closest integer. For example, 53 divided by 5 = 10 (the remainder of 3 is discarded). For REAL type. DIV produces a decimal number result for the Real type

Modulo Division

The MOD instruction can only use types INT and DINT (REAL is not supported). The MOD instruction uses the formula I1 ÷ I2 = Q. However, MOD produces only the remainder from the division operation and discards the quotient. For example, 53 divided by 5 = 3 (the quotient of 10 is discarded).

Data Types for Math Functions After you have programmed a math function, you can select the data type. The data type will appear on the function just below the function’s name (see example in next figure). The three data types available for math functions are listed in the following table: Data Type

Description

INT

Signed integer.

DINT

Double precision signed integer.

REAL*

Floating Point

*REAL data type is only available on 35x and 36x series CPUs, Firmware Release 9.00 or later, and on all releases of CPU352 and CPU37X.

The default data type is signed integer. For more information on data types, please refer to Chapter 2, Section 2, “Program Organization and User References/Data.” If the operation of INT or DINT results in overflow, the output reference is set to its largest possible value for the data type. For signed numbers, the sign is set to show the direction of the

6-2

Series 90™-30/20/Micro PLC CPU Instruction Set Reference Manual – May 2002

GFK-0467M

Page 108

6 overflow. If the operation does not result in overflow (and the inputs are valid numbers), the ok output is set ON; otherwise, it is set OFF. If signed or double precision integers are used, the sign of the result depends on the signs of inputs I1 and I2.

ADD

Enable

OK

INT Input 1

I1

Input 2

I2

Data Type Q

Output

Parameters Parameter

Description enable

When the function is enabled, the operation is performed.

I1

I1 contains a constant or reference for the first value used in the operation. (I1 is on the left side of the mathematical equation, as in I1, I2).

I2

I2 contains a constant or reference for the second value used in the operation. (I2 is on the right side of the mathematical equation, as in I1, I2).

ok

The ok output is energized when the function is performed without overflow, unless an invalid operation occurs.

Q

Output Q contains the result of the operation.

Valid Memory Types Parameter flow enable

•

I1 I2 ok Q

%I

%Q

%M

%T o o o o o o

%S

%G

%R

%AI %AQ const o o

•

•

•

•† o o

•

•

•

•†

• none

• o o o o o

•

•

•

•

Valid reference or place where power may flow through the function. o Valid reference for INT data only; not valid for DINT or REAL. † When using Logicmaster, you will only be able to enter values between –32,768 and +32,767 for double precision signed integer operations. With VersaPro, you can enter full double precision values.

Note The default type is INT for 16-bit or single register operands. In Logicmaster, press F10 to change the Types selection to DINT, 32-bit double word, or REAL (for the 35x, 36x, and 37x series CPUs only). PLC INT values occupy a single 16-bit register, %R, %AI or %AQ. DINT values require two consecutive registers with the low 16 bits in the first word and the upper 16 bits with the sign in second word. REAL values, in the 35x and 36x series CPU (Release 9.00 or later) and all releases of CPU352 and CPU37x, also occupy a 32-bit double register with the sign in the high bit followed by the exponent and mantissa.

GFK-0467M

Chapter 6 Math Functions

6-3

Page 109

6

Math Function Examples ADD Circuit with a Problem In the following example, an attempt was made to create a counter circuit that would count the number of times switch %I0001 closes. The running total is stored in register %R0002. The intent of this design is that when %I0001 closes, the ADD instruction adds one to the value in %R0002 (the input on I2) and places the new value right back into %R0002 (the output on Q). The problem with this design is that the ADD instruction will execute once every PLC scan while %I0001 is closed. So, for example, if %I0001 stays closed for five scans, the output will increment five times, even though %I0001 only closed once during that period. To correct this problem, the enable input to the ADD instruction should come from a transition (“one-shot”) coil, as shown in the second figure below. %I0001

%Q0001 ADD INT

%R0002 +00095

I1

CONST +00001

I2

Q

%R0002 +00095

In the following improved circuit, the %I0001 input switch controls a transition (“one-shot”) coil, %M0001, whose contact turns on the enable input of the ADD function for only one scan each time contact %I0001 closes. In order for the %M0001 contact to close again, contact %I0001 has to open and close again.

Corrected ADD Circuit Design %I0001

%M0001

%M0001

%Q0001 ADD INT

6-4

%R0002 +00095

I1

CONST +00001

I2

Q

%R0002 +00095

Series 90™-30/20/Micro PLC CPU Instruction Set Reference Manual – May 2002

GFK-0467M

Page 110

6

Math Functions and Data Types Function

Operation

Displays as

ADD INT

Q(16 bit) = I1(16 bit) + I2(16 bit)

5-digit base 10 number with sign

ADD DINT

Q(32 bit) = I1(32 bit) + I2(32 bit)

8-digit base 10 number with sign

ADD REAL*

Q(32 bit) = I1(32 bit) + I2(32 bit)

7-digit base 10 number, sign and decimal

SUB INT

Q(16 bit) = I1(16 bit) – I2(16 bit)

5-digit base 10 number with sign

SUB DINT

Q(32 bit) = I1(32 bit) – I2(32 bit)

8-digit base 10 number with sign

SUB REAL*

Q(32 bit) = I1(32 bit) – I2(32 bit)

7-digit base 10 number, sign and decimal

MUL INT

Q(16 bit) = I1(16 bit) * I2(16 bit)

5-digit base 10 number with sign

MUL DINT

Q(32 bit) = I1(32 bit) * I2(32 bit)

8-digit base 10 number with sign

MUL REAL*

Q(32 bit) = I1(32 bit) * I2(32 bit)

7-digit base 10 number, sign and decimal

DIV INT

Q(16 bit) = I1(16 bit) / I2(16 bit)

5-digit base 10 number with sign

DIV DINT

Q(32 bit) = I1(32 bit) / I2(32 bit)

8-digit base 10 number with sign

DIV REAL*

Q(32 bit) = I1(32 bit) / I2(32 bit)

7-digit base 10 number, sign and decimal

* 35x and 36x series CPUs only, Release 9 or later, and all releases of CPU352 and CPU37x.

Note The input and output data types must be the same for math functions. The MUL and DIV functions do not support a mixed mode as the Series 90-70 PLCs do. For example, the MUL INT of two 16-bit inputs produces a 16-bit product, not a 32-bit product. Using MUL DINT for a 32-bit product requires both inputs to be 32-bit. The DIV INT divides a 16-bit I1 by a 16-bit I2 for a 16-bit result, while DIV DINT divides a 32-bit I1 by 32-bit I2 for a 32-bit result. When enabled, these functions pass power if there is no math overflow. If an overflow occurs, the result is the largest value with the proper sign and no power flow. Be careful to avoid overflows when using MUL and DIV functions. If you have to convert INT to DINT values, remember that the CPU uses standard 2’s complement with the sign extended to the highest bit of the second (most significant) word. You must check the sign of the low 16-bit word and extend it into the second 16-bit word. If the most significant bit in a 16-bit INT low word is 0 (indicating positive value), move a 0 to the second word. If the most significant bit in a 16-bit word is 1 (indicating a negative value), move a –1 or hex 0FFFFh to the second word. Converting from DINT to INT is easier as the low 16-bit word (first register) is the INT part of a DINT 32-bit word. The upper 16 bits or second word should be either a 0 (positive) or –1 (negative) value or the DINT number is too big to convert to 16 bit.

GFK-0467M

Chapter 6 Math Functions

6-5

Page 111

6 Example A common application is to scale analog input values with a MUL operation followed by a DIV and possibly an ADD operation. A 0 to ± 10 volt analog input will place values of 0 to ± 32,000 in its corresponding %AI input register. Multiplying this input register using an INT MUL function will result in an overflow since an INT type instruction has an input and output range of 32,767 to –32,768. Using the %AI value as in input to a MUL DINT will also not work as the 32-bit I1 will combine 2 analog inputs at the same time. To solve this problem, you can move the analog input to the low word of a double register, then test the sign and set the second register to 0 if the sign tests positive or –1 if negative. Then use the double register just created with a MUL DINT which gives a 32-bit result, and which can be used with a following DINT DIV function. For example, the following logic could be used to scale a ± 10 volt input %AI1 to ± 25000 engineering units in %R5. | _____ _____ _____ |ALW_ON | | | | | | | - - ] [ - - - | MOVE| - - - - - - - - - - - - - - - - - | MOVE| - - - - - - - - - - - - - - - - - | LT_ | | - - - - - - - - - - <+> | | | | | | | | | | INT | | INT | | INT | | | | | | | | | | |%AI0001–|IN Q|–%R0001 CONST –|IN Q|–%R0002 %R0001 –|I1 Q|| | LEN | +00000 | LEN | | | | |00001| |00001| | | | |_____| |_____| CONST –|I2 | | +00000 |_____| | | _____ | | | |<+> - - - - - |MOVE_|– | | | | | INT | | | | | CONST –|IN Q|–%R0002 | –00001 | LEN | | |00001| | |_____| | | | _____ _____ |ALW_ON | | | | | - - ] [ - - - - - - - - - - - | MUL_| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | DIV_|– | | | | | | | DINT| | DINT| | | | | | | %R0001 –|I1 Q|–%R0003 %R0003 –|I1 Q|–%R0005 | | | | | | CONST –|I2 | CONST –|I2 | | +0000025000 |_____| +0000032000 |_____| |

An alternate, but less accurate, way of programming this circuit using INT instructions involves placing the DIV instruction first, followed by the MUL instruction. The value of I2 for the DIV instruction would be 32, and the value of I2 for the MUL would be 25. This maintains the scaling proportion of the above circuit and keeps the values within the working range of the INT type instructions. However, the DIV instruction inherently discards any remainder value, so when the DIV output is multiplied by the MUL instruction, the error introduced by a discarded remainder is multiplied. The percent of error is non-linear over the full range of input values and is greater at lower input values. By contrast, in the example above, the results are more accurate because the DIV operation is performed last, so the discarded remainder is not multiplied. If even greater precision is required, substitute REAL type math instructions in this example so that the remainder is not discarded.

6-6

Series 90™-30/20/Micro PLC CPU Instruction Set Reference Manual – May 2002

GFK-0467M

Page 112

6

MOD

(INT, DINT) The Modulo (MOD) function is used to divide one value by another value of the same data type to obtain the remainder. The sign of the result is always the same as the sign of input parameter I1. The MOD function operates on these types of data: Data Type

Description

INT

Signed integer.

DINT

Double precision signed integer.

The default data type is signed integer; however, it can be changed after selecting the function. For more information on data types, please refer to chapter 2, section 2, “Program Organization and User References/Data.” When the function receives power flow, it divides input parameter I1 by input parameter I2. These parameters must be the same data type. Output Q is calculated using the formula: Q = I1 - ([I1 DIV I2] * I2)

where DIV produces an integer number. Q is the same data type as input parameters I1 and I2. OK is always ON when the function receives power flow, unless there is an attempt to divide by zero. In that case, it is set OFF. MOD

Enable

OK

INT Input 1

I1

Input 2

I2

Data Type Q

Output

Parameters Parameter Enable

GFK-0467M

Description When the function is enabled, the operation is performed.

I1

I1 contains a constant or reference for the value to be divided by I2.

I2

I2 contains a constant or reference for the value to be divided into I1.

OK

The ok output is energized when the function is performed without overflow.

Q

Output Q contains the remainder, if any, that results from dividing I1 by I2. If the value in I2 is an even multiple of I1, output Q will be zero, indicating no remainder.

Chapter 6 Math Functions

6-7

Page 113

6 Valid Memory Types Parameter flow enable

•

I1 I2 ok

%Q

%M

%T o o o o o o

%S

%G

%R

%AI %AQ const o o

•

•

•

•† o o

•

•

•

•†

•

Q • o †

%I none

• o o o o o

•

•

•

Valid reference or place where power may flow through the function. Valid reference for INT data only; not valid for DINT. Constants are limited to values between –32,768 and +32,767 for double precision signed integer operations.

Example In the following example, boxes are being automatically filled with parts. One box holds six parts. This circuit determines the status of the current box being filled by using modulo division. When enabled, the MOD function divides the register (PARTS) holding the count of parts produced, by six. The output (STATUS) of the MOD instruction indicates how many parts (between 1 and 5) have been loaded into the current box. When the current box is full, the output at Q will equal zero; if the current box is only partially filled, the output will indicate the number of parts already in the box. The values in the example show that a total 17 parts have been produced and that the current box has five parts in it. (The other 12 parts filled two boxes.) %I0001 MOD INT PARTS +0017

I1

CONST +0006

I2

Q

STATUS +0005

To determine the number of boxes filled, you could use the DIV instruction in the following circuit. %I0001 DIV INT PARTS +0017

I1

CONST +0006

I2

Q

BOXES +0002

One possible problem with these circuits is that the register nicknamed PARTS can only hold a maximum of 32,767 counts. If you need to count higher than that, some additional logic will be required to (1) reset the PARTS register before it reaches maximum, (2) to capture the number of boxes filled before you reset the PARTS register, and (3) to reset the PARTS register when the STATUS register is zero so that its count stays accurate.

6-8

Series 90™-30/20/Micro PLC CPU Instruction Set Reference Manual – May 2002

GFK-0467M