//Variables where our values can be stored
int cmps[3];
int accl[3];
int gyro[3];
int temp;
void setup()
{
// Initialize the Serial Bus for printing data.
Serial.begin(9600);
// Initialize the 'Wire' class for the I2C-bus.
Wire.begin();
// Clear the 'sleep' bit to start the sensor.
MPU9150_writeSensor(MPU9150_PWR_MGMT_1, 0);
MPU9150_setupCompass();
}
void loop()
{
// Print all sensor values which the sensor provides
// Formated all values as x, y, and z in order for
// Compass, Gyro, Acceleration. The First value is
// the temperature.
double dT = ( (double) MPU9150_readSensor(MPU9150_TEMP_OUT_L,MPU9150_TEMP_OUT_H) + 12412.0) / 340.0;
Serial.print(dT);
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_CMPS_XOUT_L,MPU9150_CMPS_XOUT_H));
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_CMPS_YOUT_L,MPU9150_CMPS_YOUT_H));
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_CMPS_ZOUT_L,MPU9150_CMPS_ZOUT_H));
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_GYRO_XOUT_L,MPU9150_GYRO_XOUT_H));
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_GYRO_YOUT_L,MPU9150_GYRO_YOUT_H));
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_GYRO_ZOUT_L,MPU9150_GYRO_ZOUT_H));
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_ACCEL_XOUT_L,MPU9150_ACCEL_XOUT_H));
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_ACCEL_YOUT_L,MPU9150_ACCEL_YOUT_H));
Serial.print(" ");
Serial.print(MPU9150_readSensor(MPU9150_ACCEL_ZOUT_L,MPU9150_ACCEL_ZOUT_H));
Serial.println();
delay(100);
}
//http://pansenti.wordpress.com/2013/03/26/pansentis-invensense-mpu-9150-software-for-arduino-is-now-on-github/
//Thank you to pansenti for setup code.
//I will documented this one later.
void MPU9150_setupCompass(){
MPU9150_I2C_ADDRESS = 0x0C; //change Adress to Compass
MPU9150_writeSensor(0x0A, 0x00); //PowerDownMode
MPU9150_writeSensor(0x0A, 0x0F); //SelfTest
MPU9150_writeSensor(0x0A, 0x00); //PowerDownMode
MPU9150_I2C_ADDRESS = 0x68; //change Adress to MPU
MPU9150_writeSensor(0x24, 0x40); //Wait for Data at Slave0
MPU9150_writeSensor(0x25, 0x8C); //Set i2c address at slave0 at 0x0C
MPU9150_writeSensor(0x26, 0x02); //Set where reading at slave 0 starts
MPU9150_writeSensor(0x27, 0x88); //set offset at start reading and enable
MPU9150_writeSensor(0x28, 0x0C); //set i2c address at slv1 at 0x0C
MPU9150_writeSensor(0x29, 0x0A); //Set where reading at slave 1 starts
MPU9150_writeSensor(0x2A, 0x81); //Enable at set length to 1
MPU9150_writeSensor(0x64, 0x01); //overvride register
MPU9150_writeSensor(0x67, 0x03); //set delay rate
MPU9150_writeSensor(0x01, 0x80);
MPU9150_writeSensor(0x34, 0x04); //set i2c slv4 delay
MPU9150_writeSensor(0x64, 0x00); //override register
MPU9150_writeSensor(0x6A, 0x00); //clear usr setting
MPU9150_writeSensor(0x64, 0x01); //override register
MPU9150_writeSensor(0x6A, 0x20); //enable master i2c mode
MPU9150_writeSensor(0x34, 0x13); //disable slv4
}
////////////////////////////////////////////////////////////
///////// I2C functions to get easier all values ///////////
////////////////////////////////////////////////////////////
int MPU9150_readSensor(int addrL, int addrH){
Wire.beginTransmission(MPU9150_I2C_ADDRESS);
Wire.write(addrL);
Wire.endTransmission(false);
Wire.requestFrom(MPU9150_I2C_ADDRESS, 1, true);
byte L = Wire.read();
Wire.beginTransmission(MPU9150_I2C_ADDRESS);
Wire.write(addrH);
Wire.endTransmission(false);
Wire.requestFrom(MPU9150_I2C_ADDRESS, 1, true);
byte H = Wire.read();
return (int16_t)((H<<8)+L);
}
int MPU9150_readSensor(int addr){
Wire.beginTransmission(MPU9150_I2C_ADDRESS);
Wire.write(addr);
Wire.endTransmission(false);
Wire.requestFrom(MPU9150_I2C_ADDRESS, 1, true);
return Wire.read();
}
int MPU9150_writeSensor(int addr,int data){
Wire.beginTransmission(MPU9150_I2C_ADDRESS);
Wire.write(addr);
Wire.write(data);
Wire.endTransmission(true);
return 1;
}
So i looked into the code. And i have some trouble understanding it.
MPU9150_writeSensor(0x26, 0x02); //Set where reading at slave 0 starts
MPU9150_writeSensor(0x27, 0x88); //set offset at start reading and enable
MPU9150_writeSensor(0x28, 0x0C); //set i2c address at slv1 at 0x0C
MPU9150_writeSensor(0x29, 0x0A); //Set where reading at slave 1 starts
MPU9150_writeSensor(0x2A, 0x81); //Enable at set length to 1
MPU9150_writeSensor(0x64, 0x01); //overvride register
MPU9150_writeSensor(0x67, 0x03); //set delay rate
MPU9150_writeSensor(0x01, 0x80);
MPU9150_writeSensor(0x34, 0x04); //set i2c slv4 delay
MPU9150_writeSensor(0x64, 0x00); //override register
MPU9150_writeSensor(0x6A, 0x00); //clear usr setting
MPU9150_writeSensor(0x64, 0x01); //override register
MPU9150_writeSensor(0x6A, 0x20); //enable master i2c mode
MPU9150_writeSensor(0x34, 0x13); //disable slv4
I am confused about this part. "set where reading at slave 1 starts", why is the register here set to 0x0A, which is even not defined yet. Why is the slave 0 is set to length 4, and slave 1 is set to length 1? What does this part mean?
I have been thinking about it for several days but still don't get it.
Does anybody have some clue? I have also attached the Register Map for MPU9150.
Thanks a lot for your help.