drivers/mma8652: convert raw data to milli g

pr/gpio
Cenk Gündoğan 7 years ago
parent 192f20eabe
commit 37c2cddf32

@ -64,6 +64,7 @@ typedef struct {
i2c_t i2c; /**< I2C device, the accelerometer is connected to */
uint8_t addr; /**< the accelerometer's slave address on the I2C bus */
bool initialized; /**< accelerometer status, true if accelerometer is initialized */
int16_t scale; /**< each count corresponds to (1/scale) g */
} mma8652_t;
/**
@ -152,15 +153,15 @@ int mma8652_is_ready(mma8652_t *dev);
/**
* @brief Read accelerometer's data.
* Acceleration can be calculated as:<br>
* \f$ a = \frac{value}{1024} \cdot g \f$ if full scale is set to 2g<br>
* \f$ a = \frac{value}{512} \cdot g \f$ if full scale is set to 4g<br>
* \f$ a = \frac{value}{256} \cdot g \f$ if full scale is set to 8g<br>
* Acceleration will be calculated as:<br>
* \f$ a = \frac{value \cdot 1000}{1024} \cdot mg \f$ if full scale is set to 2g<br>
* \f$ a = \frac{value \cdot 1000}{512} \cdot mg \f$ if full scale is set to 4g<br>
* \f$ a = \frac{value \cdot 1000}{256} \cdot mg \f$ if full scale is set to 8g<br>
*
* @param[in] dev device descriptor of accelerometer
* @param[out] x x-axis value
* @param[out] y y-axis value
* @param[out] z z-axis value
* @param[out] x x-axis value in mg
* @param[out] y y-axis value in mg
* @param[out] z z-axis value in mg
* @param[out] status accelerometer status register
*
* @return 0 on success

@ -97,6 +97,7 @@ int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t
i2c_release(dev->i2c);
dev->initialized = true;
dev->scale = 1024 >> range;
return 0;
}
@ -216,9 +217,9 @@ int mma8652_read(mma8652_t *dev, int16_t *x, int16_t *y, int16_t *z, uint8_t *st
i2c_release(dev->i2c);
*status = buf[0];
*x = (int16_t)(((int16_t)buf[1] << 8) | buf[2]) / 16;
*y = (int16_t)(((int16_t)buf[3] << 8) | buf[4]) / 16;
*z = (int16_t)(((int16_t)buf[5] << 8) | buf[6]) / 16;
*x = (int32_t)((int16_t)(((int16_t)buf[1] << 8) | buf[2]) >> 4) * 1000 / dev->scale;
*y = (int32_t)((int16_t)(((int16_t)buf[3] << 8) | buf[4]) >> 4) * 1000 / dev->scale;
*z = (int32_t)((int16_t)(((int16_t)buf[5] << 8) | buf[6]) >> 4) * 1000 / dev->scale;
return 0;
}

@ -67,7 +67,7 @@ int main(void)
while (1) {
mma8652_read(&dev, &x, &y, &z, &status);
printf("Acceleration, raw: X: %d Y: %d Z: %d S: %2x\n", x, y, z, status);
printf("Acceleration, in mg: X: %d Y: %d Z: %d S: %2x\n", x, y, z, status);
xtimer_usleep(SLEEP);
}

Loading…
Cancel
Save