COP v CPW - What's the Word?
Tim Ganz said:
...Does the COP instruction work the same way the CPW does?...
...the COP instruction can work differently depending on what processor you are using such as a micrologix vs a SLC?...
...What will be the gotcha to look for when converting the CPW logic to COP?...
...using the CPW to copy Integers to floats and floats to integers...
While they are quite similar in what they are designed to do, the Copy Word (CPW) and Copy File (COP) instructions do not work in quite the same way.
All MicroLogix controllers, 1100 and above, support both the COP and CPW instructions.
While the MicroLogix controllers support the COP instruction it can only be used for copying between similar data types. It cannot copy between dissimilar data types. The CPW instruction must be used when copying dissimilar data types.
All SLC controllers, 5/02 or above, support the COP instruction but not the CPW instruction.
In the SLC controllers the COP instruction can copy between dissimilar data types. So there is no real need for the CPW instruction with regard to copying dissimilar data types.
For both platforms the COP instruction Length operand determines the number of elements to be copied using the
Destination data type...
Example: SLC (COP using different data types)
COP
Source: #N7:0
Dest: #F8:0
Length:
1
Here the Length is 1 x the Destination data type, which is a Float.
F8:0 = 1 x 32-bit Float
Or, in terms of the Source data type...
F8:0 = 2 x 16-bit Integers
The COP instruction will need to copy 2 x 16-bit Integers deep into the N7 Data File. So both N7:0 and N7:1 will be copied in a raw bit-wise fashion into the 32-bit Float address F8:0 to satisfy the Length of 1 x Float.
When the 2 x 16-bit Integers are copied to the 1 x 32-bit Float the resultant value may not be as intended. Because the Float data type's binary representation is not available in RSLogix 500 it is difficult to diagnose and spot what may have happened here.
When the SLC COP instruction is copying from a smaller data type to a larger data type, such as Integers to a Float, it will copy the first Source address Integer to the High Word of the Float and the second Source address Integer to the Low Word of the Float.
Example: SLC (COP using different data types)
N7:0 = 1
N7:1 = 2
F8:0 = 0.0
COP
Source: #N7:0
Dest: #F8:0
Length: 1
Result: F8:0 = 9.18383e-41
The 32-bit binary representation, which is not viewable in RSLogix 500, is...
F8:0 = bit 31>
00000000000000010000000000000010 <bit 0
Where...
N7:0 =
1 =
0000000000000001 <High Word
N7:1 =
2 =
0000000000000010 <Low Word
So it's important to note that the SLC COP instruction will swap the Word Order when copying between Integers and Floats.
Why is it so important?...
Because the CPW instruction in the MicroLogix controllers does the opposite...
Example: MicroLogix (CPW using different data types)
N7:0 = 1
N7:1 = 2
F8:0 = 0.0
CPW
Source: #N7:0
Dest: #F8:0
Length:
2
NOTE: The Length is equal to 2 here because the CPW instruction Length operand determines the number of elements to be copied using the
Source data type.
Result: F8:0 = 1.836724e-40
The 32-bit binary representation is...
F8:0 = bit 31>
00000000000000100000000000000001 <bit 0
Where...
N7:0 =
1 =
0000000000000001 <Low Word
N7:1 =
2 =
0000000000000010 <High Word
So, for your conversion, when going between Integer and Float data types, the MicroLogix CPW instructions would not have been swapping the Word Order whereas the SLC COP instructions will, giving you totally different values than expected.
The difference between the Length operand and the way the Word Order is handled are the two main gotchas to watch out for when converting between these two instructions.
In the SLC, you could swap the order the data is written into the Integers, if possible. But this is not always possible when the data is coming in from an external source in a particular order of which you cannot manipulate. Failing that, you would have to swap the integer values around before using the COP instruction. MOV instructions would suffice here.
Note: With regard to not swapping the Word Order, the COP instruction on the Logix 5000 platform behaves in the same way as the CPW instruction on the MicroLogix platform. So similarly, when converting from SLC COP instructions to Logix 5000 COP instructions, the above also applies.
Regards,
George