Frederic Thierry
/
Nucleo_test_accelero
Only for Future hands-on
Diff: main.cpp
- Revision:
- 1:4eaf2ea6eb62
- Parent:
- 0:24c9d0b3277e
--- a/main.cpp Thu Dec 11 00:25:34 2014 +0000 +++ b/main.cpp Mon Mar 07 17:38:22 2016 +0000 @@ -1,5 +1,6 @@ #include "mbed.h" -#include <LSM6DS0.h> +#include "math.h" +#include <FT_LSM6DS0.h> I2C i2c(I2C_SDA, I2C_SCL); @@ -11,7 +12,8 @@ { char data_write[6]; char data_read[6]; - short X_Acc; + short X_Acc, Y_Acc; + float alpha; data_write[0] = LSM6DS0_WHO_AM_I; i2c.write(LSM6DS0_ADDR, data_write, 1, 1); // no stop @@ -25,7 +27,7 @@ i2c.write(LSM6DS0_ADDR, data_write,2,0); data_write[0]=LSM6DS0_CTRL_REG5_XL; - data_write[1]=0x08; //Enable X-Axis Accelerometer + data_write[1]=0xD8; //Enable X-Axis Accelerometer i2c.write(LSM6DS0_ADDR, data_write,2,0); @@ -33,11 +35,14 @@ { data_write[0]=LSM6DS0_OUT_X_XL; i2c.write(LSM6DS0_ADDR, data_write, 1, 1); // no stop - i2c.read(LSM6DS0_ADDR, data_read,2,0); + i2c.read(LSM6DS0_ADDR, data_read,4,0); X_Acc= (short)(((unsigned char)data_read[1]) << 8 | ((unsigned char)data_read[0])); + Y_Acc= (short)(((unsigned char)data_read[3]) << 8 | ((unsigned char)data_read[2])); - pc.printf("X_Acc= %d \n",X_Acc); + alpha= 180*atan2((double)Y_Acc,(double)X_Acc)/3.14159; + + pc.printf("X_Acc= %d Y_Acc= %d alpha= %.2f \n",X_Acc,Y_Acc,alpha); wait(1); }