diff --git a/drivers/include/mma8652.h b/drivers/include/mma8652.h
index 15397fc680..08534eaedb 100644
--- a/drivers/include/mma8652.h
+++ b/drivers/include/mma8652.h
@@ -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;
/**
@@ -90,7 +91,8 @@ int mma8652_test(mma8652_t *dev);
* @return -1 if parameters are wrong
* @return -2 if initialization of I2C bus failed
* @return -3 if accelerometer test failed
- * @return -4 if accelerometer configuration failed
+ * @return -4 if setting to STANDBY mode failed
+ * @return -5 if accelerometer configuration failed
*/
int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t range);
@@ -151,15 +153,15 @@ int mma8652_is_ready(mma8652_t *dev);
/**
* @brief Read accelerometer's data.
- * Acceleration can be calculated as:
- * \f$ a = \frac{value}{1024} \cdot g \f$ if full scale is set to 2g
- * \f$ a = \frac{value}{512} \cdot g \f$ if full scale is set to 4g
- * \f$ a = \frac{value}{256} \cdot g \f$ if full scale is set to 8g
+ * Acceleration will be calculated as:
+ * \f$ a = \frac{value \cdot 1000}{1024} \cdot mg \f$ if full scale is set to 2g
+ * \f$ a = \frac{value \cdot 1000}{512} \cdot mg \f$ if full scale is set to 4g
+ * \f$ a = \frac{value \cdot 1000}{256} \cdot mg \f$ if full scale is set to 8g
*
* @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
diff --git a/drivers/mma8652/mma8652.c b/drivers/mma8652/mma8652.c
index 99ae290238..7c330e50b3 100644
--- a/drivers/mma8652/mma8652.c
+++ b/drivers/mma8652/mma8652.c
@@ -76,23 +76,28 @@ int mma8652_init(mma8652_t *dev, i2c_t i2c, uint8_t address, uint8_t dr, uint8_t
return -3;
}
+ if (mma8652_set_standby(dev) < 0) {
+ return -4;
+ }
+
reg = MMA8652_XYZ_DATA_CFG_FS(range);
i2c_acquire(dev->i2c);
if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_XYZ_DATA_CFG, ®, 1) != 1) {
i2c_release(dev->i2c);
- return -4;
+ return -5;
}
reg = MMA8652_CTRL_REG1_DR(dr);
if (i2c_write_regs(dev->i2c, dev->addr, MMA8652_CTRL_REG1, ®, 1) != 1) {
i2c_release(dev->i2c);
- return -4;
+ return -5;
}
i2c_release(dev->i2c);
dev->initialized = true;
+ dev->scale = 1024 >> range;
return 0;
}
@@ -212,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;
}
diff --git a/tests/driver_mma8652/main.c b/tests/driver_mma8652/main.c
index db0d954a60..f9b8d832a5 100644
--- a/tests/driver_mma8652/main.c
+++ b/tests/driver_mma8652/main.c
@@ -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);
}