Page 1 of 2

MPU 60X0 Sample Code

Posted: Tue May 29, 2012 5:57 am
by sapan
Hi,

Code is part of my project. Right now its for I2C1. Prior to calling any of these function you need to initialize I2C1.

One sample part to read accelerometer, temperature sensor and gyro data. I have tested this code on MPU6000 and MPU6050 on I2C bus.

Code: Select all

  set_mpu_sample_rate(9);
  set_mpu_config_regsiter(EXT_SYNC_SET0, DLPF_CFG0);
  set_mpu_gyro(XG_ST_DIS, YG_ST_DIS, ZG_ST_DIS, FS_SEL_250);
  set_mpu_accel(XA_ST_DIS, YA_ST_DIS, ZA_ST_DIS, AFS_SEL_2g, ACCEL_HPF0);
  set_mpu_power_mgmt1(DEVICE_RESET_DIS, SLEEP_DIS, CYCLE_DIS, TEMPERATURE_EN, CLKSEL_XG);
  set_mpu_user_control(USER_FIFO_DIS, I2C_MST_DIS, I2C_IF_DIS, FIFO_RESET_DIS, I2C_MST_RESET_DIS, SIG_COND_RESET_DIS);

  write_mpu_power_mgmt1();
  write_mpu_gyro();
  write_mpu_accel();
  write_mpu_sample_rate();
  while (TRUE) {
     mpu_i2c_read_data(0x3B, 14); /* Read accelerometer, temperature and gyro data */
     chThdSleepMilliseconds(50);
  }


Still this is work in progress. It sends data over UART in mpu60x0.c file. First three values are accelerometer, fourth one is temperature sensor and last three are gyro values. Havent explored FIFO and interrupt part yet.
Waiting for feedbacks.


Sapan

Re: MPU 60X0 Sample Code

Posted: Tue May 29, 2012 7:16 am
by barthess
Hi sapan,
This device uses two’s complement data format. Using shift/addition operations for acquiring data is incorrect.
I use following function.

Code: Select all

int16_t complement2signed(uint8_t msb, uint8_t lsb){
  uint16_t word = 0;
  word = (msb << 8) + lsb;
  if (msb > 0x7F){
    return -1 * ((int16_t)((~word) + 1));
  }
  return (int16_t)word;
}

Re: MPU 60X0 Sample Code

Posted: Tue May 29, 2012 7:38 am
by sapan
I have checked output and it give correct data as per orientation of the sensor. I mean as mentioned in data sheet (Apart from resolution, which is less because its engineering samples).

Re: MPU 60X0 Sample Code

Posted: Tue May 29, 2012 7:41 am
by barthess
In my cases shift produces correct results for positive values, but negative values are incorrect.

Re: MPU 60X0 Sample Code

Posted: Tue May 29, 2012 7:44 am
by sapan
Thats weird, it should work fine. I am using eclipse + arm-none-eabi-gdb toolchain to compile ChibiOS. Btw I am using part of your BMP085 sample code to interface with BMP180. Will post that once commenting and cleaning up my part.

Re: MPU 60X0 Sample Code

Posted: Tue May 29, 2012 7:49 am
by barthess
May be your device actually outputs data in different format than my ITG3200, but datasheet on MPU-6xxx says "Output in two’s complement format" too.

Re: MPU 60X0 Sample Code

Posted: Tue May 29, 2012 8:05 am
by sapan
Yup I have used this device via LPC2138 as well as Discovery board. On LPC2138 I programmed it using basic code (no RTOS) but used similar method over there and it worked fine. Discovery board I am using ChibiOS. I have used both MPU6000 and MPU6050 gives same outputs.

Re: MPU 60X0 Sample Code

Posted: Wed May 30, 2012 6:41 am
by sapan
Any comments about code??

Re: MPU 60X0 Sample Code

Posted: Wed May 30, 2012 7:58 am
by barthess
For my cases it is overengineered. I use hardcoded values in my project for sampling rate, LPF, etc., that are suitable enough.

Re: MPU 60X0 Sample Code

Posted: Thu May 31, 2012 5:24 am
by sapan
ok...can do one thing... declare some default values which will be used...so no need to initialize them.