void Init_MPU6050()
{
I2CWrite_MPU6050(I2C_ADDR,PWR_MGMT_1, 0x00); //
I2CWrite_MPU6050(I2C_ADDR,SMPLRT_DIV, 0x00); //1khz
//I2CWrite_MPU6050(I2C_ADDR,SMPLRT_DIV, 0x07);
I2CWrite_MPU6050(I2C_ADDR,CONFIG, 0x02);
I2CWrite_MPU6050(I2C_ADDR,GYRO_CONFIG, 0x00); //250dps
//I2CWrite_MPU6050(I2C_ADDR,GYRO_CONFIG, 0x03);
I2CWrite_MPU6050(I2C_ADDR,ACCEL_CONFIG, 0x00); //2g
I2CWrite_MPU6050(I2C_ADDR,ACCEL_CONFIG2,0x02);//lpf
I2CWrite_MPU6050(AK8963_ADDRESS,AK8963_CNTL1,0x02);//mode1 0110
I2CWrite_MPU6050(I2C_ADDR,INT_PIN_CFG,0x02);
}
void mag()
{
// I2CWrite_MPU6050(AK8963_ADDRESS,AK8963_CNTL1,0x02);
if(I2CRead_MPU6050(AK8963_ADDRESS,AK8963_ST1)&0x01)
{
m[0] = I2CRead_MPU6050(AK8963_ADDRESS,AK8963_XOUT_L); // read Gyroscope X_Low value
m[1] = I2CRead_MPU6050(AK8963_ADDRESS,AK8963_XOUT_H); // read Gyroscope X_High value
m[2] = I2CRead_MPU6050(AK8963_ADDRESS,AK8963_YOUT_L); // read Gyroscope Y_Low value
m[3] = I2CRead_MPU6050(AK8963_ADDRESS,AK8963_YOUT_H); // read Gyroscope Y_High value
m[4] = I2CRead_MPU6050(AK8963_ADDRESS,AK8963_ZOUT_L); // read Gyroscope Z_Low value
m[5] = I2CRead_MPU6050(AK8963_ADDRESS,AK8963_ZOUT_H); // read Gyroscope Z_High value
m[6]=I2CRead_MPU6050(AK8963_ADDRESS,AK8963_ST2);
if(!(m[6]&0x08))
{mX=((m[1]<<8)+m[0])*50/256*4912/8190.00;
mY=((m[3]<<8)+m[2])*52/256*4912/8190.00;
mZ=((m[5]<<8)+m[4])*40/256*4912/8190.00;}
printf("mX=%d, mY=%d, mZ=%d\n", (int)mX, (int)mY,(int) mZ);
}
}
為何偶讀mag data獨到一半會突然不行?我用debug除錯發現執行到if(I2CRead_MPU6050(AK8963_ADDRESS,AK8963_ST1)&0x01)這時,會判斷False而不執行if裡的i2c read |