AnsweredAssumed Answered

Having trouble in communication between MMA8451 and MKW2x series platform

Question asked by 吕晨 赵 on Apr 17, 2015
Latest reply on Apr 30, 2015 by AngelC

Hi everyone .I have trouble in communication between MMA8451 and MKW2x series platform.I have got something from MMA8451 through I2C bus.

Originally, I have tried to modify the driver code generated by the Beekit. It is based on the interrrupt system and seems didn't work.So I gave up the driver and used registers of  i2c module directly.According to the document I use these registers addresses and values and got the correct device id(default value 0x1A) .But there is still some problems.

1. All accelerometer data seems not changed whether I shaked accelerometers or not.It is X_MSB 0x43,Y_MSB 0x20,Z_MSB 0x56,and Z_LSB 0x48.

2 The code like this  is working fine step by step.But it got stuck when I run it without stop.

I guess I need delay for a while after writing or reading the I2C1_D(data register). It still got stuck sometime,but better than no delay.

So,these two problems, any suggestion?

Thanks!

All the registers and its value's meaning is described in the MKW2xD Reference Manual and MMA8451Q data sheet.Before the code,I have already done the init work.If someone needs the init code, I'll post it later.

while(1){
    /***************start signal*****************/
    I2C1_C1 |= 0x20u;//set master mode
    I2C1_C1 |= 0x10u;//set transmit mode
    I2C1_C1 |= 0x8u; //set txak mode
   
    /********write byte to the IIC bus**********/
    I2C1_D = 0x38;
    /********wait flag*******************/
    while((I2C1_S & 0x2u)==0);
    I2C1_S |= 0x2u;
    /****************get ack signal**********/
    while((I2C1_S & 0x1u));
   
    /*****************get byte from XYZ registers****************************************/
    I2C1_D = buf[Index];
    Index = (Index+1)%9;
    while((I2C1_S & 0x2u)==0);
    I2C1_S |= 0x2u;
    while((I2C1_S & 0x1u));
    /****************************************************************/
   
    /****************repeated start signal***************************/
    I2C1_C1 |= 0x04u;
    I2C1_D = 0x39;

    while((I2C1_S & 0x2u)==0);
    I2C1_S |= 0x2u;
    while((I2C1_S & 0x1u));
    /********set rx mode********/
    I2C1_C1 &= ~0x10u;
    /******give nack***********/
    I2C1_C1 |= 0x8u;
    /******read data from IIC bus*****/
    results = I2C1_D;
    /***********WAIT****************/
    while((I2C1_S & 0x2)==0);
    I2C1_S |= 0x2u;
    /**********STOP*******************/
    I2C1_C1 &= ~0x20u;
    I2C1_C1 &= ~0x10u;
   
    results =I2C1_D;
    for(n=0;n<IDLE_LOOP;n++)
      asm("nop");
   
    iLoop++;  
  }

Outcomes