HEPTA-Sat Training 2019
/
Lab4-02_Detect_acceleration_x-axis
Lab4
Diff: main.cpp
- Revision:
- 24:ca9f33c96136
- Parent:
- 21:92c25e853b87
- Child:
- 25:864883d3fe57
diff -r 198d7b6e1dd5 -r ca9f33c96136 main.cpp --- a/main.cpp Wed Aug 21 12:22:21 2019 +0000 +++ b/main.cpp Tue Aug 27 01:00:32 2019 +0000 @@ -1,26 +1,41 @@ #include "mbed.h" -#include"HEPTA_EPS.h" -#include"HEPTA_CDH.h" -#include"HEPTA_SENSOR.h" -#include"HEPTA_COM.h" +#include "HEPTA_EPS.h" Serial pc(USBTX,USBRX); HEPTA_EPS eps(p16,p26); -HEPTA_CDH cdh(p5, p6, p7, p8, "sd"); -/*HEPTA_SENSOR sensor(p13, p14,p25,p24, - p28,p27,0xD0,0x18, - p17);*/ -HEPTA_SENSOR sensor(p17, - p28,p27,0xD0,0x18, - p13, p14,p25,p24); -HEPTA_COM com(p9,p10); +I2C i2c(p28, p27); -DigitalOut myled(LED1); - +const int addr_accel_gyro = 0xD0; +char cmd[2]; +short int xl,xh,yl,yh,zl,zh; int main() { - myled = 1; - wait(0.5); - myled = 0; - wait(0.5); + i2c.frequency(100000); + cmd[0]=0x6B; + cmd[1]=0x00; + i2c.write(addr_accel_gyro,cmd,2); + cmd[0] = 0x37; + cmd[1] = 0x02; + i2c.write(addr_accel_gyro,cmd,2); + i2c.stop(); + while(1) { + i2c.start(); + i2c.write(addr_accel_gyro); + i2c.write(0x3b);//read adress + i2c.start(); + i2c.write(addr_accel_gyro|0x01); + xh = i2c.read(0); + i2c.stop(); + i2c.start(); + i2c.write(addr_accel_gyro); + i2c.write(0x3c); + i2c.start(); + i2c.write(addr_accel_gyro|0x01); + xl = i2c.read(0); + i2c.stop(); + double acc_ax = short((xh<<8) | (xl)); + double AX = (acc_ax)*2/32764*9.81; + pc.printf("AX = %f\r\n",AX); + wait(0.5); + } } \ No newline at end of file