# Twos compliment in Oshonsoft and Compass calibration

#### camerart

##### Active Member
I just used the calculator supplied with Windows..
Set it in Programmer mode (in View), then "word" as the data size option.

Entering the value while hex is selected, then switch to decimal.

For a generic calculator, enter the hex (in hex mode), convert to decimal then if the number is larger than 32767, subtract 65536.
That should give the same results.
Hi R.
Ok, the above method works as you say, thanks.

Is it possible to not use the HEX route. The DATA shows two BYTEs of DEC and I want to see the end result in DEC?

EDIT: I think I've worked it out (HIGH BYTE X 256 + LOW BYTE) [If > 32767 then] -65536 =X

I'll play with this and the results I'm getting so I can get the feel of what happens, when the module is moved around, before using the calculations, within this thread.

C.

Last edited:

#### rjenkinsgb

##### Active Member
Is it possible to not use the HEX route
Yes, absolutely; your method will work fine.

I'm so used to working in hex I tend to forget other things when my mind is in programming mode!

#### camerart

##### Active Member
I'm so used to working in hex I tend to forget other things when my mind is in programming mode!
Hi R,
This is absolutely baffling to me, as you can see I don't even find numbers easy.
It's coming back to me now, thanks.
C.

#### jjw

##### Member
Hi R.
Ok, the above method works as you say, thanks.

Is it possible to not use the HEX route. The DATA shows two BYTEs of DEC and I want to see the end result in DEC?

EDIT: I think I've worked it out (HIGH BYTE X 256 + LOW BYTE) [If > 32767 then] -65536 =X

I'll play with this and the results I'm getting so I can get the feel of what happens, when the module is moved around, before using the calculations, within this thread.

C.
Look msg #12 in this thread 8 months ago.
X = 65536 - X
If X is 16bit integer, X = 65535 - X + 1

#### camerart

##### Active Member
Look msg #12 in this thread 8 months ago.
X = 65536 - X
If X is 16bit integer, X = 65535 - X + 1
Hi J,
I have the 8 month old calculation, safely in the program, but 8 months, is a long time, for me to need a refresher course. Using the calculator, with the recent sums, will get me used to twos compliment again, before adding in the calculation to the program, where I'll be able to see what's gong on better.
Cheers, C.

#### camerart

##### Active Member
That's just normal big endien.

Mike.
Hi M,
I've just got to this in the DATA sheet:
Measuremnet data is stored in two’s complement and Little Endian format. Measurement range of each axis is -8190 ~ +8190 in decimal in 14-bit output, and -32760 ~ 32760 in 16-bit output.
How can I tell one 'endian' from the other?
(Set to 16BIT)
C.

#### Pommie

##### Well-Known Member
If the low byte is first in memory then it's little endian. Most pic 16 bit variables are stored in little endian format.

#### jjw

##### Member
Hi M,
I've just got to this in the DATA sheet:
Measuremnet data is stored in two’s complement and Little Endian format. Measurement range of each axis is -8190 ~ +8190 in decimal in 14-bit output, and -32760 ~ 32760 in 16-bit output.
How can I tell one 'endian' from the other?
(Set to 16BIT)
C.
From the datasheet:
Address Magnetic field Data ( byte)
03 Xlow
04 Xhigh
05 Ylow
06 Yhigh
07 Zlow
08 Zhigh

#### camerart

##### Active Member
Hi,
I presume then that:
03 Xhigh
04 Xlow
05 Yhigh
06 Ylow
07 Zhigh
08 Zlow
is big endian? Perhaps obvious, but just checking.

C

#### jjw

##### Member
If it needs negative numbers, you must input negative numbers
You must convert from the low and high bytes ( twos complement )
to x, y, z values, which will be some +- values.

#### camerart

##### Active Member
If it needs negative numbers, you must input negative numbers
You must convert from the low and high bytes ( twos complement )
to x, y, z values, which will be some +- values.
Hi J,
The Magmaster needs 6x values.
C.

#### Attachments

• 84.5 KB Views: 8

#### jjw

##### Member
Hi J,
The Magmaster needs 6x values.
C.
Doesn't it need 12 * x, y, z values, see msg#50

#### Pommie

##### Well-Known Member
It appears to need 6 floating point numbers.

Mike.

#### camerart

##### Active Member
Hi,
J and P,
Yes, a bit confusing!
It needs the 6x measurement register READings, in this format. ( I added the - to the HSEROUT, so not real, but it worked)
255, 255, 255
78, 230, 138

255, 255, 255
89, 225, 137

255, 255, 255
84, 230, 146

255, 255, 255
84, 228, 150

255, 255, 255
87, 231, 149

255, 255, 255
81, 231, 143

They are as the image in #52.
Once these are running, they show in the top bar.
Move the compass module around in these 12 orientations, (see image), while pressing the nearby button to enter each READing into Magmaster.

The Magviewer only needs the DATA as #52.
C

#### Attachments

• 347.5 KB Views: 5
Last edited:

#### camerart

##### Active Member
It appears to need 6 floating point numbers.

Mike.
Hi M,
Re-looking at #52, I think Magmaster may need the results under the lines-----------------------.
I tried 3x values and Magviewer looked the same as 6x, so it looks as though 3x twos compliment values are needed.
C

#### Pommie

##### Well-Known Member
The attachment in #52 shows floating point numbers!

Mike.

#### jjw

##### Member
Hi M,
Re-looking at #52, I think Magmaster may need the results under the lines-----------------------.
I tried 3x values and Magviewer looked the same as 6x, so it looks as though 3x twos compliment values are needed.
C
Are you now sending the data through serial interface to Magmaster or inputting it manually?
I think you should send repeatedly N coordinates x1, y1, z1 - xn, yn, zn
Coordinates should be in floating point.
You can then see live 3D data as in video #2 from Magmaster link in #msg 50 and after calibration video #4

#### camerart

##### Active Member
Hi M and J,
Stuck again

Here is the Magmaster DATA again:
Will someone tell me where twos compliment and floating point starts and ends in ABCDEF please?
(I have the calculation for twos compliment)

If I enter A and B I get this (Magviewer)

There are no negatives. so all of the DOTs are in the same quadrant!#

I use the Magviewer LIVE as I send the DATA via serial and radio modules. Once I get DOTs in all 4 quadrants, I'll follow the 12 orientations in the instructions while entering DATA into Magmaster.

EDIT: In yellow
C

#### Attachments

• 58.6 KB Views: 6
• 39 KB Views: 6
Last edited:

#### jjw

##### Member
Hi M and J,
Stuck again

Here is the Magmaster DATA again:
Will someone tell me where twos compliment and floating point starts and ends in ABCDEF please?
(I have the calculation for twos compliment)

If I enter A and B I get this (Magviewer)

There are no negatives. so all of the DOTs are in the same quadrant!#

I use the Magviewer LIVE as I send the DATA via serial and radio modules. Once I get sensible results, I'll follow the 12 orientations in the instructions while entering DATA.
C
The coordinates before the calibration is done can be floating point or 16bit signed integers
When the calbration is done you get the correction coefficients M11 - M33, which are in floating point, see pictures 1 and 9.
Because Oshonsoft does not handle signed integers, it is easier to convert the twos complement coordinates to floating point from the beginning and then send them to Magmaster, something like
Hserout #Xf, #Yf, #Zf.... etc

Converting to floating point can be done in the same way as in the pressure sensor program:
- make 16bit integer from Xlowbyte, Xhighbyte-> Xraw
Xfloat = Xraw
if Xfoat > 32767 then // is it negative
Xfloat = xfloat-65536

edit: corrected the xfoat negative calculation

Last edited: