LpcXpresso802

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LpcXpresso802

1,044 Views
mihai_robu
Contributor I

Hi. I'm new to the embedded world. I have a LPCXpresso802 Board and an accelerometer MPU6050.

I want to read data from Mpu6050 using I2C. I use LPCOpen Lib but I don't understand that well. I was able to connect with him but I guess I don't extract the data from where I need. I modified this code to read data from 0x68 because I know that is MPU6050 access register. 

#define MPU_I2CAddress             0x68

void MPU_read(float *data, uint8_t *orgdatabuffer)
{
    uint8_t  LM75_Buf[8];
    WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE); // Wait for the master state to be idle
    LPC_I2C0->MSTDAT = (MPU_I2CAddress<<1) | 0;       // Address with 0 for RWn bit (WRITE)
    LPC_I2C0->MSTCTL = CTL_MSTSTART;                   // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
    WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX);   // Wait for the address to be ACK'd
    LPC_I2C0->MSTDAT = 0x00;
    LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;
    WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX);   // Wait for the address to be ACK'd
   
    LPC_I2C0->MSTDAT = MPU_I2CAddress<<1) | 1;       // Address with 1 for RWn bit (READ)
    LPC_I2C0->MSTCTL = CTL_MSTSTART;                   // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
    while ((LPC_I2C0->STAT & MASTER_STATE_MASK) != STAT_MSTRX)
    {
       
    }
    MPU_Buf[0] = LPC_I2C0->MSTDAT;
    LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;
    WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_RX);   // Wait for the address to be ACK'd
    while ((LPC_I2C0->STAT & MASTER_STATE_MASK) != STAT_MSTRX)
    {
       
    }

   MPU_Buf[1] = LPC_I2C0->MSTDAT;
    LPC_I2C0->MSTCTL = CTL_MSTSTOP;   
    while ((LPC_I2C0->STAT & MASTER_STATE_MASK) != STAT_MSTIDLE)
    {
       
    }
*data= (LM75_Buf[0]+(LM75_Buf[1]);
Can you help me ?
Mihai R.
Labels (2)
2 Replies

741 Views
mihai_robu
Contributor I

Ok, I make changes. I read Who Am I register(75) and he return me value 68, witch is good.

But when I try to read the temperature returns 0, why?

REG FOR TEM IS :   0X41-> TEMP_OUT_H

                                  0X42-> TEMP_OUT_L 

#define MPU6050_I2CAddress             0x68
void MPU6050_init(void)
{
}
void MPU6050_read(float *data, uint8_t *orgdatabuffer)
{
    uint8_t  MPU6050_Buf[2];  MPU6050_Buf[0]=-1;
            MPU6050_Buf[1]=-1;
    WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_IDLE); // Wait for the master state to be idle
    LPC_I2C0->MSTDAT = (MPU6050_I2CAddress<<1) | 0;       // Address with 0 for RWn bit (WRITE)
    LPC_I2C0->MSTCTL = CTL_MSTSTART;                   // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
    WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX);   // Wait for the address to be ACK'd
    LPC_I2C0->MSTDAT = 0x41;
    LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;
    WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_TX);   // Wait for the address to be ACK'd
    LPC_I2C0->MSTDAT = (MPU6050_I2CAddress<<1) | 1;       // Address with 0 for RWn bit (READ)
    LPC_I2C0->MSTCTL = CTL_MSTSTART;                   // Start the transaction by setting the MSTSTART bit to 1 in the Master control register.
    while ((LPC_I2C0->STAT & MASTER_STATE_MASK) != STAT_MSTRX)
    {
       
    }
    MPU6050_Buf[0] = LPC_I2C0->MSTDAT;
    LPC_I2C0->MSTCTL = CTL_MSTCONTINUE;
    WaitI2CMasterState(LPC_I2C0, I2C_STAT_MSTST_RX);   // Wait for the address to be ACK'd
    while ((LPC_I2C0->STAT & MASTER_STATE_MASK) != STAT_MSTRX)
    {
       
    }
    MPU6050_Buf[1] = LPC_I2C0->MSTDAT;
    LPC_I2C0->MSTCTL = CTL_MSTSTOP;
    while ((LPC_I2C0->STAT & MASTER_STATE_MASK) != STAT_MSTIDLE)
    {
    }
     printf("%d ",MPU6050_Buf[0]);
     printf("%d ",MPU6050_Buf[1]);
}
Can you help me ?
Mihai R.


0 Kudos

741 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Mihai Robu,

     If you can read Who Am I register, it means the hardware connection don't have the problem.

     I think the temperature read have problem still in the code side, you need to check the I2C bus wave, check whether the control I2C wave meet your MPU6050 read register demand? Maybe the control step have the problem. You can associated with the logical analyzer to check the I2C wave, this will be more easy to find the problem, after you get the I2C wave, check it with your MPU6050 datasheet read temperature wave.

    About the MPU6050, I find there has a post on the website may help you:

Arduino Playground - MPU-6050 


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------