I'm looking at some I2C temperature sensors that return data in 2s complement. The format is 2 bytes:
SIGN | 2^7 | 2^6 | ... | 2^0 | 2^-1 | ... | 2^-4 | 0 | 0 | 0
This can give steps of 0.0625 if the sensor ADC is run in 13 bit mode. The 3 least significant bits are always 0.
I can think of a few ways of converting this to a number for display, but I haven't found any that seem particularly efficient.
It seems the complete DIY way would involve bit flipping if the SIGN is set followed by shifting the integer parts right so they line up with a single byte. The decimal part could be calculated manually by adding 0.5/0.25/0.125/0.0625 for each bit that is set.
I'm aiming code at an 18F, can anybody spot the easy way which I must have missed? Or am I on the right track?
(The sensor is a DS620, accurate to 0.5C)
SIGN | 2^7 | 2^6 | ... | 2^0 | 2^-1 | ... | 2^-4 | 0 | 0 | 0
This can give steps of 0.0625 if the sensor ADC is run in 13 bit mode. The 3 least significant bits are always 0.
I can think of a few ways of converting this to a number for display, but I haven't found any that seem particularly efficient.
It seems the complete DIY way would involve bit flipping if the SIGN is set followed by shifting the integer parts right so they line up with a single byte. The decimal part could be calculated manually by adding 0.5/0.25/0.125/0.0625 for each bit that is set.
I'm aiming code at an 18F, can anybody spot the easy way which I must have missed? Or am I on the right track?
(The sensor is a DS620, accurate to 0.5C)