通过I2C读取2个字节

时间:2017-04-18 11:11:15

标签: c microcontroller i2c lpc nxp-microcontroller

我正在写作,因为我正在尝试实现一个读取2个寄存器(或更多)的I2C通信协议,并从每个寄存器接收一个字节。据我所知,写入部分是根据逻辑分析仪正确完成的。问题是我只收到一个字节重复两次而不是每个寄存器一个字节。这是我的第一个LPC / I2C程序,我想某处肯定会有一些小错误。我附加了读取功能,主要和逻辑分析仪输出。我想读取寄存器0x28和0x29并接收加速度(LSM9DS0 IMU)。

我正在使用LPC1549 Xpresso

提前感谢您的帮助和时间。

阅读功能:

uint32_t E2PROM_Read(uint8_t slave_address, uint16_t read_address, uint16_t   bytes_to_read, uint8_t * in_buffer)
{
uint8_t out_buffer[2];
out_buffer[0]=0x0000;
out_buffer[1]=0x0000;

//out_buffer[0] high address
//out_buffer[1] low address
I2CM_XFER_T  i2cmXferRec;

out_buffer[0] = (uint8_t)(read_address);
out_buffer[1] = (uint8_t)(read_address + 0x01);


i2cmXferRec.slaveAddr = slave_address;
i2cmXferRec.status    = 0;
i2cmXferRec.txSz      = 2;
i2cmXferRec.txBuff    = out_buffer;
i2cmXferRec.rxSz      = bytes_to_read;
i2cmXferRec.rxBuff    = in_buffer;




//* Test for valid operation */
        if (i2cmXferRec.status == I2CM_STATUS_OK) {
            /* Output acc. */
            DEBUGOUT("acceleration read over I2C is: \n %d \n %d ",
            (int) in_buffer[0], (int) in_buffer[1] );

Chip_I2CM_XferBlocking(LPC_I2C0, &i2cmXferRec);



return i2cmXferRec.status;
// .status = 0 if ok
    }
        else {
                DEBUGOUT("Error %d reading temperature.\r\n", i2cmXferRec.status );
            }
}

调用该函数:

uint8_t in_buffer[10];
uint8_t read_address=0x28;
E2PROM_Write(I2C_TEMP_ADDR_7BIT, 0x20, 2, out_buffer1);

E2PROM_Write(I2C_TEMP_ADDR_7BIT, 0x21, 2, out_buffer2);
/* Loop forever, toggle LED on board */
while (1) {

    /* Sleep until a state change occurs in SysTick */
    while (lastState == state) {
        __WFI();
    }


    E2PROM_Read(I2C_TEMP_ADDR_7BIT, read_address, 2, in_buffer);

这是我从逻辑分析仪得到的,我正确地写了两个地址,但接收相同的字节而不是两个不同的字节(每个地址一个)。

enter image description here

0 个答案:

没有答案