Detecting z-axis acceralation

Dependencies:   mbed

Committer:
RyusukeIwata
Date:
Tue Aug 03 06:57:44 2021 +0000
Revision:
0:99043f8d668d
first commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RyusukeIwata 0:99043f8d668d 1 //icm20601 最新版
RyusukeIwata 0:99043f8d668d 2 #include "mbed.h"
RyusukeIwata 0:99043f8d668d 3 Serial pc(USBTX, USBRX);
RyusukeIwata 0:99043f8d668d 4 I2C i2c(PB_7,PB_6);
RyusukeIwata 0:99043f8d668d 5 DigitalOut conv(PA_4);
RyusukeIwata 0:99043f8d668d 6
RyusukeIwata 0:99043f8d668d 7 const int addr_accel_gyro = 0xD0;
RyusukeIwata 0:99043f8d668d 8 char cmd[2];
RyusukeIwata 0:99043f8d668d 9 char data[1];
RyusukeIwata 0:99043f8d668d 10 char xh[1];
RyusukeIwata 0:99043f8d668d 11 char xl[1];
RyusukeIwata 0:99043f8d668d 12
RyusukeIwata 0:99043f8d668d 13 int main()
RyusukeIwata 0:99043f8d668d 14 {
RyusukeIwata 0:99043f8d668d 15 conv = 1;
RyusukeIwata 0:99043f8d668d 16 i2c.frequency(100000);
RyusukeIwata 0:99043f8d668d 17 char cmdd;
RyusukeIwata 0:99043f8d668d 18 wait(2);
RyusukeIwata 0:99043f8d668d 19 data[0] = 0x75;
RyusukeIwata 0:99043f8d668d 20 i2c.write(addr_accel_gyro,data,1);
RyusukeIwata 0:99043f8d668d 21 i2c.read(addr_accel_gyro|0x01,xh,1);
RyusukeIwata 0:99043f8d668d 22 pc.printf("xh = 0x%02X\r\n",xh[0]);
RyusukeIwata 0:99043f8d668d 23 while(1){
RyusukeIwata 0:99043f8d668d 24 cmdd = pc.getc();
RyusukeIwata 0:99043f8d668d 25 if(cmdd == 'a'){
RyusukeIwata 0:99043f8d668d 26 data[0] = 0x75;
RyusukeIwata 0:99043f8d668d 27 i2c.write(addr_accel_gyro,data,1);
RyusukeIwata 0:99043f8d668d 28 i2c.read(addr_accel_gyro|0x01,xh,1);
RyusukeIwata 0:99043f8d668d 29 pc.printf("xh = 0x%02X\r\n",xh[0]);
RyusukeIwata 0:99043f8d668d 30
RyusukeIwata 0:99043f8d668d 31 cmd[0]=0x6B;
RyusukeIwata 0:99043f8d668d 32 cmd[1]=0x00;
RyusukeIwata 0:99043f8d668d 33 i2c.write(addr_accel_gyro,cmd,2);
RyusukeIwata 0:99043f8d668d 34
RyusukeIwata 0:99043f8d668d 35 cmd[0]=0x6C;
RyusukeIwata 0:99043f8d668d 36 cmd[1]=0x00;
RyusukeIwata 0:99043f8d668d 37 i2c.write(addr_accel_gyro,cmd,2);
RyusukeIwata 0:99043f8d668d 38
RyusukeIwata 0:99043f8d668d 39
RyusukeIwata 0:99043f8d668d 40 cmd[0] = 0x37;
RyusukeIwata 0:99043f8d668d 41 cmd[1] = 0x02;
RyusukeIwata 0:99043f8d668d 42 i2c.write(addr_accel_gyro,cmd,2);
RyusukeIwata 0:99043f8d668d 43
RyusukeIwata 0:99043f8d668d 44
RyusukeIwata 0:99043f8d668d 45 while(1) {
RyusukeIwata 0:99043f8d668d 46 data[0] = 0x3F;
RyusukeIwata 0:99043f8d668d 47 i2c.write(addr_accel_gyro,data,1);
RyusukeIwata 0:99043f8d668d 48 i2c.read(addr_accel_gyro|0x01,xh,1);
RyusukeIwata 0:99043f8d668d 49 data[0] = 0x40;
RyusukeIwata 0:99043f8d668d 50 i2c.write(addr_accel_gyro,data,1);
RyusukeIwata 0:99043f8d668d 51 i2c.read(addr_accel_gyro|0x01,xl,1);
RyusukeIwata 0:99043f8d668d 52 //pc.printf("xh = 0x%02X,xl = 0x%02X\r\n",xh[0],xl[0]);
RyusukeIwata 0:99043f8d668d 53 double acc_ax = short((xh[0]<<8) | (xl[0]));
RyusukeIwata 0:99043f8d668d 54 double AX = (acc_ax)*4/32764*9.81;
RyusukeIwata 0:99043f8d668d 55 pc.printf("AZ = %f\r\n",AX);
RyusukeIwata 0:99043f8d668d 56 wait(0.5);
RyusukeIwata 0:99043f8d668d 57 }
RyusukeIwata 0:99043f8d668d 58 }
RyusukeIwata 0:99043f8d668d 59 }
RyusukeIwata 0:99043f8d668d 60
RyusukeIwata 0:99043f8d668d 61
RyusukeIwata 0:99043f8d668d 62
RyusukeIwata 0:99043f8d668d 63 }