Skip to main content
Visitor II
May 6, 2021
Solved

Hi, Not quite sure on how to extract data from LIS2DH12 in 12 bit mode.

  • May 6, 2021
  • 2 replies
  • 825 views

It appears to be a 2's complement left justified value.

But making it simple, how do I convert this data... say for a start:

au8_l_Data[0] is the low byte returned from the MEMS (OUT_X_L)

au8_l_Data[1] is the high byte returned from the MEMS (OUT_X_H)

Then in my code:    

u16_l_Temp = (uint16_t)au8_l_Data[0];

u16_l_Temp += ((uint16_t)au8_l_Data[1] << 8);

Until here all is ok... but now how do I simply convert this unsigned 16 bit value (u16_l_Temp) into a 16 bit signed value? Simply casting this value to a int16 shouldn't do it if it is a 2's complement... or am I misunderstanding something?

Thanks

Phil

    This topic has been closed for replies.
    Best answer by raptorhal2

    Since the data is specified as 2's complement, I suspect it is already signed, so code it as int16_t.

    A good check is to simply examine the data with the sensor at rest. If signed, the readings should be 2 axes close to zero, and a third axis close to 1g. If unsigned, there will have to be a half of full scale bias to provide for negative g's.

    Cheers, Hal

    2 replies

    Graduate
    May 6, 2021

    Since the data is specified as 2's complement, I suspect it is already signed, so code it as int16_t.

    A good check is to simply examine the data with the sensor at rest. If signed, the readings should be 2 axes close to zero, and a third axis close to 1g. If unsigned, there will have to be a half of full scale bias to provide for negative g's.

    Cheers, Hal

    PBalz.1Author
    Visitor II
    May 7, 2021

    Perfect Hal,

    I tested it out just a minute ago.

    No problem casting it directly to a signed value and I get the +1g and the -1g.

    Thanks

    Phil