user3825537
user3825537

Reputation: 1

issues accessing magnetometer data from MPU9150

Output: -What I am getting is 0xFF on reading every register (corresponding to magnetometer). On the other hand I am able to access the registers of the accelerometer and the gyroscope perfectly. I have initialized the system with i2c master mode disabled and i2c bypass mode enabled. Following is the code I have used to initialize the system:-

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) writes the data byte to the register with ‘address’ to the slave address as 0x69’)

I accessed the registers of accel. and gyro. as following:- single_byte_read(0x75,wia_mpu); which returns 0x68 which is correct as the who_i_am register. But when I tried to access the magnetometer registers as:- single_byte_read_compass(0x00,wia_compass); it returns 0xFF while it should return 0x48.

In order to ensure that single_byte_read_compass()/single_byte_write_compass() works correctly I have used these functions with a change in slave address(from ‘0x0C’ to ‘0x69’)to access the registers of accel. and gyro. and it worked correctly.

(The difference between ‘single_byte_read()’ and ‘single_byte_read_compass()’ is just that the former uses the slave address as ‘0x69’ while the later uses ‘0x0C’ as the slave address. I have also tried ‘0x0D’, ‘0x0E’ and ‘0x0F’ as the slave address but the output remained the same.)

I have also ensured whether the MPU9150 is in pass-through mode. I have checked the output at the pins ‘ES_DA’ i.e. pin6 and the SDA i.e. pin24 with the help of oscilloscope which comes out to be exactly same, with by-pass mode enabled. With master-mode disabled and by-pass mode disabled the output at ‘ES_DA’ is zero always.

I have also tried the same procedure as above with not just one but many MPU9150s but the output remained the same, thus most likely there is some problem with the code.

Upvotes: 0

Views: 4305

Answers (1)

Tomas Stibrany
Tomas Stibrany

Reputation: 380

This is the code I use to initialize MPU9150 and it works for me (It is a modified arduino version I found on the internet):

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
}

And then to read magnetometer:

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);
}

Where:

//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

Upvotes: 1

Related Questions