MPU9255讀取AK8963地磁的問題
我目前在調通mpu9255的通訊,傳輸介面是使用SPI但磁力計一直無法獲取數據,想請問板上高手協助看問題在哪裡,
以下是我的code,麻煩各位幫我看看哪裡出了問題,謝謝
void readMagData(float *mx,float *my, float *mz){
uint8_t rawData;
int16_t magCount;
float destination;
float data;
int i;
writeByte(I2C_SLV0_ADDR,AK8963_ADDRESS|READ_FLAG); //Set the I2C slave addres of AK8963 and set for read.
writeByte(I2C_SLV0_REG, AK8963_XOUT_L); //I2C slave 0 register address from where to begin data transfer
writeByte(I2C_SLV0_CTRL, 0x87); //Read 6 bytes from the magnetometer
wait(0.01);
readBytes(EXT_SENS_DATA_00,rawData,7);
//must start your read from AK8963A register 0x03 and read seven bytes so that upon read of ST2 register 0x09 the AK8963A will unlatch the data registers for the next measurement.
magCount=(int16_t)(((int16_t)rawData<<8)|rawData);
magCount=(int16_t)(((int16_t)rawData<<8)|rawData);
magCount=(int16_t)(((int16_t)rawData<<8)|rawData);
*mx = (float)magCount*mRes*magCalibration - magbias;// get actual magnetometer value, this depends on scale being set
*my = (float)magCount*mRes*magCalibration - magbias;
*mz = (float)magCount*mRes*magCalibration - magbias;
**********************************************************************************************************
unsigned int writeByte(uint8_t WriteAddr,uint8_t WriteData){
unsigned int temp_val;
select();
spi.write(WriteAddr);
temp_val=spi.write(WriteData);
deselect();
wait_us(500);
return temp_val;
}
***************************************************************************************************************
void readBytes(uint8_t ReadAddr, uint8_t *ReadBuf, unsigned int Bytes){
unsigned inti = 0;
select();
spi.write(ReadAddr | READ_FLAG);
for(i=0; i<Bytes; i++)
ReadBuf = spi.write(0x00);
deselect();
wait_us(500);
}
怀疑芯片是不是出问题了,或者接线不对,也可以参考下面的资料【MPU9150读磁力计数据问题】
页:
[1]