Go Down

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

AlfaOmega

Dec 23, 2015, 11:05 pmLast 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

#1
Dec 23, 2015, 11:08 pm
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 pmLast 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 * + 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 pmLast 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

#4
Dec 24, 2015, 12:03 am
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

#5
Dec 24, 2015, 12:10 am
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

#6
Dec 24, 2015, 12:41 am
Thank you El_supremo!

Declaring variables as int16_t indeed solved it!

aarg

#7
Dec 24, 2015, 12:58 am
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