AnsweredAssumed Answered

Getting wrong axies from my accelormeter

Question asked by cohen.gal on Feb 25, 2016
Latest reply on Feb 25, 2016 by cohen.gal
Hello everyone,

I have trying to reading axies from my accelerometer and I keep getting 0 from my X Y Z axies at movement.
Have someone encounter this problem?
My code:

void writeTo(u8 addressToWrite,uint8_t value, u8 address)
{
  I2C_start(I2C3, address, I2C_Direction_Transmitter);
  I2C_write(I2C3,addressToWrite);
  I2C_write(I2C3,value);
  I2C_stop(I2C3);
}
 
void setup()
{
     writeTo(DATA_FORMAT, 0x0B, WRITE_ADDRESS_1);
     writeTo(POWER_CTL, 0x08, WRITE_ADDRESS_1);
     writeTo(INT_ENABLE, 0x80, WRITE_ADDRESS_1);
}
 
void ReadOperation(uint8_t address)
{
  I2C_start(I2C3, WRITE_ADDRESS_1 , I2C_Direction_Transmitter);
  I2C_write(I2C3,address);
  I2C_stop(I2C3);
}




void ReadFromAccel()
{
          ReadOperation(DATAX);
          I2C_start(I2C3,READ_ADDRESS_1 , I2C_Direction_Receiver);  //Normal Reading and then offset
          Accel_Param.X_1 = I2C_read_nack(I2C3);
          Delay(0xFFFFa);
          
               ReadOperation(DATAY);
          I2C_start(I2C3,READ_ADDRESS_1 , I2C_Direction_Receiver);  //Normal Reading and then offset
          Accel_Param.Y_1 = I2C_read_nack(I2C3);
          Delay(0xFFFFa);
          
               ReadOperation(DATAZ);
          I2C_start(I2C3,READ_ADDRESS_1 , I2C_Direction_Receiver);  //Normal Reading and then offset
          Accel_Param.Z_1 = I2C_read_nack(I2C3);
          Delay(0xFFFFa);
}


//void ReadFromAccel() //Try to read all the axies together
//{
//          ReadOperation(DATAX);
//          I2C_start(I2C3,READ_ADDRESS_1 , I2C_Direction_Receiver);  //Normal Reading and then offset
//          Accel_Param.X_1 = I2C_read_ack(I2C3);
//          Accel_Param.Y_1 = I2C_read_ack(I2C3);
//          Accel_Param.Z_1 = I2C_read_nack(I2C3);
//          Delay(0xFFFFa);
//}




void ResetAccel()
{
     int i;
  inti_RCC();
  init_I2C3();
     
     GLB_DelayUs(1100); //need delay of 1.1 ms
  setup(); 
      
     GLB_DelayUs(11000); //need delay of 11.1 ms
     
     for(i =0; i< 100; i++) //For noise
     {
          ReadFromAccel();
     }
     
     for(i =0; i< 100; i++) 
     {
          ReadFromAccel();
     }
}


 
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction)
{
       // wait until I2C3 is not busy anymore
          while(I2C_GetFlagStatus(I2C3, I2C_FLAG_BUSY));
   
   // Send I2C3 START condition
    I2C_GenerateSTART(I2C3, ENABLE);     
     
    // wait for I2C3 EV5 --> Slave has acknowledged start condition
    while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_MODE_SELECT));
 
    // Send slave Address for write
    I2C_Send7bitAddress(I2C3, address, direction);


        if(direction == I2C_Direction_Transmitter){
        while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); 
    }
    else if(direction == I2C_Direction_Receiver){
       while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    }
}
 
 
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
{
    I2C_SendData(I2C3, data);
         
    // wait for I2C3 EV8_2 --> byte has been transmitted
    while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
 
void I2C_stop(I2C_TypeDef* I2Cx)
{
    // Send I2C3 STOP Condition
    I2C_GenerateSTOP(I2C3, ENABLE);
        while(!I2C_CheckEvent(I2C3, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); 
}


// read one byte and don't request another byte, stop transmission
uint8_t I2C_read_nack(I2C_TypeDef* I2Cx){
          uint8_t data;
    // disable acknowledge of received data
    // nack also generates stop condition after last byte received
    I2C_AcknowledgeConfig(I2Cx, DISABLE);
    I2C_GenerateSTOP(I2C3, ENABLE); 
    // wait until one byte has been received
    while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
    // read data from I2C data register and return data byte
    data = I2C_ReceiveData(I2Cx);
    return data;
}

Outcomes