Peter Nachtwey
Member
Which method do you prefer for taking the difference between two encoder counts and sign extending the difference into a DINT.
MyVarIn:=CurrentEnoderCounts-PreviousEncoderCounts;
1) MyVarOut := ((MyVarIn*65536)/65536);
The MyVarIn*65536 may cause an overflow but that is not a problem with our product.
2) MyVarOut := ASHR(MyVarIn*65536),16);
The ASHR ( arithmetic shift right ) instruction shift right DINTs the number of bits specified. Bit 31 used to fill the most significant bits. Not the ASHR would be an extension to the IEC specification. It is standard to have a ASHR instruction on a microcontroller. It is a flaw not to have a ASHR instruction.
3) MyVarOut := ASHR(ASHL(MyVarIn,16),16);
This is similar to the above only the ASHL instruction replaces the multiply. The ASHL may potentionaly be faster on a small microcontroller but it does make any difference on our product. The advantage is consistency.
4) MyVarOut := ASH(ASH(MyVarIn,16),-16);
This is how our TI DSP do it. The sign of the shift number determines the direction. This reduces the number of instructions to implement.
5) MyVarOut := SEXT(MyVarIn,16);
This is the simplest. Just provide a sign extend instruction.
6) MyVarOut := ASHR(DWORD_TO_DINT(SHL(DINT_TO_DWORD(MyVarIn),16)),16)
Yuk, the IECE spec makes you type cast all the conversions. Originally the MyVarIn is signed and it must be type cast to an DWORD. Then it can be shifted left. Then it must be type cast to a DINT so it can be shifted right and sign extended. Those IEC guy really want to make life easy for you don't they?
7) Another option is to not to follow the IECs in the area of type casting. In this case option 6 would be.
MyVarOut := ASHR(SHL(MyVarIn,16)),16)
Rockwell converts the types for you whereas IEC and Siemens don't. This would be a more 'Rockwell' approach to the problem. Unfortunately I can't update my poll.
This is my first poll.
BTW, one of our tech support guys said the number of people that would care about this in the next two years is 0 and that is an over estimate. We will see.
MyVarIn:=CurrentEnoderCounts-PreviousEncoderCounts;
1) MyVarOut := ((MyVarIn*65536)/65536);
The MyVarIn*65536 may cause an overflow but that is not a problem with our product.
2) MyVarOut := ASHR(MyVarIn*65536),16);
The ASHR ( arithmetic shift right ) instruction shift right DINTs the number of bits specified. Bit 31 used to fill the most significant bits. Not the ASHR would be an extension to the IEC specification. It is standard to have a ASHR instruction on a microcontroller. It is a flaw not to have a ASHR instruction.
3) MyVarOut := ASHR(ASHL(MyVarIn,16),16);
This is similar to the above only the ASHL instruction replaces the multiply. The ASHL may potentionaly be faster on a small microcontroller but it does make any difference on our product. The advantage is consistency.
4) MyVarOut := ASH(ASH(MyVarIn,16),-16);
This is how our TI DSP do it. The sign of the shift number determines the direction. This reduces the number of instructions to implement.
5) MyVarOut := SEXT(MyVarIn,16);
This is the simplest. Just provide a sign extend instruction.
6) MyVarOut := ASHR(DWORD_TO_DINT(SHL(DINT_TO_DWORD(MyVarIn),16)),16)
Yuk, the IECE spec makes you type cast all the conversions. Originally the MyVarIn is signed and it must be type cast to an DWORD. Then it can be shifted left. Then it must be type cast to a DINT so it can be shifted right and sign extended. Those IEC guy really want to make life easy for you don't they?
7) Another option is to not to follow the IECs in the area of type casting. In this case option 6 would be.
MyVarOut := ASHR(SHL(MyVarIn,16)),16)
Rockwell converts the types for you whereas IEC and Siemens don't. This would be a more 'Rockwell' approach to the problem. Unfortunately I can't update my poll.
This is my first poll.
BTW, one of our tech support guys said the number of people that would care about this in the next two years is 0 and that is an over estimate. We will see.
Last edited: