Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Twos compliment in Oshonsoft and Compass calibration

Status
Not open for further replies.

camerart

Well-Known Member
Hi,
I've asked a similar question in another thread, but this is to clarify a point.

I am getting results of a HIGH BYTE and a LOW BYTE. The D/S points out that these are in 'twos compliment form'

Am I correct that these two BYTEs must be converted to a WORD, then 'twos complimented' or are the two BYTES used 'against' each other in a calculation?

Thanks, Camerart
 
Hi,
I think I may be able to answer my own question.

Farther reading of the D/S says the BYTEs are in 16BIT 'twos compliment'
The results alway show the HIGH BYTE as 0xff, this was what was throwing me, I thought the LOW BYTE was NANDed (from experiment in Oshonsoft) from the HIGH BYTE.
Still testing!
C.
 
Two's complement... Invert and add 1..

binary 22 = 00010110 Invert 11101001 ( - 23 ).. then add 1, 11101010 ( -22 )

The Idea is for subtraction.. 7 - 8 = -1.
8 inverted = -9 then +1 = -8.. then addition.. 7 + -8 = -1

So if you are given a two's complement number it is basically a negative number.
Minus one and toggle all the bits to get a positive number!!
I use ABS to get a negative number to a positive one..

Code:
Function abs(value As Word) As Word
 Dim result As Word
 result = value - 1
 result = 65535 - result
 abs = result 
End Function
 
Hi I,

I was thinking that the TWO BYTES were in 'twos compliment', but now I think the TWO BYTEs need to be joined to make a WORD, then 'twos complimented. Is this correct?
From D/S: The value stored in these two registers is a 16-bit value in 2’s complement form, whose range is 0xF800 to 0x07FF.

If correct then I now have a WORD in 'twos compliment' which needs your calculation.

I've been testing ideas in Oshonsoft, does this do the same as your suggestion?
NOTE: This is before I read the 16-BIT section in D/S (The resulting READings have always given the HB as 255 so far)
C
 

Attachments

  • Twos compliment.bas
    1.5 KB · Views: 358
You are confusing 2's complement with the fact you have two bytes... 2's complement just means negative..

A word is 16 bits.. Lets take random number 437 in 16 bit binary 0000 0001 1011 0101
To make it negative we use the term 2's complement... If you complement the number above ( invert ) we get

1111 1110 0100 1010 which isn't -437! It is -438. To make it right, we need another operation, ie.. we add 1
1111 1110 0100 1011 which is -437.. Two operations or 2's complement!!

It could contain any number of bytes. I'm using a word as you have a word ( two bytes)
 
Hi I,
Yes, I was thrown by having 2x BYTES.

Here's my effort in Oshonsoft, with your random number, is this ok?
C
 

Attachments

  • Twos complement.jpg
    Twos complement.jpg
    51.2 KB · Views: 364
But Oshonsoft uses only unsigned 16 bit variables.

Telling the OP to negate the value no matter what it's value is bad advice.

If Oshonsoft doesn't handle signed numbers then the OP needs to check bit 15 of the value and act accordingly - not just negate what maybe a positive number.

Mike.
 
Telling the OP to negate the value no matter what it's value is bad advice.

If Oshonsoft doesn't handle signed numbers then the OP needs to check bit 15 of the value and act accordingly - not just negate what maybe a positive number.

Mike.
I just showed how to make a 2's complement in Oshonsoft Basic, no advice how to handle signed words.
He could convert 16bit word to a floating point variable and do the math with them:

Dim x as word
Dim xf as single ' single precision floating point
xf = x
if xf > 32767
xf = 65536 - xf​
endif
 
Telling the OP to negate the value no matter what it's value is bad advice.
You are missing the point!!! The input value is 2's complement...( negative ) he needs it positive as Oshonsoft will display 65535 instead of -1.. Its easy to put a sign in front...

BUT!!! unless C tells us what the intension is, we are unsure.. If its added to a larger positive number, this argument is academic...
 
Hi,
Interesting!
My overnight question would have been 'How do I find if the WORD is positive or not? before applying the equation.

The Compass chip in question is HMC5983. Here is a jpg:
C
 

Attachments

  • 2's complement.jpg
    2's complement.jpg
    155.4 KB · Views: 351
Hi,
Interesting!
My overnight question would have been 'How do I find if the WORD is positive or not? before applying the equation.

The Compass chip in question is HMC5983. Here is a jpg:
C
If the value is < 32768 it is positive.
Or if bit (15) is 0 it is positive.
 
This is as I thought!! As jjw has already explained..

if compass_ value > 32768 then
compass_value = abs(compass_value )
sign = '-'​
end if
Something like this should work..
 
Last edited:
Hi,
From previous similar answers a couple of years ago, there is a reference to check WORD.N (I assume check WORD.15? for negativity) and I don't think Oshonsoft deals with ABS.

I think from all of your answers, I should be able to get it working.

I now wonder that, if Oshonsoft doesn't deal with negatives, will any BYTE or WORD be a negative?

C.
 
From previous similar answers a couple of years ago, there is a reference to check WORD.N (I assume check WORD.15? for negativity) and I don't think Oshonsoft deals with ABS.

I think from all of your answers, I should be able to get it working.

I now wonder that, if Oshonsoft doesn't deal with negatives, will any BYTE or WORD be a negative?

C.
No byte or word or long is negative in Oshonsoft.
It handles all integers as positive.
What are you doing with these x, y values?
Calculate directions / angles ?
 
No byte or word or long is negative in Oshonsoft.
It handles all integers as positive.
What are you doing with these x, y values?
Calculate directions / angles ?
Hi J,
The way I wrote the last sentence isn't clear.
To clarify: Oshonsoft doesn't handle negative integers, so if one arrives and it is negative, does Oshonsoft simply see it as positive, which I assume is erroneous. (Perhaps does what we are trying to achieve?)

The HMC5983 compass module, along with other types, delivers their READings to different registers. In this case X,Z and Y, possibly a triangle of magnets inside the chip. The 2x BYTEs for each register, combine to make a WORD which then goes through a calculation to give a compass reading.

So far I haven't got sensible results from the chip yet although some of them look likely.
I haven't seen any calculation yet, so that's to come.
C
 
Oshonsoft interpreters all integers as positive.
For example 16 bit word as 0-65535.
When you know, that the integer is 2's complement, you must take care of the sign in the program.
You can do it easily by converting the signed integer to a floating point variable ( see msg #12 ), as was done in the BMP280 calculation or carry the sign in a separate variable.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top