Frederic Thierry
/
Nucleo_test_accelero
Only for Future hands-on
main.cpp@1:4eaf2ea6eb62, 2016-03-07 (annotated)
- Committer:
- Future_FThierry
- Date:
- Mon Mar 07 17:38:22 2016 +0000
- Revision:
- 1:4eaf2ea6eb62
- Parent:
- 0:24c9d0b3277e
demo LSM6;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Future_FThierry | 0:24c9d0b3277e | 1 | #include "mbed.h" |
Future_FThierry | 1:4eaf2ea6eb62 | 2 | #include "math.h" |
Future_FThierry | 1:4eaf2ea6eb62 | 3 | #include <FT_LSM6DS0.h> |
Future_FThierry | 0:24c9d0b3277e | 4 | |
Future_FThierry | 0:24c9d0b3277e | 5 | I2C i2c(I2C_SDA, I2C_SCL); |
Future_FThierry | 0:24c9d0b3277e | 6 | |
Future_FThierry | 0:24c9d0b3277e | 7 | DigitalOut myled(LED1); |
Future_FThierry | 0:24c9d0b3277e | 8 | |
Future_FThierry | 0:24c9d0b3277e | 9 | Serial pc(SERIAL_TX, SERIAL_RX); |
Future_FThierry | 0:24c9d0b3277e | 10 | |
Future_FThierry | 0:24c9d0b3277e | 11 | int main() |
Future_FThierry | 0:24c9d0b3277e | 12 | { |
Future_FThierry | 0:24c9d0b3277e | 13 | char data_write[6]; |
Future_FThierry | 0:24c9d0b3277e | 14 | char data_read[6]; |
Future_FThierry | 1:4eaf2ea6eb62 | 15 | short X_Acc, Y_Acc; |
Future_FThierry | 1:4eaf2ea6eb62 | 16 | float alpha; |
Future_FThierry | 0:24c9d0b3277e | 17 | |
Future_FThierry | 0:24c9d0b3277e | 18 | data_write[0] = LSM6DS0_WHO_AM_I; |
Future_FThierry | 0:24c9d0b3277e | 19 | i2c.write(LSM6DS0_ADDR, data_write, 1, 1); // no stop |
Future_FThierry | 0:24c9d0b3277e | 20 | i2c.read(LSM6DS0_ADDR, data_read, 2, 0); |
Future_FThierry | 0:24c9d0b3277e | 21 | |
Future_FThierry | 0:24c9d0b3277e | 22 | pc.printf("who am I= %2x%c%c",data_read[0],0x0D,0X0A); |
Future_FThierry | 0:24c9d0b3277e | 23 | |
Future_FThierry | 0:24c9d0b3277e | 24 | data_write[0]=LSM6DS0_CTRL_REG6_XL; |
Future_FThierry | 0:24c9d0b3277e | 25 | data_write[1]=0x20; // Set output Data Rate to 10Hz, exiting sleep mode |
Future_FThierry | 0:24c9d0b3277e | 26 | |
Future_FThierry | 0:24c9d0b3277e | 27 | i2c.write(LSM6DS0_ADDR, data_write,2,0); |
Future_FThierry | 0:24c9d0b3277e | 28 | |
Future_FThierry | 0:24c9d0b3277e | 29 | data_write[0]=LSM6DS0_CTRL_REG5_XL; |
Future_FThierry | 1:4eaf2ea6eb62 | 30 | data_write[1]=0xD8; //Enable X-Axis Accelerometer |
Future_FThierry | 0:24c9d0b3277e | 31 | |
Future_FThierry | 0:24c9d0b3277e | 32 | i2c.write(LSM6DS0_ADDR, data_write,2,0); |
Future_FThierry | 0:24c9d0b3277e | 33 | |
Future_FThierry | 0:24c9d0b3277e | 34 | while(1) |
Future_FThierry | 0:24c9d0b3277e | 35 | { |
Future_FThierry | 0:24c9d0b3277e | 36 | data_write[0]=LSM6DS0_OUT_X_XL; |
Future_FThierry | 0:24c9d0b3277e | 37 | i2c.write(LSM6DS0_ADDR, data_write, 1, 1); // no stop |
Future_FThierry | 1:4eaf2ea6eb62 | 38 | i2c.read(LSM6DS0_ADDR, data_read,4,0); |
Future_FThierry | 0:24c9d0b3277e | 39 | |
Future_FThierry | 0:24c9d0b3277e | 40 | X_Acc= (short)(((unsigned char)data_read[1]) << 8 | ((unsigned char)data_read[0])); |
Future_FThierry | 1:4eaf2ea6eb62 | 41 | Y_Acc= (short)(((unsigned char)data_read[3]) << 8 | ((unsigned char)data_read[2])); |
Future_FThierry | 0:24c9d0b3277e | 42 | |
Future_FThierry | 1:4eaf2ea6eb62 | 43 | alpha= 180*atan2((double)Y_Acc,(double)X_Acc)/3.14159; |
Future_FThierry | 1:4eaf2ea6eb62 | 44 | |
Future_FThierry | 1:4eaf2ea6eb62 | 45 | pc.printf("X_Acc= %d Y_Acc= %d alpha= %.2f \n",X_Acc,Y_Acc,alpha); |
Future_FThierry | 0:24c9d0b3277e | 46 | wait(1); |
Future_FThierry | 0:24c9d0b3277e | 47 | } |
Future_FThierry | 0:24c9d0b3277e | 48 | |
Future_FThierry | 0:24c9d0b3277e | 49 | } |