LIS2DH12 HOWTO Convert Register value to mg value
Hi,
I am using the LIS2DH12 to interrupt me on an inertial wake. I am running in Low Power mode with a scale of 4G. After verifying that the X axis produced the force that caused the interrupt I read the X_OUT_L and X_OUT_H registers to get the 2's complement value in the X_OUT register. I want to calculate the actual force in mg that caused the wake. Can someone verify that I am doing this correctly?
OUT_X register contains 0xFC00
I use these functions in my calculation
int16_t twos_complement_to_decimal(int num_bits, int16_t num) {
// Create a mask to isolate the sign bit (most significant bit)
int sign_bit_mask = 1 << (num_bits - 1);
// If the sign bit is set (number is negative)
if (num & sign_bit_mask) {
// Invert all bits, add 1, and negate to get the decimal value
return -((~num & ((1 << num_bits) - 1)) + 1);
} else {
// If the sign bit is not set (number is positive), return the number as is
return num;
}
}
float_t lis2dh12_from_fs4_lp_to_mg(int16_t lsb)
{
return ((float_t)lsb / 256.0f) * 32.0f;
}
Here is what I think I should be doing to calculate the mg value...
int16_t iVal = (int16_t)twos_complement_to_decimal(16, 0xF900);
float_t fVal = lis2dh12_from_fs4_lp_to_mg(iVal);
Thanks for your help..
