The text of a manual · part 7 of 70
SLC 500 Instruction Set Reference Manual, pages 61 to 70
Pages 61 to 70 of 694
The original document is the PDF (7.2 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 Allen-Bradley 1747-L514
Page 61
Comparison Instructions
Limit Test (LIM) LIM Limit Test Low Lim Test High Lim
Use the LIM instruction to test for values within or outside a specified range, depending on how you set the limits. 5 5< N7:13 7< 8 8<
Entering Parameters
Fixed SLC SLC SLC SLC SLC 5/01 5/02 5/03 5/04 5/05 •
3-5
•
Input Instruction
•
•
The Low Limit, Test, and High Limit values can be word addresses or constants, restricted to the following combinations: • If the Test parameter is a program constant, both the Low Limit and High Limit parameters must be word addresses. • If the Test parameter is a word address, the Low Limit and High Limit parameters can be either a program constant or a word address.
True/False Status of the Instruction If the Low Limit has a value equal to or less than the High Limit, the instruction is true when the Test value is between the limits or is equal to either limit. If the Test value is outside the limits, the instruction is false, as shown below. False
True
False
-32,768
+32,767 Low Limit
High Limit
Example, low limit less than high limit: Low Limit High Limit Instruction is True Instruction is False when Test value is when Test value is 5 8 5 through 8 -32,768 through 4 and 9 through 32,767
If the Low Limit has a value greater than the High Limit, the instruction is false when the Test value is between the limits. If the Test value is equal to either limit or outside the limits, the instruction is true, as shown below.
True
False
True
-32,768
+32,767 High Limit
Low Limit
Example, low limit greater than high limit: Low Limit High Limit Instruction is True when Test value is 8 5 -32,768 through 5 and 8 through 32,767
Instruction is False when Test value is 6 and 7
Publication 1747-RM001G-EN-P - November 2008
Page 62
3-6
Comparison Instructions
Notes:
Publication 1747-RM001G-EN-P - November 2008
Page 63
Chapter
4
Math Instructions This chapter contains general information about math instructions and explains how they function in your logic program. Each of the math instructions includes information on: • instruction symbol. • instruction usage. Table 4.1 Math Instructions Instruction Mnemonic ADD SUB MUL DIV DDV CLR SQR SCP SCL RMP ABS CPT SWP ASN ACS ATN COS LN LOG SIN TAN XPY
1
Purpose
Page
Name Add Subtract Multiply Divide
Adds source A to source B and stores the result in the destination. 4-5 Subtracts source B from source A and stores the result in the destination. 4-5 Multiplies source A by source B and stores the result in the destination. 4-8 Divides source A by source B and stores the result in the destination and the 4-9 math register. Double Divide Divides the contents of the math register by the source and stores the result in 4-11 the destination and the math register. Clear Sets all bits of a word to zero. 4-12 Square Root Calculates the square root of the source and places the integer result in the 4-12 destination. Scale with Produces a scaled output value that has a linear relationship between the input 4-13 Parameters and scaled values. Scale Data Multiplies the source by a specified rate, adds to an offset value, and stores 4-15 the result in the destination. Ramp Provides the ability to create linear, acceleration, deceleration, and “S” curve 4-20 ramp output data wave forms. Absolute Calculates the absolute value of the source and places the result in the 4-24 destination. Compute Evaluates an expression and stores the result in the destination. 4-25 Swap Swaps the low and high bytes of a specified number of words in a bit, integer, 4-28 ASCII, or string file. Arc Sine Takes the arc sine of a number and stores the result (in radians) in the 4-29 destination. Arc Cosine Takes the arc cosine of a number and stores the result (in radians) in the 4-29 destination. Arc Tangent Takes the arc tangent of a number and stores the result (in radians) in the 4-30 destination. Cosine Takes the cosine of a number and stores the result in the destination. 4-30 Natural Log Takes the natural log of the value in the source and stores it in the destination. 4-31 Log to the Base 10 Takes the log base 10 of the value in the source and stores the result in the 4-31 destination. Sine Takes the sine of a number and stores the result in the destination. 4-32 Tangent Takes the tangent of a number and stores the result in the destination. 4-32 X to the Power of Y Raise a value to a power and stores the result in the destination. 4-33
Publication 1747-RM001G-EN-P - November 2008
Page 64
4-2
Math Instructions
About the Math Instructions The majority of the instructions take two input values, perform the specified arithmetic function, and output the result to an assigned memory location.
For example, both the ADD and SUB instructions take a pair of input values, add or subtract them, and place the result in the specified destination. If the result of the operation exceeds the allowable value, an overflow or underflow bit is set. To learn more about the math instructions, we suggest that you read the Math Instructions Overview that follows.
Math Instructions Overview The following general information applies to math instructions. Entering Parameters • Source is the address(es) of the value(s) on which the mathematical, logical, or move operation is to be performed. This can be word addresses or program constants. An instruction that has two source operands does not accept program constants in both operands. • Destination is the address of the result of the operation. Signed integers are stored in two’s complement form and apply to both source and destination parameters. When using either an SLC 5/03 (OS301 and higher), SLC 5/04, or SLC 5/05 processor; floating point and string values (specified at the word level) are supported.
Using Indexed Word Addresses You have the option of using indexed word addresses for instruction parameters specifying word addresses (except for fixed and SLC 5/01 processors).
Using Indirect Word Addresses You have the option of using indirect word-level and bit-level addresses for instructions specifying word addresses when using an SLC 5/03 (OS302), SLC 5/04 (OS401), or SLC 5/05 processors.
Publication 1747-RM001G-EN-P - November 2008
Page 65
Math Instructions
4-3
Updates to Arithmetic Status Bits The arithmetic status bits are found in Word 0, bits 0 to 3 in the controller status file. After an instruction is executed, the arithmetic status bits in the status file are updated. Table 4.2 Processor Function With this Bit
The Controller
S:0/0
Carry (C)
sets if carry is generated; otherwise cleared.
S:0/1
Overflow (V)
indicates that the actual result of a math instruction does not fit in the designated destination.
S:0/2
Zero (Z)
indicates a 0 value after a math, move, or logic instruction.
S:0/3
Sign (S)
indicates a negative (less than 0) value after a math, move, or logic instruction.
Overflow Trap Bit, S:5/0 Minor error bit (S:5/0) is set upon detection of a mathematical overflow or division by zero. If this bit is set upon execution of an END statement, a Temporary End (TND) instruction, or an I/O Refresh (REF), the recoverable major error code 0020 is declared. In applications where a math overflow or divide by zero occurs, you can avoid a CPU fault by using an unlatch (OTU) instruction with address S:5/0 in your program. The rung must be between the overflow point and the END, TND, or REF statement.
Updates to the Math Register, S:13 and S:14 Status word S:13 contains the least significant word of the 32-bit value of the MUL instruction. It contains the remainder for DIV and DDV instructions. It also contains the first four BCD digits for the Convert from BCD (FRD) and Convert to BCD (TOD) instructions. Status word S:14 contains the most significant word of the 32-bit value of the MUL instruction. It contains the unrounded quotient for DIV and DDV instructions. It also contains the most significant digit (digit 5) for TOD and FRD instructions.
TIP
When using floating point, S:13 and S:14 are not used.
Publication 1747-RM001G-EN-P - November 2008
Page 66
4-4
Math Instructions
Using Floating Point Data File (F8:) This file type is valid for SLC 5/03 (OS301 and higher), SLC 5/04, and SLC 5/05 processors. These are 2-word elements and addressable only at the element level. Assign floating point addresses as follows. Table 4.3 Addressing Format Format Ff:e
Explanation F f
: e
Examples:
Publication 1747-RM001G-EN-P - November 2008
F8:2 F10:36
Floating Point file File number. Number 8 is the default file. A file number between 9- 255 can be used if additional storage is required. Element delimiter Element number Ranges from 0- 255. These are 2-word elements. Non-extended 32-bit numbers Element 2, floating point file 8 Element 36, floating point file 10 (file 10 designated as a floating point file by the user)
Page 67
Math Instructions
4-5
Use the ADD instruction to add one value (source A) to another value (source B) and place the result in the destination.
Add (ADD)
Updates to Arithmetic Status Bits ADD Add Source A
N7:14 6< N7:15 8< N7:16 0<
Source B Dest
The arithmetic status bits are found in Word 0, bits 0 to 3 in the status file. After an instruction is executed, the arithmetic status bits in the status file are updated.
Fixed SLC SLC SLC SLC SLC 5/01 5/02 5/03 5/04 5/05 •
•
•
•
•
•
Table 4.4 Processor Function With this Bit
The Processor
S:0/0
Carry (C)
sets if carry is generated; otherwise resets (integer). For floating point, it is cleared.
S:0/1
Overflow (V) sets if overflow is detected at destination; otherwise resets. On overflow, the minor error flag is also set. For floating point, the overflow value is placed in the destination. For an integer, the value -32,768 or 32,767 is placed in the destination. Exception: If you are using an SLC 5/02 or higher processor and have S:2/14 (math overflow selection bit) set, then the unsigned, truncated overflow remains in the destination.
S:0/2
Zero (Z)
sets if result is zero; otherwise resets.
S:0/3
Sign (S)
sets if result is negative; otherwise resets.
Output Instruction
Use the SUB instruction to subtract one value (source B) from another (source A) and place the result in the destination.
Subtract (SUB)
Updates to Arithmetic Status Bits SUB Subtract Source A Source B Dest
N7:17 0< N7:18 0< N7:19 0<
The arithmetic status bits are found in Word 0, bits 0 to 3 in the status file. After an instruction is executed, the arithmetic status bits in the status file are updated.
Fixed SLC SLC SLC SLC SLC 5/01 5/02 5/03 5/04 5/05 •
•
•
•
Output Instruction
•
•
Table 4.5 Processor Function With this Bit
The Processor
S:0/0
Carry (C)
sets if borrow is generated; otherwise resets (integer). For floating point it is cleared.
S:0/1
Overflow (V) sets if underflow; otherwise reset. On underflow, the minor error flag is also set. For floating point, the overflow value is placed in the destination. For an integer, the value -32,768 or 32,767 is placed in the destination. Exception: If you are using an SLC 5/02 or higher processor and have S:2/14 (math overflow selection bit) set, then the unsigned, truncated overflow remains in the destination.
S:0/2
Zero (Z)
sets if result is zero; otherwise resets.
S:0/3
Sign (S)
sets if result is negative; otherwise resets.
Publication 1747-RM001G-EN-P - November 2008
Page 68
4-6
Math Instructions
You have the option of performing 16-bit or 32-bit signed integer addition and subtraction. This is facilitated by status file bit S:2/14 (math overflow selection bit).
32-Bit Addition and Subtraction
Math Overflow Selection Bit S:2/14 Fixed SLC SLC SLC SLC SLC 5/01 5/02 5/03 5/04 5/05 •
•
•
•
Set this bit when you intend to use 32-bit addition and subtraction. When S:2/14 is set, and the result of an ADD, SUB, MUL, DIV, or NEG instruction cannot be represented in the destination address (due to math underflow or overflow.: • The overflow bit S:0/1 is set • The overflow trap bit S:5/0 is se. • The destination address contains the unsigned, truncated, least significant 16 bits of the result
TIP
For MUL, DIV, integer, and all floating point instructions with an integer destination, when S:2/14 is set, the state change takes effect immediately.
When S:2/14 is reset (default condition), and the result of an ADD, SUB, MUL, DIV, or NEG instruction cannot be represented in the destination address (due to math underflow or overflow). • The overflow bit S:0/1 is set • The overflow trap bit S:5/0 is set • The destination address contains 32767 if the result is positive or -32768 if the result is negative
TIP
Additionally, the SLC 5/03 and higher processors only assert the state of bit S:2/14 at the end of scan for the ADD, SUB, and NEG instructions.
Note that the status of bit S:2/14 has no effect on the DDV instruction. Also, it has no effect on the math register content when using MUL and DIV instructions.
TIP
Publication 1747-RM001G-EN-P - November 2008
The SLC 5/03 and higher processors only interrogate the S:2/14 bit upon going to the Run mode and end-of-scan. Use the Data Monitor function to make this selection prior to entering the Run mode.
Page 69
Math Instructions
4-7
Example of 32-bit Addition The following example shows how a 16-bit signed integer is added to a 32-bit signed integer. Remember that S:2/14 must be set for 32-bit addition. Note that the value of the most significant 16 bits (B3:3) of the 32-bit number is increased by 1 if the carry bit S:0/0 is set and it is decreased by 1 if the number being added (B3:1) is negative.
The largest possible number is 2,147,483,647 (7FFF FFFF)h.
TIP
To avoid a major error from occurring at the end of the scan, you must unlatch overflow trap bit S:5/0 as shown in the example ladder diagram to follow. Table 4.6 Add 16-bit value B3:1 to 32-bit value B3:3 B3:2 Add Operation
Binary
Hex
Decimal(1)
Addend Addend
B3:3 B3:2 B3:1
0000 0000 0000 0011 0001 1001 0100 0000 0003 1940 0101 0101 1010 1000 55A8
203,072 21,928
Sum
B3:3 B3:2
0000 0000 0000 0011 0110 1110 1110 1000 0003 6EE8
225,000
(1)
The programming device displays 16-bit decimal values only. The decimal value of a 32-bit integer is derived from the displayed binary or hex value. For example, 0003 1940 Hex is 164x3 +163x1 + 162x9 + 161x4 + 160x0 = 203,072.
Publication 1747-RM001G-EN-P - November 2008
Page 70
4-8
Math Instructions
B3 ] [ 0
When rung goes true for a single scan, B3:1 is added to B3:2. The result is placed in B3:2
ADD
B3 [OSR] 1
ADD Source A B3:1 0101010110101000 Source B B3:2 0001100101000000 Dest B3:2 0001100101000000
S:0 ] [ 0
If a carry is generated (S:0/0 set), 1 is added to B3:3.
ADD ADD Source A
1
Source B B3:3 0000000000000011 Dest B3:3 0000000000000011
B3 ] [ 31
SUB SUBTRACT Source A B3:3 0000000000000011 Source B 1
If B3:1 is negative (B3/31 set), 1 is subtracted from B3:3.
Dest B3:3 0000000000000011
S:5 (U) 0 END
TIP
Overflow trap bit S:5/0 is unlatched to prevent a major error from occurring at the end of the scan.
You can use the rung above with a DDV instruction and a counter to find the average value of B3:1.
Use the MUL instruction to multiply one value (source A) by another (source B) and place the result in the destination.
Multiply (MUL)
Updates to Arithmetic Status Bits MUL Multiply Source A Source B Dest
N7:20 0< N7:21 0< N7:22 0<
The arithmetic status bits are found in Word 0, bits 0 to 3 in the status file. After an instruction is executed, the arithmetic status bits in the status file are updated.
Fixed SLC SLC SLC SLC SLC 5/01 5/02 5/03 5/04 5/05 •
•
•
•
•
•
Output Instruction
Publication 1747-RM001G-EN-P - November 2008