Calculation or phrase problem !

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times

Calculation or phrase problem !

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Calculation or phrase problem !

Post 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 )

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times

Re: Calculation or phrase problem !

Post by acestu »

Hi Benj,

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

cheers
Acestu
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times

Re: Calculation or phrase problem !

Post by acestu »

Hi Benj,

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

thanks
Stuart
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Calculation or phrase problem !

Post 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

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times

Re: Calculation or phrase problem !

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Calculation or phrase problem !

Post 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.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times

Re: Calculation or phrase problem !

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

elprofe_tellez
Posts: 3
Joined: Wed Jan 05, 2011 4:06 pm
Been thanked: 1 time

Re: Calculation or phrase problem !

Post 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.

User avatar
acestu
Posts: 1720
Joined: Thu Dec 01, 2011 9:36 pm
Location: Macclesfield UK
Has thanked: 783 times
Been thanked: 223 times

Re: Calculation or phrase problem !

Post 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
Laptop Mac Book Pro i7 retina El Capitan //// Tower/Intel i7-Windows 7 64 Bit, Toshiba i5 Laptop Windows 10
Computers are like air conditioners. They work fine until you start opening windows.

elprofe_tellez
Posts: 3
Joined: Wed Jan 05, 2011 4:06 pm
Been thanked: 1 time

Re: Calculation or phrase problem !

Post 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!

Post Reply