## How to get 11 bits out of 2 bytes ?

12 Nov 2010

Hi all..

a C Question.

I'm reading 2 bytes out of a register on an I2C chip.

I put them in 2 char fields.

data[0], data[1]

The value is a 2's complement number, and its 11 bits (naturally left justified), with the remaining 5 bits not used.

How on earth do I get those 11 bits into a float.

Google (after 4 hours googling) has just not been my friend.

any help would be much appreciated.

12 Nov 2010

read them as a 16 bit int?

if its bits 15< >5 you want, right shift by 5. (4,3,2,1,0 not needed)

force the conversion as

float temp;

temp = dataint;

12 Nov 2010

Try something like: CombinedData = (data[0]<<8) + (data[1]);

You will need to convert from 2's compliment first which involes subtracting 1 then XOR all the bits if I remember correctly.

12 Nov 2010

Hi Chaps,..

I appreciate the replies, I really do, the problem is that I'm a Java Programmer by trade, this bitwise stuff is gobbledygook to me, but I'm learning !

the CombinedData - what type would that be ? some sort of 16 bit type ? and how would I convert from 2's complement first? When its still in 2x8 byte fields.

Do I really have to "manually" sort out the 2's complement ?

The thing is.. I was hoping this was a really normal thing to do for a C programmer, sort of bread and butter sort of thing, as it looks like lots of I2C stuff has these odd registers, but do you know, I just can't find a standard way of doing it.

any further help still appreciated !

12 Nov 2010 . Edited: 12 Nov 2010

int will do it.

Integers on the mBed are 32 bits so an int will hold it in the lower word.

is the value 2's complement all together or before combining ( unlikely )

What I2C chip are you looking at might be easier if we knew.

12 Nov 2010

Check the NXP LM75 example:

LM75:

The temperature register always stores an 11-bit 2's complement data giving a
temperature resolution of 0.125 °C.

int temp_short( void )
{
char    data[ 2 ];

data[ 0 ]    = 0;

if ( i2c.write( device_address, data, 1 ) )
;

;

return ( (((short)data[ 0 ]) << 8 | data[ 1 ]) >> 5 );
}

float temp( void ) {
return ( (float)(temp_short()) / 8.0 );
}

Regards,

Sjoerd

12 Nov 2010

That's brilliant, a massive THANKYOU !!

It's an SE95D temp sensor, (in it's datasheet it says its a direct, more accurate replacement for an LM75, but I'd missed there was an LM75 example)

I have borrowed heavily from the TMP102 library on the site to write a library.

Which I shall publish. If we all publish libraries for anything we use, suddenly we'll all have loads of bits we can just pick up and use !

Now, it will go out to 13 bits ! (I shall see if I can get it working at 13 bits!)

I had it datalogging the temperature in my dining room overnight last night (at 8 bits / 1 degrees C resolution).

it came up with an 11k .csv file, containing a record of temperature against time, taken at one minute intervals overnight.

I have a nice graph in excel this morning. You can see when the heating came on at 7 !

Tonight, it's getting a battery and going in the shed. What fun eh?!

cheers

Dave.

12 Nov 2010

If you browse to pachube.com ( and search it on this site ) , write JSON data instead of .csv's you can get it all graphed out for you too :)

12 Nov 2010

You can also create an html page with javascript querying your sensor data and plot it in realtime using http://code.google.com/p/flot/  :)

Great fun :)

Sjoerd