Detecting z-axis acceralation

Dependencies:   mbed

Revision:
0:99043f8d668d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Aug 03 06:57:44 2021 +0000
@@ -0,0 +1,63 @@
+//icm20601 最新版
+#include "mbed.h"
+Serial pc(USBTX, USBRX);
+I2C i2c(PB_7,PB_6);
+DigitalOut conv(PA_4);
+
+const int addr_accel_gyro = 0xD0;
+char cmd[2];
+char data[1];
+char xh[1];
+char xl[1];
+
+int main()
+{
+    conv = 1;
+    i2c.frequency(100000);
+    char cmdd;
+    wait(2);
+    data[0] = 0x75;
+    i2c.write(addr_accel_gyro,data,1);
+    i2c.read(addr_accel_gyro|0x01,xh,1);
+    pc.printf("xh = 0x%02X\r\n",xh[0]);
+    while(1){
+        cmdd = pc.getc();
+        if(cmdd == 'a'){
+            data[0] = 0x75;
+            i2c.write(addr_accel_gyro,data,1);
+            i2c.read(addr_accel_gyro|0x01,xh,1);
+            pc.printf("xh = 0x%02X\r\n",xh[0]);
+            
+            cmd[0]=0x6B;
+            cmd[1]=0x00;
+            i2c.write(addr_accel_gyro,cmd,2);
+            
+            cmd[0]=0x6C;
+            cmd[1]=0x00;
+            i2c.write(addr_accel_gyro,cmd,2);
+            
+            
+            cmd[0] = 0x37;
+            cmd[1] = 0x02;
+            i2c.write(addr_accel_gyro,cmd,2);
+            
+            
+            while(1) {
+                data[0] = 0x3F;
+                i2c.write(addr_accel_gyro,data,1);
+                i2c.read(addr_accel_gyro|0x01,xh,1);
+                data[0] = 0x40;
+                i2c.write(addr_accel_gyro,data,1);
+                i2c.read(addr_accel_gyro|0x01,xl,1);
+                //pc.printf("xh = 0x%02X,xl = 0x%02X\r\n",xh[0],xl[0]);
+                double  acc_ax = short((xh[0]<<8) | (xl[0]));
+                double AX = (acc_ax)*4/32764*9.81;
+                pc.printf("AZ = %f\r\n",AX);
+                wait(0.5);
+            }
+        }
+    }
+    
+    
+    
+}
\ No newline at end of file