3軸加速度の表示

Dependencies:   mbed

main.cpp

Committer:
wasabimal
Date:
2022-01-23
Revision:
0:5b22d0b5610e

File content as of revision 0:5b22d0b5610e:

#include "mbed.h"

Serial pc(USBTX,USBRX);
I2C i2c(p28, p27);

const int addr_accel_gyro = 0xD0;   //9軸センサ自体のアドレス
char cmd[2];
short int xl,xh,yl,yh,zl,zh;
int main()
{
    i2c.frequency(100000);
//    cmd[0]=0x6B;
//    cmd[1]=0x00;
//    i2c.write(addr_accel_gyro,amd,2);
//    cmd[0] = 0x37;
//    cmd[1] = 0x02;
//    i2c.write(adde_accel_gyro,cmd,2);
//    i2c.stop();
    pc.printf("AX Start!!\r\n");
    pc.printf("AY Start!!\r\n");
    pc.printf("AZ Start!!\r\n");

    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();
        i2c.start();
        i2c.write(addr_accel_gyro);
        i2c.write(0x3d);//read adress
        i2c.start();
        i2c.write(addr_accel_gyro|0x01);
        yh = i2c.read(0);
        i2c.stop();
        i2c.start();
        i2c.write(addr_accel_gyro);
        i2c.write(0x3e);
        i2c.start();
        i2c.write(addr_accel_gyro|0x01);
        yl = i2c.read(0);
        i2c.stop();
        i2c.start();
        i2c.write(addr_accel_gyro);
        i2c.write(0x3f);//read adress
        i2c.start();
        i2c.write(addr_accel_gyro|0x01);
        zh = i2c.read(0);
        i2c.stop();
        i2c.start();
        i2c.write(addr_accel_gyro);
        i2c.write(0x40);
        i2c.start();
        i2c.write(addr_accel_gyro|0x01);
        zl = i2c.read(0);
        i2c.stop();
        double  acc_ax = short((xh<<8) | (xl));
        double AX = (acc_ax)*2/32768*9.81;
        double  acc_ay = short((yh<<8) | (yl));
        double AY = (acc_ay)*2/32768*9.81;
        double  acc_az = short((zh<<8) | (zl));
        double AZ = (acc_az)*2/32768*9.81;
        pc.printf("AX = %f",AX);
        pc.printf("  AY = %f",AY);
        pc.printf("  AZ = %f\r\n",AZ);
        wait(0.1);
    }
}