如何通过I2C读取字节序列?

时间:2011-12-14 05:44:38

标签: c arm sequence i2c stm32

我想从加速度计读取一系列字节。我无法让它发挥作用。 当我逐字节读取时,一切正常,但是当我尝试读取序列时,我无法获得真正的值。

  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 );  // Send the internal address to read from: MSB of the address first
        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 num=6;
        while (num)
            {
                if(num==1)
                {
                    I2C_AcknowledgeConfig(I2C1, DISABLE);   /* ЧоєуТ»О»єуТЄ№Ш±ХУ¦ґрµД */
                    I2C_GenerateSTOP(I2C1, ENABLE);         /* ·ўЛННЈЦ№О» */
                }

                while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));  /* EV7 */
                *data = I2C_ReceiveData(I2C1);
                data++;
                /* Decrement the read bytes counter */
                num--;
            }
            /* ФЩґОФКРнУ¦ґрДЈКЅ */
            I2C_AcknowledgeConfig(I2C1, ENABLE);

    }

我做错了什么? 我从使用SPI http://code.google.com/p/flyless/source/browse/trunk/FLYLess/src/hal/adxl345.c

的其他函数转换此函数

1 个答案:

答案 0 :(得分:2)

检查加速度计的文档。 I2C与SPI非常不同,您的设备可能不支持I2C读取序列。