The text of a manual · part 32 of 55
Hand-Held Terminal (Catalog Number 1747-PT1) User Manual: 20 - Math Instructions, pages 277 to 284
Pages 277 to 284 of 486, from Hand-Held Terminal (Catalog Number 1747-PT1) User Manual
The original document is the PDF (1.5 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-PT1
Page 277
Chapter
A–B
20
Math Instructions This chapter covers output instructions that allow you to perform computation and math operations on individual words. Instructions for use with fixed, SLC 5/01, and SLC 5/02 processors: • Add (ADD) • Subtract (SUB) • Multiply (MUL) • Divide (DIV) • Double Divide (DDV) • Negate (NEG) • Clear (CLR) • Convert to BCD (TOD) • Convert from BCD (FRD) • Decode (DCD) Instructions for use with SLC 5/02 processors only: • Square Root (SQR) • Scale (SCL) Application techniques possible with Series C and later SLC 5/02 processors: • 32-bit addition and subtraction All application examples shown are in the HHT zoom display.
Math Instructions Overview
The following general information applies to math instructions.
Entering Parameters • Source – address(es) of the value(s) on which the mathematical, logical, or move operation is to be performed; can be word addresses or program constants. An instruction that has two source operands will not accept program constants in both operands. • Destination – the address (destination) of the result of the operation. Signed integers are stored in two’s complementary form. Refer to appendix B for more information regarding two’s complement form.
20–1
Page 278
Chapter 20 Math Instructions
Using Arithmetic Status Bits After an instruction is executed, the arithmetic status bits in the status file are updated: • Carry (C), S:0/0 – Set if a carry is generated; otherwise cleared. • Overflow (V), S:0/1 – Indicates that the actual result of a math instruction does not fit in the designated destination. • Zero (Z), S:0/2 – Indicates a 0 value after a math, move, or logic instruction. • Sign (S), S:0/3 – Indicates a negative (less than 0) value after a math, move, or logic instruction.
Overflow Trap Bit, S:5/0 The minor error bit is set upon detection of a mathematical overflow or division by 0. If this bit is still set upon execution of the END statement, a TND instruction, or an REF instruction, a recoverable major error will be declared. In applications where a math overflow or a division by 0 will occur, you can avoid a major error from occurring by resetting S:5/0 with an unlatch (OTU) instruction in your program. The rung containing the OTU instruction must be between the overflow point and the END statement, or TND instruction, or REF instruction.
Math Register, S:14 and S:13 Status word S:13 contains the least significant word of the 32-bit values of MUL and DDV instructions. It contains the remainder for DIV and DDV instructions. It also contains the first four BCD digits for the FRD and TOD instructions. Status word S:14 contains the most significant word of the 32-bit values of MUL and DDV instructions. It contains the unrounded quotient for DIV and DDV instructions. It also contains the most significant digit (digit 5) for TOD and FRD instructions.
Indexed Word Addresses With SLC 5/02 processors, you have the option of using indexed word addresses for instruction parameters specifying word addresses. Indexed addressing is discussed in chapter 4.
20–2
Page 279
Chapter 20 Math Instructions
Add (ADD)
Add
ADD
Output Instruction
HHT Ladder Display:
(ADD)
HHT Zoom Display: (online monitor mode)
ZOOM on ADD –(ADD)– NAME: ADD SOURCE A: N7:0 SOURCE B: N7:1 DEST: N7:2
2.3.0.0.2 879 2150 3029
EDT_DAT F1
F2
F3
F4
F5
Ladder Diagrams and APS Displays: ADD ADD Source A Source B Dest
N7:0 879 N7:1 2150 N7:2 3029
The value at source A is added to the value at source B and then stored in the destination.
Using Arithmetic Status Bits C set if carry is generated; otherwise reset V set if overflow is detected at destination; otherwise reset. On overflow, the minor error flag (S:5/0) is also set. The value – 32,768 or 32,767 is placed in the destination. Exception: If you are using a Series C or later SLC 5/02 processor and have the Math Overflow Selection Bit S:2/14 set, then the unsigned, truncated overflow remains in the destination. Z set if the result is zero; otherwise reset S set if the result is negative; otherwise reset
Math Register Contents unchanged.
20–3
Page 280
Chapter 20 Math Instructions
Subtract (SUB)
Subtract
SUB
Output Instruction
HHT Ladder Display:
(SUB)
HHT Zoom Display: (online monitor mode)
ZOOM on SUB –(SUB)– NAME: SUBTRACT SOURCE A: N7:0 879 SOURCE B: N7:1 2150 DEST: N7:2 –1271
2.3.0.0.2
EDT_DAT F1
F2
F3
F4
F5
Ladder Diagrams and APS Displays: SUB SUBTRACT Source A Source B Dest
N7:0 879 N7:1 2150 N7:2 –1271
The value at source B is subtracted from the value at source A and then stored in the destination.
Using Arithmetic Status Bits C set if borrow is generated; otherwise reset V set if underflow; otherwise reset. On underflow, the minor error flag (S:5/0) is also set, and the value-32,768 or 32,767 will be placed in the destination. Exception: If you are using a Series C or later SLC 5/02 processor and have the Math Overflow Selection Bit S:2/14 set, then the unsigned, truncated overflow remains in the destination. Z set if the result is zero; otherwise reset S set if the result is negative; otherwise reset
Math Register Contents unchanged.
20–4
Page 281
Chapter 20 Math Instructions
32-Bit Addition and Subtraction–Series C and Later SLC 5/02 Processors
With the Series C SLC 5/02 processor, you have the option of performing 16-bit signed integer addition and subtraction (same as Series B SLC 5/02 processors) or 32-bit signed integer addition and subtraction. This is facilitated by status file bit S:2/14, the Math Overflow Selection Bit.
Bit S:2/14 Math Overflow Selection 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, or DIV instruction cannot be represented in the destination address (due to a 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 the unsigned truncated least significant 16 bits of the result. When combined with the operation of the carry bit, the unsigned truncated value in the destination allows you to retain the true value of the result. The default condition of S:2/14 is reset (0). This provides the same operation as that of the Series B SLC 5/02 processor. When S:2/14 is reset, and the result of an ADD, SUB, MUL, or DIV instruction cannot be represented in the destination address (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. 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.
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 in this program, 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. To avoid a major error from occurring at the end of the scan, you must unlatch overflow trap bit S:5/0 as shown.
20–5
Page 282
Chapter 20 Math Instructions
Add 16–bit value B3:1 to 32–bit value B3:3 B3:2 Add operation
Binary
Decimal➀
Hex
Addend B3:3 B3:2 Addend B3:1
0000 0000 0000 0011 0001 1001 0100 0000 0003 1940 55A8 0101 0101 1010 1000
203,072 21,928
Sum B3:3 B3:2
0000 0000 0000 0011 0110 1110 1110 1000 0003 6EE8
225,000
➀ 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 16 4x3 + 16 3x1 + 16 2x9 + 16 1x4 + 16 0x0 = 203,072.
B3 ] [
ADD
B3 [OSR] 1 0
ADD Source A B3:1 0101010110101000 Source B B3:2 0001100101000000 Dest B3:2 0001100101000000
ADD
S:0 ] [ 0
ADD Source A
1
When rung goes true for a single scan, B3:1 is added to B3:2. The result is placed in B3:2.
If a carry is generated (S:0/0 set), 1 is added to B3:3.
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
Application Note: You could use the rung above with a DDV instruction and a counter to find the average value of B3:1
20–6
Overflow trap bit S:5/0 is unlatched to prevent a major error from occurring at the end of the scan.
Page 283
Chapter 20 Math Instructions
Multiply (MUL)
Multiply
MUL
Output Instruction
HHT Ladder Display:
(MUL)
HHT Zoom Display: (online monitor mode)
ZOOM on MUL –(MUL)– NAME: MULTIPLY SOURCE A: N7:0 SOURCE B: N7:1 DEST: N7:2
2.3.0.0.2 8 2150 17200
EDT_DAT F1
F2
F3
F4
F5
Ladder Diagrams and APS Displays: MUL MULTIPLY Source A Source B Dest
N7:0 8 N7:1 2150 N7:2 17200
The value at source A is multiplied by the value at source B and then stored in the destination.
Using Arithmetic Status Bits C always reset V set if overflow is detected at the destination; otherwise reset. On overflow, the minor error flag is also set. The value 32,767 or –32,768 is placed in the destination. Exception: If you are using a Series C or later SLC 5/02 processor and have the Math Overflow Selection Bit S:2/14 set, then the unsigned, truncated overflow remains in the destination. Z set if the result is zero; otherwise reset S set if the result is negative; otherwise reset
Math Register Contains the 32–bit signed integer result of the multiply operation. This result is valid at overflow.
20–7
Page 284
Chapter 20 Math Instructions
Divide (DIV)
Divide
DIV
Output Instruction
HHT Ladder Display:
(DIV)
HHT Zoom Display: (online monitor mode)
ZOOM on DIV –(DIV)– NAME: DIVIDE SOURCE A: N7:0 SOURCE B: N7:1 DEST: N7:2
2.3.0.0.2 6214 19 327
EDT_DAT F1
F2
F3
F4
F5
Ladder Diagrams and APS Displays: DIV DIVIDE Source A Source B Dest
N7:0 6214 N7:1 19 N7:2 327
The value at source A is divided by the value at source B with the rounded quotient being stored in the destination. If the remainder is 0.5 or greater, round up occurs in the destination. The unrounded quotient is stored in the most significant word of the math register. The remainder is placed in the least significant word of the math register.
Using Arithmetic Status Bits C always reset V set if division by zero or overflow; otherwise reset. On overflow, the minor error flag is also set. The value 32,767 is placed in the destination. Exception: If you are using a Series C or later SLC 5/02 processor and have the Math Overflow Selection Bit S:2/14 set, then the unsigned, truncated overflow remains in the destination. Z set if the result is zero; otherwise reset; undefined if overflow is set S set if the result is negative; otherwise reset; undefined if overflow is set
Math Register The unrounded quotient is placed in the most significant word, the remainder is placed in the least significant word. 20–8