I have a bit of a problem with this code.
I am dealing with 32 bit messages and on of the fields within this message is 21 bits long.
It also scaled to provide a range of -180 to + 180; i.e. LSB = 0.000172.
I have for each field its mask for the message; i.e. in this case 0x1FFFFF00. Also the shift value from the right (LSB) = 8 in this case.
Code:
unsigned int currentData; // This is the 32 bit message
unsigned long currentField; // The 21 bit field
Now if I have to set the field to 34.56 (float fEngValue = 34.56) and using the scaling factor (float m_fScalingFactor = 0.000172) as follows:-
Code:
currentField = (unsigned int)(fEngValue/m_fScalingFactor);
newData = (unsigned int)(currentField);
// Clear current field and leave the other fields intact
unsigned int maskData = ~mask;
unsigned int otherData = currentData & maskData;
unsigned int bitField = (newData << shift);
// Or the new data with the old fields together and write to memory
*(unsigned int *) fieldPtr = bitField | otherData;
This works fine for +ve numbers, but NOT for -ve numbers.
Again I have a mental block for negative numbers.
Any suggestions please.
Its Visual Studio .Net; C++ Windows platform.
Andy.