Checking 6-axis sensor of accelerometer(z-axis)

Dependencies:   mbed

main.cpp

Committer:
umezawa
Date:
2020-12-29
Revision:
0:441039f94b75

File content as of revision 0:441039f94b75:

#include "mbed.h"
Serial pc(USBTX, USBRX);
I2C i2c(PB_7,PB_6);
DigitalOut conv(A3);

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);
    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);
    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)*2/32764*9.81;
        pc.printf("AZ = %f\r\n",AX);
        wait(0.5);
    }
}