2019-03-23 08:56 AM
Hi team,
I am working on LIS3DH sensor, and found a lot resource from internet.
But I go confused when I saw difference formula to calculate the OUTPUT_X or OUTPUT_Y.
Source1:
dt_usr[0] = float(short((data[1] << 8) | data[0]) >> 4) * fs_factor;
https://os.mbed.com/users/electronichamsters/code/LIS3DH/file/c32d2b25d4c2/LIS3DH.cpp/
source2:
dev->accX = INT16_TO_FLOAT(buf[1], buf[0]);
https://github.com/intel-iot-devkit/upm/blob/master/src/lis3dh/lis3dh.c
My question is why the source1 need to do " >> 4"? where can I get the detail information?
Could you have example code to calculate the OUTPUT_X,Y,Z?
Thanks.
2019-03-26 08:59 AM
hi, you simply have to concatenate OUT_X_L (28h) and OUT_X_H (29h) and pay attention to the operating mode you set. The mode determines the output value bit format (8bit, 10bit or 12 bit) and the LSB to physical units factor (e.g. for FS +-2g, high resolution it is 1 mg/digit)
In case of 12 bits, you first concatenate on 16 bits and then disregard the 4 LSB (that is, shift right the 16bit value to get a 12bit value): e.g. you read 15h and E0h on 29h and 28h, you concatenate the values in the order 15E0h on 16 bits and then you have to disregard the last 0h, obtaining 15Eh on 12 bits (equivalent of shift right 4 position), which is 350 decimal. Finally multiply this value for the sensitivity factor 1mg/digit to get 350mg in physical units.
This example is from LIS3DH app note:
You can btw check this online code on github https://github.com/adafruit/Adafruit_LIS3DH/blob/master/Adafruit_LIS3DH.cpp, referring to the section of
I2Cinterface->beginTransmission(_i2caddr);
I2Cinterface->write(LIS3DH_REG_OUT_X_L | 0x80); // 0x80 for autoincrement
I2Cinterface->endTransmission();
I2Cinterface->requestFrom(_i2caddr, 6);
x = I2Cinterface->read(); x |= ((uint16_t)I2Cinterface->read()) << 8;
y = I2Cinterface->read(); y |= ((uint16_t)I2Cinterface->read()) << 8;
z = I2Cinterface->read(); z |= ((uint16_t)I2Cinterface->read()) << 8;
Regards