从MPU9150访问磁力计数据的问题

时间:2014-07-10 13:00:29

标签: magnetometer mpu

输出: - 在读取每个寄存器(对应磁力计)时,我得到的是0xFF。另一方面,我能够完美地访问加速度计和陀螺仪的寄存器。我在禁用i2c主模式和启用i2c旁路模式的情况下初始化了系统。以下是我用于初始化系统的代码: -

single_byte_write(0x6B,0x01);
single_byte_write(0x19,0x01);
single_byte_write(0x1A,0x02);
single_byte_write(0x1B,0x11);
single_byte_write(0x1C,0x10);
single_byte_write(0x6A,0x00);
single_byte_write(0x37,0x02);

('single_byte_write(address,data)将数据字节写入寄存器,地址为从地址为0x69')

我访问了accel的寄存器。和陀螺。如下: - single_byte_read(0x75,wia_mpu);返回0x68,这是who_i_am寄存器正确的。 但当我试图访问磁力计寄存器时: - single_byte_read_compass(0×00,wia_compass);它返回0xFF,而它应该返回0x48。

为了确保single_byte_read_compass()/ single_byte_write_compass()正常工作,我使用这些函数改变了从地址(从'0x0C'到'0x69')来访问accel的寄存器。和陀螺。它工作正常。

('single_byte_read()'和'single_byte_read_compass()'之间的区别只是前者使用从属地址为'0x69'而后者使用'0x0C'作为从属地址。我也试过'0x0D' ,'0x0E'和'0x0F'作为从地址,但输出保持不变。)

我还确保了MPU9150是否处于直通模式。我已经在示波器的帮助下检查了引脚'ES_DA'(即引脚6和SDA,即引脚24)的输出,该示波器完全相同,并启用了旁路模式。禁用主模式并禁用旁路模式后,'ES_DA'的输出始终为零。

我也尝试过与上面相同的程序,不只是一个而是多个MPU9150,但输出保持不变,因此很可能代码存在一些问题。

1 个答案:

答案 0 :(得分:1)

这是我用来初始化MPU9150的代码,它适用于我(这是我在互联网上找到的经过修改的arduino版本):

void MPU9150::initialize(){
   write(MPU9150_PWR_MGMT_1, 0); //Wake up

   initializeCompass();
}

void MPU9150::initializeCompass(){
  this->i2cDevice.address = this->i2cDevice.compass; // 0x0C or 0x0D

  write(0x0A, 0x00); //PowerDownMode
  write(0x0A, 0x0F); //SelfTest
  write(0x0A, 0x00); //PowerDownMode

  this->i2cDevice.address = this->i2cDevice.mpu; //0x68 or 0x69

  write(0x24, 0x40); //Wait for Data at Slave0
  write(0x25, 0x8C); //Set i2c address at slave0 at 0x0C
  write(0x26, 0x02); //Set where reading at slave 0 starts
  write(0x27, 0x88); //set offset at start reading and enable
  write(0x28, 0x0C); //set i2c address at slv1 at 0x0C
  write(0x29, 0x0A); //Set where reading at slave 1 starts
  write(0x2A, 0x81); //Enable at set length to 1
  write(0x64, 0x01); //overvride register
  write(0x67, 0x03); //set delay rate
  write(0x01, 0x80);

  write(0x34, 0x04); //set i2c slv4 delay
  write(0x64, 0x00); //override register
  write(0x6A, 0x00); //clear usr setting
  write(0x64, 0x01); //override register
  write(0x6A, 0x20); //enable master i2c mode
  write(0x34, 0x13); //disable slv4
}

然后读磁力计:

void MPU9150::readCompass()
{
    data.compass.x = read(MPU9150_CMPS_XOUT_L,MPU9150_CMPS_XOUT_H);
    data.compass.y = read(MPU9150_CMPS_YOUT_L,MPU9150_CMPS_YOUT_H);
    data.compass.z = read(MPU9150_CMPS_ZOUT_L,MPU9150_CMPS_ZOUT_H);
}

其中:

//MPU9150 Compass
#define MPU9150_CMPS_XOUT_L        0x4A   // R
#define MPU9150_CMPS_XOUT_H        0x4B   // R
#define MPU9150_CMPS_YOUT_L        0x4C   // R
#define MPU9150_CMPS_YOUT_H        0x4D   // R
#define MPU9150_CMPS_ZOUT_L        0x4E   // R
#define MPU9150_CMPS_ZOUT_H        0x4F   // R