MPU 9150 access compass by 2 ways - different results, how possible?

Hi all,

unfortunately I’m a bit lost. I tried to access the magnetometer (AK8975A) of the MPU9150 on two different ways.
1: Directly by using the registers of sections 5&6 of the register map
2: Access via “External Sensor Data” as if there is an external device connected to an MPU6050

Therefore I used and adapted code from the internet. Please see the code attached because of character restrictions.

What’s weird I get different result for both methods. Sensor is not moved between the two measurements!
Serial output of “direct access”:

G: -16    53    15 A: 360    -64    16176    	C: 22    20    78
G: -40    63    19 A: 368    -68    16232    	C: 22    14    82
G: -17    59    22 A: 472    -88    16384    	C: 19    19    81
G: -31    62    -9 A: 444    -60    16164    	C: 19    21    79
G: -33    51    0  A: 608    108    16208    	C: 18    20    78
G: -30    54    6  A: 516    -56    16288    	C: 19    21    85

Serial output of “Access via External Sensor Data”:

G: 2    45    13 	A: 488    -60    16388    	C: 31    31    32
G: -39    68    0 	A: 580    32    16388    	C: 31    31    32
G: -19    62    25 	A: 448    -12    16372    	C: 28    28    28
G: -22    65    1 	A: 320    -52    16388    	C: 31    31    31
G: -19    60    14 	A: 404    -108    16404    	C: 30    30    30
G: -28    67    43 	A: 408    -136    16388    	C: 30    30    30

As you see in the “C”-column the values are quite different. This behaviour is reproducible(!) and I always get the same results as long as the sensor is in the same position between the measurements.

Any ideas?

Thanks for any help in advance!!!

MPU9150_AccessComp_2Ways.cpp (14 KB)

If you don't fully understand the methods, use the one that gives sensible values.

I think I do understand the different methods. I tried a third code (JRowberg's) and it gives me the same values as in the "direct access" approach (direct = 1 in my code).

What I don't understand is how a digital sensor interface can return different values depending on the way you are accessing the sensor...I'm just lost:)!

The answer my be in the setup-compass methode which I followed with the datasheet but honestly don't get every detail...may be due to my inexperience ;-P :

void MPU9150_setupCompass(){

  writeByte(COMPASS_I2C_ADDRESS, 0x0A, 0x00); //PowerDownMode
  writeByte(COMPASS_I2C_ADDRESS, 0x0A, 0x0F); //Fuse ROM access mode
  writeByte(COMPASS_I2C_ADDRESS, 0x0A, 0x00); //PowerDownMode

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

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

My idea basically was that someone maybe can try my code with his MPU9150 and see if he got the same problem, namely: Get different values wether we set my "direct" variable to 1 or 0 in the code (and that reproducibly when sensor is not moved between the two execution).