|
Post by Camerart on Apr 2, 2018 19:42:34 GMT
Hi,
I've had twos compliment explained a few times, but for me it's not easy to understand properly.
I have a compass module that has 3 axis XY and Z. Each axis gives a two BYTES in twos compliment 12Bit output.
The BYTES/WORDS are READ by my program which has the twos compliment calculation.
I'm getting incorrect results, so I am looking at each BYTE to try to 'see' what's going on.
X gives HB=255 or 0 or 254 and LB= 0 to 254.
Could someone give an explanation how the 2xBYTES make up the 12BIT please?
Thanks Camerart.
|
|
|
Post by cundo on Apr 3, 2018 0:30:26 GMT
Two's complement? Can you please post some code? I'm not familiar with that mathematical operation.
|
|
|
Post by Rod on Apr 3, 2018 8:22:10 GMT
On my iPad right now so no code. It is really all about representing negative and positive numbers in one format. Your compass outputs data wider than 255 so you need more than 8 bits to store the number, 12 in your case. But two bytes has 16 bits so some will never be used. The high bit is set to signal a negative number. Your compass presumably outputs + or - 360 degrees or perhaps 1024.
If we were dealing with one byte it would represent 0 and + or - 1 to 127 not 0 to 255 so the higher byte values 128 to 255 actually represent the negative 1 to 127 and 1 to 127 represent the positive 1 to 127.
So if your high bit is set ie and 255 then the number in the rest of the bits is negative. Otherwise it’s positive.
|
|
|
Post by Camerart on Apr 3, 2018 9:19:28 GMT
Two's complement? Can you please post some code? I'm not familiar with that mathematical operation. Hi C, Here is a bit of code that converts 2x BYTES 'IN TWO'S COMPLIMENT' for use in a program: (Written for Oshonsoft BASIC) 'VVV 2'S COMPLIMENT CALC VVVVVVVVVVVV If x_raw > 32767 Then x2scomp = 65535 - x_raw x2scomp = x2scomp + 1 Endif If y_raw > 32767 Then y2scomp = 65535 - y_raw y2scomp = y2scomp + 1 Endif If z_raw > 32767 Then z_raw = 65535 - z_raw z_raw = z_raw + 1 Endif 'AAAAAAA 2'S COMPLIMENT CALC AAAAAAA C. Hi R, In your reply, you said "so the higher byte values 128 to 255" In my question I said "X gives HB=255 or 0 or 254" So from my tests I only get (HB) %00000000 or %11111110 or %11111111. The LB gives 0 to 255 Apart from the SIGNAL high BIT in the HB, can you explain which of the other BITS could be the 'counting' BITS? I would have thought: (HB) %1'1111XXX look possibilities, but it's not clear. My logic tells me that I should be seeing (HB) %1111001 sometimes. C.
|
|
|
Post by Rod on Apr 3, 2018 10:51:07 GMT
What precise compass are you using, I want to look at the spec sheet.
Just to add, the reason we are doing this is because the Arduino has a 16bit signed integer data type. So it is perfectly happy with the bytes. When we move the data to Just BASIC we don't have that data type so we need to convert it to decimal + or negative. It will be held as the usual integer type inside Just BASIC.
So a 12bit number is generated by the compass and passed over the serial link as two 8bit values.
The number 110 in decimal should look like this in 12bit binary "0000 01101110" and as 16bit "00000000 01101110" the space is just me separating the bytes in my mind. So it should be padded with 0000 to make it 16Bit which is why your numbers confuse me.
Negative -110 would look like this in 12bit binary "1111 10010010" and as 16bit "00001111 10010010"
The twelfth bit is the sign bit. If it is set you have a negative number.
So if we look at the decimal number range possible with 12bits that gives us a number between 0-4096 The numbers 0-2048 represent positive values and the number between 2048 -4096 negative numbers. So if the number is greater than 2048 simply deducting the number from 4096 gives us its negative value. 4096-2048=-2048 and 4096-4095=-1 I may be a digit off but that's the gist of it.
So can we have some real data, point your compass left and give us a reading, point it right and give us a reading.
|
|
|
Post by tsh73 on Apr 3, 2018 17:43:43 GMT
my guess that number passed is ordinary 16-bit signed and 12-bit refers to device accuracy, only. (may be wrong, but it explains high byte being 255 or 254)
|
|
|
Post by Rod on Apr 3, 2018 20:01:19 GMT
So the code posted should be doing the job, provided the two bytes are being converted to decimal correctly. The sign byte becomes byte 16 . Sample data might let us confirm.
|
|
|
Post by Camerart on Apr 4, 2018 20:35:08 GMT
Hi R and T,
I'm using HMC5983 compass module. I'm not sure but it could be that all 3axis compass modules use the same protocol??
Today I tried different 'gain' settings (See D/S) It was set as default, then I tried all of the 'gains' At minimum, it now shows HB 0 or 1 or 2 or 253 or 254 or 255. With the odd 252. This looks encouraging.
I'll post DATA as suggested if asked again, but at the moment I'm trying the program, which includes the 'twos compliment' calculation.
(I don't receive notifications! How do I switch on 'notifications?)
Thanks, C.
|
|
|
Post by Rod on Apr 4, 2018 21:16:38 GMT
Click on help then notifications, it’s a profile setting.
|
|
|
Post by Camerart on Apr 5, 2018 8:18:13 GMT
Hi R, Thanks, done. C.
|
|