Help me, Please!
I can't read data from magnetometer in MPU9150! Data always equal 0x00!
Register 0x02 bit 0 (DRDY) never set to 1!
I2C interface works normally.
Data of Gyro and Accelerometer read normally, correspond to motions.
Code ID of the compass (0x48) in bypass mode read correctly.
I2C initialization is taken from CA-SDK:
// ******************************************
int MPU9150_Comp_Init (void)
{
xdata unsigned BUFF[3];
register byte CH, i;
if (MPU9150_Set_Bypass (1)!=OK) return -1; // Bypass Mode On
CH = I2C_Read (CFG.SENSOR_ADDR, REG_USER_CTRL); // Check bit 5 of Register 0x6A
if (CH&0x10) return -2; // Must be 0
CH = I2C_Read (CFG.SENSOR_ADDR, REG_INT_PIN_CFG); // Check bit 1 of Register 0x37
if ((CH&0x02)!=0x02) return -3; // Nust be 1
CH = I2C_Read (CFG.COMPASS_ADDR, AKM_REG_WHOAMI);
if (CH != 0x48) return -4;
Delay(10);
I2C_Write (CFG.COMPASS_ADDR, AKM_REG_CNTL, AKM_POWER_DOWN); // Reset Compass
Delay(10);
I2C_Write (CFG.COMPASS_ADDR, AKM_REG_CNTL, AKM_FUSE_ROM_ACCESS);
Delay(10);
/* Get sensitivity adjustment BUFF from fuse ROM. */
for (i=0;i<3;i++)
BUFF = I2C_Read (CFG.COMPASS_ADDR, AKM_REG_ASAX+i);
CFG.MAG_SENS_ADJ[0] = (unsigned)BUFF[0] + 0x10;
CFG.MAG_SENS_ADJ[1] = (unsigned)BUFF[1] + 0x10;
CFG.MAG_SENS_ADJ[2] = (unsigned)BUFF[2] + 0x10;
I2C_Write (CFG.COMPASS_ADDR, AKM_REG_CNTL, AKM_POWER_DOWN);// Reset Compass
Delay(10);
if (MPU9150_Set_Bypass (0)!=OK) return -5; // Bypass Mode Off
return OK;
}
// ******************************************
void MPU9150_Master_I2C_Init (void)
{
register byte CH;
xdata byte TBUFF[9];
xdata unsigned DAT[3];
// Slave 0 reads from AKM BUFF registers
I2C_Write (CFG.SENSOR_ADDR, REG_S0_ADDR, BIT_I2C_Read | CFG.COMPASS_ADDR);
// Compass reads begin from this register
I2C_Write (CFG.SENSOR_ADDR, REG_S0_REG, AKM_REG_ST1);
// Enable slave 0 and 8 byte reads
I2C_Write (CFG.SENSOR_ADDR, REG_S0_CTRL, BIT_SLAVE_EN | 8);
// ******************************************
// Slave 1 changes AKM measurement mode
I2C_Write (CFG.SENSOR_ADDR, REG_S1_ADDR, CFG.COMPASS_ADDR);
// AKM measurement mode register
I2C_Write (CFG.SENSOR_ADDR, REG_S1_REG, AKM_REG_CNTL);
// Set slave 1 BUFF
I2C_Write (CFG.SENSOR_ADDR, REG_S1_DO, AKM_SINGLE_MEASUREMENT);
// Enable slave 1, 1-byte writes
I2C_Write (CFG.SENSOR_ADDR, REG_S1_CTRL, BIT_SLAVE_EN | 1);
// ******************************************
// Trigger slave 0 and slave 1 actions at each sample
I2C_Write (CFG.SENSOR_ADDR, REG_I2C_DELAY_CTRL, 0x03);
// For the MPU9150, the auxiliary I2C bus needs to be set to VDD
I2C_Write (CFG.SENSOR_ADDR, REG_YG_OFFS_TC, BIT_I2C_MST_VDDIO);
MPU9150_Set_Compass_Sample_Rate (10);
}
// ******************************************
int MPU9150_Get_Compass (void)
{
register byte i;
if (!(CFG.SENSORS & INV_XYZ_COMPASS)) return ERROR;
for (i=0;i<8;i++)
{
TBUFF = I2C_Read (CFG.SENSOR_ADDR, REG_RAW_COMPASS + i); Delay (1);
}
if (!(TBUFF[0] & AKM_DAT_READY))
return -2;
if ((TBUFF[7] & AKM_OVERFLOW) || (TBUFF[7] & AKM_DAT_ERROR))
return -3;
DAT[0] = ((unsigned)TBUFF[2] << 8) | TBUFF[1];
DAT[1] = ((unsigned)TBUFF[4] << 8) | TBUFF[3];
DAT[2] = ((unsigned)TBUFF[6] << 8) | TBUFF[5];
MOTION.Compass_X = ((unsigned)DAT[0] * CFG.MAG_SENS_ADJ[0]) >> 8;
MOTION.Compass_Y = ((unsigned)DAT[1] * CFG.MAG_SENS_ADJ[1]) >> 8;
MOTION.Compass_Z = ((unsigned)DAT[2] * CFG.MAG_SENS_ADJ[2]) >> 8;
return OK;
}
// ******************************************