我目前在調通mpu9255的通訊,傳輸介面是使用SPI 但磁力計一直無法獲取數據,想請問板上高手協助看問題在哪裡, 以下是我的code,麻煩各位幫我看看哪裡出了問題,謝謝 void readMagData(float *mx,float *my, float *mz){ uint8_t rawData[7]; int16_t magCount[3]; float destination[3]; 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[0]=(int16_t)(((int16_t)rawData[1]<<8)|rawData[0]); magCount[1]=(int16_t)(((int16_t)rawData[3]<<8)|rawData[2]); magCount[2]=(int16_t)(((int16_t)rawData[5]<<8)|rawData[4]); *mx = (float)magCount[0]*mRes*magCalibration[0] - magbias[0]; // get actual magnetometer value, this depends on scale being set *my = (float)magCount[1]*mRes*magCalibration[1] - magbias[1]; *mz = (float)magCount[2]*mRes*magCalibration[2] - magbias[2]; ********************************************************************************************************** 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 int i = 0; select(); spi.write(ReadAddr | READ_FLAG); for(i=0; i<Bytes; i++) ReadBuf[i] = spi.write(0x00); deselect(); wait_us(500); } |
评分
查看全部评分