3軸加速度の表示

Dependencies:   mbed

Committer:
wasabimal
Date:
Sun Jan 23 04:27:39 2022 +0000
Revision:
0:5b22d0b5610e
a

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wasabimal 0:5b22d0b5610e 1 #include "mbed.h"
wasabimal 0:5b22d0b5610e 2
wasabimal 0:5b22d0b5610e 3 Serial pc(USBTX,USBRX);
wasabimal 0:5b22d0b5610e 4 I2C i2c(p28, p27);
wasabimal 0:5b22d0b5610e 5
wasabimal 0:5b22d0b5610e 6 const int addr_accel_gyro = 0xD0; //9軸センサ自体のアドレス
wasabimal 0:5b22d0b5610e 7 char cmd[2];
wasabimal 0:5b22d0b5610e 8 short int xl,xh,yl,yh,zl,zh;
wasabimal 0:5b22d0b5610e 9 int main()
wasabimal 0:5b22d0b5610e 10 {
wasabimal 0:5b22d0b5610e 11 i2c.frequency(100000);
wasabimal 0:5b22d0b5610e 12 // cmd[0]=0x6B;
wasabimal 0:5b22d0b5610e 13 // cmd[1]=0x00;
wasabimal 0:5b22d0b5610e 14 // i2c.write(addr_accel_gyro,amd,2);
wasabimal 0:5b22d0b5610e 15 // cmd[0] = 0x37;
wasabimal 0:5b22d0b5610e 16 // cmd[1] = 0x02;
wasabimal 0:5b22d0b5610e 17 // i2c.write(adde_accel_gyro,cmd,2);
wasabimal 0:5b22d0b5610e 18 // i2c.stop();
wasabimal 0:5b22d0b5610e 19 pc.printf("AX Start!!\r\n");
wasabimal 0:5b22d0b5610e 20 pc.printf("AY Start!!\r\n");
wasabimal 0:5b22d0b5610e 21 pc.printf("AZ Start!!\r\n");
wasabimal 0:5b22d0b5610e 22
wasabimal 0:5b22d0b5610e 23 while(1) {
wasabimal 0:5b22d0b5610e 24 i2c.start();
wasabimal 0:5b22d0b5610e 25 i2c.write(addr_accel_gyro);
wasabimal 0:5b22d0b5610e 26 i2c.write(0x3b);//read adress
wasabimal 0:5b22d0b5610e 27 i2c.start();
wasabimal 0:5b22d0b5610e 28 i2c.write(addr_accel_gyro|0x01);
wasabimal 0:5b22d0b5610e 29 xh = i2c.read(0);
wasabimal 0:5b22d0b5610e 30 i2c.stop();
wasabimal 0:5b22d0b5610e 31 i2c.start();
wasabimal 0:5b22d0b5610e 32 i2c.write(addr_accel_gyro);
wasabimal 0:5b22d0b5610e 33 i2c.write(0x3c);
wasabimal 0:5b22d0b5610e 34 i2c.start();
wasabimal 0:5b22d0b5610e 35 i2c.write(addr_accel_gyro|0x01);
wasabimal 0:5b22d0b5610e 36 xl = i2c.read(0);
wasabimal 0:5b22d0b5610e 37 i2c.stop();
wasabimal 0:5b22d0b5610e 38 i2c.start();
wasabimal 0:5b22d0b5610e 39 i2c.write(addr_accel_gyro);
wasabimal 0:5b22d0b5610e 40 i2c.write(0x3d);//read adress
wasabimal 0:5b22d0b5610e 41 i2c.start();
wasabimal 0:5b22d0b5610e 42 i2c.write(addr_accel_gyro|0x01);
wasabimal 0:5b22d0b5610e 43 yh = i2c.read(0);
wasabimal 0:5b22d0b5610e 44 i2c.stop();
wasabimal 0:5b22d0b5610e 45 i2c.start();
wasabimal 0:5b22d0b5610e 46 i2c.write(addr_accel_gyro);
wasabimal 0:5b22d0b5610e 47 i2c.write(0x3e);
wasabimal 0:5b22d0b5610e 48 i2c.start();
wasabimal 0:5b22d0b5610e 49 i2c.write(addr_accel_gyro|0x01);
wasabimal 0:5b22d0b5610e 50 yl = i2c.read(0);
wasabimal 0:5b22d0b5610e 51 i2c.stop();
wasabimal 0:5b22d0b5610e 52 i2c.start();
wasabimal 0:5b22d0b5610e 53 i2c.write(addr_accel_gyro);
wasabimal 0:5b22d0b5610e 54 i2c.write(0x3f);//read adress
wasabimal 0:5b22d0b5610e 55 i2c.start();
wasabimal 0:5b22d0b5610e 56 i2c.write(addr_accel_gyro|0x01);
wasabimal 0:5b22d0b5610e 57 zh = i2c.read(0);
wasabimal 0:5b22d0b5610e 58 i2c.stop();
wasabimal 0:5b22d0b5610e 59 i2c.start();
wasabimal 0:5b22d0b5610e 60 i2c.write(addr_accel_gyro);
wasabimal 0:5b22d0b5610e 61 i2c.write(0x40);
wasabimal 0:5b22d0b5610e 62 i2c.start();
wasabimal 0:5b22d0b5610e 63 i2c.write(addr_accel_gyro|0x01);
wasabimal 0:5b22d0b5610e 64 zl = i2c.read(0);
wasabimal 0:5b22d0b5610e 65 i2c.stop();
wasabimal 0:5b22d0b5610e 66 double acc_ax = short((xh<<8) | (xl));
wasabimal 0:5b22d0b5610e 67 double AX = (acc_ax)*2/32768*9.81;
wasabimal 0:5b22d0b5610e 68 double acc_ay = short((yh<<8) | (yl));
wasabimal 0:5b22d0b5610e 69 double AY = (acc_ay)*2/32768*9.81;
wasabimal 0:5b22d0b5610e 70 double acc_az = short((zh<<8) | (zl));
wasabimal 0:5b22d0b5610e 71 double AZ = (acc_az)*2/32768*9.81;
wasabimal 0:5b22d0b5610e 72 pc.printf("AX = %f",AX);
wasabimal 0:5b22d0b5610e 73 pc.printf(" AY = %f",AY);
wasabimal 0:5b22d0b5610e 74 pc.printf(" AZ = %f\r\n",AZ);
wasabimal 0:5b22d0b5610e 75 wait(0.1);
wasabimal 0:5b22d0b5610e 76 }
wasabimal 0:5b22d0b5610e 77 }