Page 1 of 1

Calculation or phrase problem !

Posted: Wed Jan 08, 2014 12:13 am
by acestu
Hi,

Could somebody please help me with my i2c compass project please, I have read the data which is 6 lots of 8 bits, which is read from 6 registers, however it is 3 x 16 bit values that I need, so i need a formula or a phrase that puts the 2 x 8 bit numbers together before I can make other calculations, I hope I have explained that properly :?

Thanks In Advance
Acestu

Re: Calculation or phrase problem !

Posted: Wed Jan 08, 2014 10:13 am
by Benj
Hi Stu,

To combine two bytes together into a 16-bit unsigned variable you can simply do this in a calculation.

intvar = bytelo | ( bytehi << 8 )

Re: Calculation or phrase problem !

Posted: Wed Jan 08, 2014 2:56 pm
by acestu
Hi Benj,

Thanks for that info, I will give it a twirl tonight when I get in...

cheers
Acestu

Re: Calculation or phrase problem !

Posted: Wed Jan 08, 2014 6:31 pm
by acestu
Hi Benj,

just checking, is bytelow the furthest right and LSB , and bytehigh furthest left and MSB ?

thanks
Stuart

Re: Calculation or phrase problem !

Posted: Wed Jan 08, 2014 6:35 pm
by Benj
Hi Stuart,

With binary you have the MSB on the left and the LSB on the right. The ByteLo is the least significant byte and the ByteHi is the most significant.

e.g. ByteHi = 0xFF, ByteLo = 0x00

Combined = 0xFF00

Re: Calculation or phrase problem !

Posted: Wed Jan 08, 2014 7:46 pm
by acestu
Hi Benj,

I have done as you say here:

Code: Select all

raw_x = x2 | (x1 << 8)
raw_z = z2 | (z1 << 8)
raw_y = y2 | (y1 << 8)
This should give me the 3 x 16 bit values, but what does this mean ?

Convert three 16-bit 2’s compliment hex values to decimal values and assign to X, Z, Y,

EDIT: Does it mean that the 2's compliment part has already been done now and raw_x, raw_z, and raw_y and they need turning into Decimal ?


Thanks
Stuart

Re: Calculation or phrase problem !

Posted: Thu Jan 09, 2014 10:29 am
by Benj
Hi Stuart,

The two's compliment is basically to allow the number to be negative and will be worked out for you by the external module. If you print out the raw values you have now then they should be correct for what you need.

I'm guessing x2 is the least significant byte and x1 is the most significant.

Binary, hexadecimal and decimal are different ways of assigning the same value to a variable or register. E.g. you don't have to do any conversions to get the value in decimal.

Re: Calculation or phrase problem !

Posted: Thu Jan 09, 2014 6:21 pm
by acestu
Hi Benj,

I am just trying to follow the Datasheet step by step, this is the part I am on at the moment:
data_sheet_excert.png
(17.15 KiB) Downloaded 4872 times
I managed to work out how to read the 6 registers and assign the values to variables, the datasheet told which were MSB and LSB , but then as far as I could see I had to convert to 3 x 16 bit numbers, and then convert to Decimal according to the sheet...

cheers
Stuart

Re: Calculation or phrase problem !

Posted: Mon Jan 27, 2014 6:20 am
by elprofe_tellez
Hi acestu, I have same problem to read a GY-271 compass, and I had tried whitout a good result. Tell me please, are your testings ok?. Greetings.

Re: Calculation or phrase problem !

Posted: Wed Jan 29, 2014 1:49 pm
by acestu
Hi Elprofe,

Welcome to the forums, I am just waiting for some information on this project, I will update later and let you know how I go on...

cheers
Acestu

Re: Calculation or phrase problem !

Posted: Sun Apr 27, 2014 5:00 am
by elprofe_tellez
Hi Acestu, have you already solve the problem?, I have thte GY271 sensor and I can't to take the righ meassurements.
Gretings!