cancel
Showing results for 
Search instead for 
Did you mean: 

How to read a sequence of bytes through I2C?

wsevendays
Associate II
Posted on December 12, 2011 at 23:40

Hello. I want to read a sequence of bytes from my accelerometer. I can't get it work.

When I read byte by byte all is ok, but when I try to read sequence I can't get a true values.

void ADXL_ReadSeq(uint8_t StartAddr,signed char *data)

{

// StartAddr |= (1<<7); //

    while(I2C_GetFlagStatus(I2CG, I2C_FLAG_BUSY)); // While the bus is busy

    I2C_GenerateSTART(I2CG, ENABLE); // Send START condition

    while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_MODE_SELECT)); // Test on EV5 and clear it

    I2C_Send7bitAddress(I2CG, DEV_ADDR<< 1, I2C_Direction_Transmitter); // Send address for write (SAD+W)

    while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); // Test on EV6 and clear it

    I2C_SendData(I2CG,ADXL_R| ADXL_MB|StartAddr ); //Multibyte reading

    while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // Test on EV8 and clear it

    I2C_GenerateSTART(I2CG, ENABLE); // Send STRAT condition a second time

    while(!I2C_CheckEvent(I2CG, I2C_EVENT_MASTER_MODE_SELECT)); // Test on EV5 and clear it

    I2C_Send7bitAddress(I2CG, DEV_ADDR<< 1, I2C_Direction_Receiver); // Send address for read

    while(!I2C_CheckEvent(I2CG,I2C_EVENT_MASTER_BYTE_RECEIVED)); // Test on EV6 and clear it

    int  i=0;

for (i;i<6;i++)

{

I2C_AcknowledgeConfig(I2CG, ENABLE);

data[i]=I2C_ReceiveData(I2CG);

delay(3000);

}

    I2C_AcknowledgeConfig(I2CG, DISABLE);

    I2C_GenerateSTOP(I2CG, ENABLE); // Send STOP Condition

}

What I'm doing wrong?

I convert this function from other function which uses SPI  

http://code.google.com/p/flyless/source/browse/trunk/FLYLess/src/hal/adxl345.c

#i2c #sequence #adxl345 #stm32
4 REPLIES 4
emalund
Associate III
Posted on December 13, 2011 at 18:33

When I read byte by byte all is ok, but when I try to read sequence I can't get a true values.

 

 

assuming you are using stm32f1xx read the errata

Erik

emalund
Associate III
Posted on December 13, 2011 at 18:33

When I read byte by byte all is ok, but when I try to read sequence I can't get a true values.

 

 

assuming you are using stm32f1xx read the errata

Erik

wsevendays
Associate II
Posted on December 13, 2011 at 18:49

Yes, I have stm32f103vet6. But I don't know where to search. Please help me.I'm getting data normally, but not data that I need.

emalund
Associate III
Posted on December 13, 2011 at 19:31

as said before READ THE ERRATA