This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Aux I2C with MPU-3050

I am trying to hook up the following accelerometer with my MPU-3050, but I am having trouble reading from the registers. So far I have code that looks like this.

Accelerometer www.seeedstudio.com/.../Grove-3Axis-Digital-Accelerometer400g-p-1897.html

Includes and some globals are assumed.... The code compiles, but I get gibberish for the output of X_L, Y_L & Z_L. I've tried the high values also.

<>

void InitializeAccelerometer() {
  char value[5];
  value[0] = 0x7F;
  I2C_Write(paddr, H3LIS331DL_CTRL_REG1, value[0], 1);

  #ifdef OUTPUT_SERIAL
  I2C_Read(paddr, H3LIS331DL_CTRL_REG1, data, 1);
  sc.printf("Written Result: %i\nTemperature: %i\n\n\n", data[0], data[1]);
  #endif

  data[0] = data[1] = 0;

  #ifdef OUTPUT_SERIAL 
  I2C_Read(paddr, H3LIS331DL_CTRL_REG1, data, 1);
  sc.printf("Peripheral Accelerometer Who Am I Test\nI Am: %x\n", data[0]);
  #endif 
}

void SetupAccelerometer ( ) { 
  char value[6];
  value[0] = 0x7F;
  I2C_Write(paddr, H3LIS331DL_CTRL_REG1, value[0], 1);
  #ifdef OUTPUT_SERIAL
  I2C_Read(paddr, H3LIS331DL_CTRL_REG1, value+1, 1);
  sc.printf("Accel CTRL Register: %x\n\n", value[1]);
  #endif

    while (1) {
      I2C_Read(paddr, H3LIS331DL_OUT_X_L, value, 1);
      I2C_Read(paddr, H3LIS331DL_OUT_Y_L, value+1, 1);
      I2C_Read(paddr, H3LIS331DL_OUT_Z_L, value+2, 1);
      #ifdef OUTPUT_SERIAL
      sc.printf("X_L: %i\nY_L: %i\nZ_L: %i\n\n", value[0], value[1], value[2]);
      #endif
      wait(3);
    } 
  }

void I2C_Write ( int addr, char cmd, char value, int length ) {

  i2c.start();
  i2c.write(addr);
  i2c.write(cmd);
  i2c.write(value);
  i2c.stop();

}

void I2C_Read ( int addr, char cmd, char *data, int length ) {
  int result;
  result = i2c.write(addr, &cmd, length, true); 
  
  #ifdef OUTPUT_READ
  sc.printf("I2C Write Result: %i\n", result);
  #endif

  result = i2c.read(addr, data, length);
  
  #ifdef OUTPUT_READ
  sc.printf("I2C Read Result: %i\n", result);
  sc.printf("Test: %i\n\r", data[0]);
  #endif
}

<>

Please help me identify my problem.

Thanks, Frank

Related