8 years, 11 months ago.

Problem of MPU6150 Connection(without MPU6150.h)

Hello

I'm learning about I2C of mbed,

But it didn't work well.

I cannot get any data from mpu6150.

The code is here :

MPU6050 Error

#include "mbed.h"
I2C i2c(p9, p10);
Serial pc(USBTX, USBRX);
int main()
{
    int addr = 0x68;
    i2c.frequency(4000000);
    char cmd;
    int tmp[2];
    double temp;
    pc.printf("Test Connection\n");
    pc.printf("Initialization of MPU6050\n");
    cmd = (0x1a << 1);
    i2c.write(addr);
    i2c.write(cmd);
    i2c.start();
    i2c.write(0);
    i2c.stop();
    while(1)
    {
        i2c.start();
        i2c.write(0x41);
        tmp[0] = i2c.read(addr);
        i2c.stop();
        i2c.start();
        i2c.write(0x42);
        tmp[1] = i2c.read(addr);
        i2c.stop();
        temp = (tmp[0] << 8) + tmp[1];
        temp = temp/340.0 + 36.53;
        pc.printf("%f\n", temp);
    }
}

Please help me!

2 Answers

8 years, 11 months ago.

There are several issues with your code above:

Maximum I2C bitrate is 400kbit/s on most platforms. Use 100kbit/s to start with. You try to set it at 4Mbit/s (see : i2c.frequency(4000000)) Make sure the I2C pull-up resistors are installed, the SDA, SCL and GND pins are connected properly between master and slave.

The mbed libs use the 8bit I2C slaveaddressmode. Your code uses the 7bit address 0x68 (assuming that the selectionbit for AD0 is set at low level. The correct mbed address is 0xD0. (I cant remember how many times I have given this same answer..)

Use the i2c blockread and blockwrite methods when possible, the byte write and read operations are problematic on some mbed platforms. Your code uses several wrong combinations of byte write and reads, missing I2C start etc..

Try something like this:

#include "mbed.h"
I2C i2c(p9, p10);
Serial pc(USBTX, USBRX);
int main()
{
    int addr = 0xD0;
    char cmd[2];
    char tmp[2];
    double temp;

    i2c.frequency(100000); //100kbit/s

    pc.printf("Test Connection\n");
    pc.printf("Initialization of MPU6150\n");

    // fill the command array with whatever bytes needs to be written;
    cmd[0] = Register you want to write to 
    cmd[1] = Data for that register
    i2c.write(addr, cmd, 2);  // 2 is number of bytes you want to write

    while(1)
    {
        // assuming you want to read from Register 0x41, first select this register in a write command
        cmd[0] = 0x41;
        i2c.write(addr, cmd, 1);  

        // now read 2 bytes starting from Register 0x41
        i2c.read(addr, tmp, 2); 

        // now compute temp from the 2 bytes
        temp = (tmp[0] << 8) + tmp[1];
        temp = temp/340.0 + 36.53;
        pc.printf("%f\n", temp);
    }
}

Accepted Answer

Thank you for your kind!!!!

I try again using this code.

Thank you very much!

posted by Seong Wuk Jeong 15 Jun 2015
8 years, 11 months ago.

There are several libraries available. I use the http://developer.mbed.org/users/Sissors/code/MPU6050/ provided by Eric Olieman (thank you Eric !) without any problem, on a LPC1768. I hope this can help you.