Jump to content
I2Cdevlib Forums
ibrahims

MSP430 + MPU 9150

Recommended Posts

Hi im trying to access the magnetometers data. I am using Energia. THe following code is being used. The x,y,z values are all nearly the same. If anyone can please post a fix with code this would be helpful.
 

#include <Wire.h>
#include <math.h>
// Math Constants
#define PI 3.142

// Address of MPU9150 memory registers
.
#define MPU9150_SELF_TEST_X        0x0D   // R/W
#define MPU9150_SELF_TEST_Y        0x0E   // R/W
#define MPU9150_SELF_TEST_X        0x0F   // R/W
#define MPU9150_SELF_TEST_A        0x10   // R/W
#define MPU9150_SMPLRT_DIV         0x19   // R/W
#define MPU9150_CONFIG             0x1A   // R/W
#define MPU9150_GYRO_CONFIG        0x1B   // R/W
#define MPU9150_ACCEL_CONFIG       0x1C   // R/W
#define MPU9150_FF_THR             0x1D   // R/W
#define MPU9150_FF_DUR             0x1E   // R/W
#define MPU9150_MOT_THR            0x1F   // R/W
#define MPU9150_MOT_DUR            0x20   // R/W
#define MPU9150_ZRMOT_THR          0x21   // R/W
#define MPU9150_ZRMOT_DUR          0x22   // R/W
#define MPU9150_FIFO_EN            0x23   // R/W
#define MPU9150_I2C_MST_CTRL       0x24   // R/W
#define MPU9150_I2C_SLV0_ADDR      0x25   // R/W
#define MPU9150_I2C_SLV0_REG       0x26   // R/W
#define MPU9150_I2C_SLV0_CTRL      0x27   // R/W
#define MPU9150_I2C_SLV1_ADDR      0x28   // R/W
#define MPU9150_I2C_SLV1_REG       0x29   // R/W
#define MPU9150_I2C_SLV1_CTRL      0x2A   // R/W
#define MPU9150_I2C_SLV2_ADDR      0x2B   // R/W
#define MPU9150_I2C_SLV2_REG       0x2C   // R/W
#define MPU9150_I2C_SLV2_CTRL      0x2D   // R/W
#define MPU9150_I2C_SLV3_ADDR      0x2E   // R/W
#define MPU9150_I2C_SLV3_REG       0x2F   // R/W
#define MPU9150_I2C_SLV3_CTRL      0x30   // R/W
#define MPU9150_I2C_SLV4_ADDR      0x31   // R/W
#define MPU9150_I2C_SLV4_REG       0x32   // R/W
#define MPU9150_I2C_SLV4_DO        0x33   // R/W
#define MPU9150_I2C_SLV4_CTRL      0x34   // R/W
#define MPU9150_I2C_SLV4_DI        0x35   // R  
#define MPU9150_I2C_MST_STATUS     0x36   // R
#define MPU9150_INT_PIN_CFG        0x37   // R/W
#define MPU9150_INT_ENABLE         0x38   // R/W
#define MPU9150_INT_STATUS         0x3A   // R  
#define MPU9150_ACCEL_XOUT_H       0x3B   // R  
#define MPU9150_ACCEL_XOUT_L       0x3C   // R  
#define MPU9150_ACCEL_YOUT_H       0x3D   // R  
#define MPU9150_ACCEL_YOUT_L       0x3E   // R  
#define MPU9150_ACCEL_ZOUT_H       0x3F   // R  
#define MPU9150_ACCEL_ZOUT_L       0x40   // R  
#define MPU9150_TEMP_OUT_H         0x41   // R  
#define MPU9150_TEMP_OUT_L         0x42   // R  
#define MPU9150_GYRO_XOUT_H        0x43   // R  
#define MPU9150_GYRO_XOUT_L        0x44   // R  
#define MPU9150_GYRO_YOUT_H        0x45   // R  
#define MPU9150_GYRO_YOUT_L        0x46   // R  
#define MPU9150_GYRO_ZOUT_H        0x47   // R  
#define MPU9150_GYRO_ZOUT_L        0x48   // R  
#define MPU9150_EXT_SENS_DATA_00   0x49   // R  
#define MPU9150_EXT_SENS_DATA_01   0x4A   // R  
#define MPU9150_EXT_SENS_DATA_02   0x4B   // R  
#define MPU9150_EXT_SENS_DATA_03   0x4C   // R  
#define MPU9150_EXT_SENS_DATA_04   0x4D   // R  
#define MPU9150_EXT_SENS_DATA_05   0x4E   // R  
#define MPU9150_EXT_SENS_DATA_06   0x4F   // R  
#define MPU9150_EXT_SENS_DATA_07   0x50   // R  
#define MPU9150_EXT_SENS_DATA_08   0x51   // R  
#define MPU9150_EXT_SENS_DATA_09   0x52   // R  
#define MPU9150_EXT_SENS_DATA_10   0x53   // R  
#define MPU9150_EXT_SENS_DATA_11   0x54   // R  
#define MPU9150_EXT_SENS_DATA_12   0x55   // R  
#define MPU9150_EXT_SENS_DATA_13   0x56   // R  
#define MPU9150_EXT_SENS_DATA_14   0x57   // R  
#define MPU9150_EXT_SENS_DATA_15   0x58   // R  
#define MPU9150_EXT_SENS_DATA_16   0x59   // R  
#define MPU9150_EXT_SENS_DATA_17   0x5A   // R  
#define MPU9150_EXT_SENS_DATA_18   0x5B   // R  
#define MPU9150_EXT_SENS_DATA_19   0x5C   // R
#define MPU9150_EXT_SENS_DATA_20   0x5D   // R  
#define MPU9150_EXT_SENS_DATA_21   0x5E   // R  
#define MPU9150_EXT_SENS_DATA_22   0x5F   // R  
#define MPU9150_EXT_SENS_DATA_23   0x60   // R  
#define MPU9150_MOT_DETECT_STATUS  0x61   // R  
#define MPU9150_I2C_SLV0_DO        0x63   // R/W
#define MPU9150_I2C_SLV1_DO        0x64   // R/W
#define MPU9150_I2C_SLV2_DO        0x65   // R/W
#define MPU9150_I2C_SLV3_DO        0x66   // R/W
#define MPU9150_I2C_MST_DELAY_CTRL 0x67   // R/W
#define MPU9150_SIGNAL_PATH_RESET  0x68   // R/W
#define MPU9150_MOT_DETECT_CTRL    0x69   // R/W
#define MPU9150_USER_CTRL          0x6A   // R/W
#define MPU9150_PWR_MGMT_1         0x6B   // R/W
#define MPU9150_PWR_MGMT_2         0x6C   // R/W
#define MPU9150_FIFO_COUNTH        0x72   // R/W
#define MPU9150_FIFO_COUNTL        0x73   // R/W
#define MPU9150_FIFO_R_W           0x74   // R/W
#define MPU9150_WHO_AM_I           0x75   // R

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


// I2C address 0x69 could be 0x68 depends on your wiring.
int MPU9150_I2C_ADDRESS = 0x68;

// Variables to define gyro and accel biases
double gyro_bias[3]={0};
double acc_bias[3]={0};

// Variables where our values can be stored
double cmps[3]={0};
double acc[3]={0};
double gyro[3]={0};

// State Variables
double euler[3]={0};
double pos[3]={0};
double vel[3]={0};

// Time Variables

unsigned long time=0,time_prev=0;




// Start of Main Program
void setup()
{
    // Initialize the Serial Bus for printing data.
    Serial.begin(115200);
        
    // Initialize the 'Wire' class for the I2C-bus.
    Wire.begin();

    // Clear the 'sleep' bit to start the sensor.
    MPU9150_write(MPU9150_PWR_MGMT_1, 0);

    MPU9150_setupCompass();
        
        delay(2000);
}


void loop()
{

    // Temperature

    double dT = ((double)MPU9150_read(MPU9150_TEMP_OUT_L, MPU9150_TEMP_OUT_H) + 12412.0) / 340.0;
        
        // Magnetometer
        int mx=MPU9150_read(MPU9150_CMPS_XOUT_L, MPU9150_CMPS_XOUT_H);
        int my=MPU9150_read(MPU9150_CMPS_YOUT_L, MPU9150_CMPS_YOUT_H);
        int mz=MPU9150_read(MPU9150_CMPS_ZOUT_L, MPU9150_CMPS_ZOUT_H);
        
        // Gyro Rates
        int p= MPU9150_read(MPU9150_GYRO_XOUT_L, MPU9150_GYRO_XOUT_H);
        int q= MPU9150_read(MPU9150_GYRO_YOUT_L, MPU9150_GYRO_YOUT_H);
        int r= MPU9150_read(MPU9150_GYRO_ZOUT_L, MPU9150_GYRO_ZOUT_H);
        
        // Accelerometer
        int ax=MPU9150_read(MPU9150_ACCEL_XOUT_L, MPU9150_ACCEL_XOUT_H);
        int ay=MPU9150_read(MPU9150_ACCEL_YOUT_L, MPU9150_ACCEL_YOUT_H);
        int az=MPU9150_read(MPU9150_ACCEL_ZOUT_L, MPU9150_ACCEL_ZOUT_H);
        
        // Get floating point values
        acc[0]=(double)ax/16384*9.81-acc_bias[0];
        acc[1]=(double)ay/16384*9.81-acc_bias[1];
        acc[2]=(double)az/16384*9.81-acc_bias[2];
        double a[3]={0};
        
        gyro[0]=(double)p/131*PI/180-gyro_bias[0];
        gyro[1]=(double)q/131*PI/180-gyro_bias[1];
        gyro[2]=(double)r/131*PI/180-gyro_bias[2];
        
        cmps[0]=(double)mx*0.3;
        cmps[1]=(double)my*0.3;
        cmps[2]=(double)mz*0.3;
       
        Serial.print("T=");Serial.println(dT);
        Serial.print("ax=");Serial.println(acc[0],4);
        Serial.print("ay=");Serial.println(acc[1],4);
        Serial.print("az=");Serial.println(acc[2],4);
        Serial.print("p=");Serial.println(gyro[0],4);
        Serial.print("q=");Serial.println(gyro[1],4);
        Serial.print("r=");Serial.println(gyro[2],4);
        Serial.print("mx=");Serial.println(cmps[0],4);
        Serial.print("my=");Serial.println(cmps[1],4);
        Serial.print("mz=");Serial.println(cmps[2],4);
       
        Serial.println("%%%%%%%%%% End of Time Step%%%%%%%%%%");
        Serial.println("");
        
    delay(10);
}
 
// Set up Compass only once
void MPU9150_setupCompass(){
    
        int tempAddress = MPU9150_I2C_ADDRESS; //temporarily store mpu9150 i2c address as it will later be modified

    MPU9150_I2C_ADDRESS = 0x0C;      //change Address to Compass    

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

    //MPU9150_I2C_ADDRESS = 0x68;      //change Address to MPU
    MPU9150_I2C_ADDRESS = tempAddress; //new version to revert to original address

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

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

////////////////////////////////////////////////////////////
///////// I2C functions to get easier all values ///////////
////////////////////////////////////////////////////////////

int MPU9150_read(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 (H << 8) + L;
}


int MPU9150_write(int addr, int data){
    Wire.beginTransmission(MPU9150_I2C_ADDRESS);
    Wire.write(addr);
    Wire.write(data);
    Wire.endTransmission(true);

    return 1;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×