Go Down

Topic: [Solved] Stuck with two's complement (Read 2761 times) previous topic - next topic

AlfaOmega

Dec 23, 2015, 11:05 pm Last Edit: Dec 24, 2015, 01:19 am by AlfaOmega
This seem to be a rather odd question..

I've connected an HMC5983 compass via the I2C..

That IC stores the direction measurement in two registers (MSB and LSB).
Together they make up a 16-bit value in 2's complement form, whose range is 0xF800 to 0x07FF (so says the datasheet).


I do not understand how to approach the conversion from two's complement form to 'normal' numbers..


I've tried to read up on this, but sill cant get it..

So I'd be grateful If somebody could explain :((

el_supremo

The Arduino uses two's complement. Put the two halves of the number into a signed 16-bit integer and you're done.

Pete
Don't send me technical questions via Private Message.

AlfaOmega

#2
Dec 23, 2015, 11:19 pm Last Edit: Dec 23, 2015, 11:29 pm by AlfaOmega
The Arduino uses two's complement. Put the two halves of the number into a signed 16-bit integer and you're done.

Pete
You mean something like?

int  X_direction = (X_MSB << 8 ) + X_LSB;



On this IC there is also a temperature sensor. Further along in the datasheet there is explanation on how to convert it readings from two's complement to 'normal': Temperature = (MSB * 2^8 + LSB) / (2^4 * 8) + 25 in C
Retrieval of temperature conversion works fine, I'm getting right result (around 27 degrees Celsius).

aarg

#3
Dec 23, 2015, 11:26 pm Last Edit: Dec 23, 2015, 11:29 pm by aarg
You have to use code tags to prevent the silly emoticons. Two's complement is a useful format for temperatures because they can be negative (minus zero). Changing that would destroy any temperatures below zero.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

AlfaOmega

Yes, that make sense - temperature can indeed have a negative value.

In other words - how to convert something 1111111111100111 to -25?
0b1111111111100111 stands for 65511, but it should be -25 in two's complement..

el_supremo

Store it in a 16-bit signed integer and it will be -25.
Best to declare the integer variable as int16_t so that it will be signed 16-bits even on a 32-bit processor such as Due or Teensy 3.

Pete
Don't send me technical questions via Private Message.

AlfaOmega

Thank you El_supremo!

Declaring variables as int16_t indeed solved it!

aarg

In other words - how to convert something 1111111111100111 to -25?
Use Serial.print(). :)
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Go Up