MPU 60X0 Sample Code

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general. This forum is NOT for support.
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

MPU 60X0 Sample Code

Post 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
Attachments
mpu 60X0.rar
(6.43 KiB) Downloaded 723 times
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: MPU 60X0 Sample Code

Post 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;
}
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: MPU 60X0 Sample Code

Post 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).
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: MPU 60X0 Sample Code

Post by barthess »

In my cases shift produces correct results for positive values, but negative values are incorrect.
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: MPU 60X0 Sample Code

Post 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.
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: MPU 60X0 Sample Code

Post 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.
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: MPU 60X0 Sample Code

Post 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.
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: MPU 60X0 Sample Code

Post by sapan »

Any comments about code??
User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Been thanked: 7 times

Re: MPU 60X0 Sample Code

Post by barthess »

For my cases it is overengineered. I use hardcoded values in my project for sampling rate, LPF, etc., that are suitable enough.
sapan
Posts: 56
Joined: Sat Dec 24, 2011 1:22 pm

Re: MPU 60X0 Sample Code

Post by sapan »

ok...can do one thing... declare some default values which will be used...so no need to initialize them.
Post Reply