demo只做了针对一个字节的读取,下面是我的连续读取的code,主要问题在于应答的设定,这个在技术参考手册里面有提到,可以自己去看看
/*---------------------------------------------------------------------------------------------------------*/
/* I2C0 (Master) Rx Callback Function */
/*---------------------------------------------------------------------------------------------------------*/
void I2C0_Callback_Rx(uint32_t status)
{
if (status == 0x08) /* START has been transmitted and prepare SLA+W */
{
DrvI2C_WriteData(I2C_PORT0, Device_Addr<<1);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
}
else if (status == 0x18) /* SLA+W has been transmitted and ACK has been received */
{
DrvI2C_WriteData(I2C_PORT0, Tx_Data[0]);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
}
else if (status == 0x20) /* SLA+W has been transmitted and NACK has been received */
{
DrvI2C_Ctrl(I2C_PORT0, 1, 1, 1, 0);
}
else if (status == 0x28) /* DATA has been transmitted and ACK has been received */
{
DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);
}
else if (status == 0x10) /* Repeat START has been transmitted and prepare SLA+R */
{
DrvI2C_WriteData(I2C_PORT0, Device_Addr<<1 | 0x01);
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
}
else if (status == 0x40) /* SLA+R has been transmitted and ACK has been received */
{
if(RxLen == 1)
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
else
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);
}
else if (status == 0x50) /* DATA has been received and ACK has been returned */
{
Rx_Data[DataLen++] = DrvI2C_ReadData(I2C_PORT0);
if(DataLen < (RxLen-1))
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);
else
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
}
else if (status == 0x58) /* DATA has been received and NACK has been returned */
{
Rx_Data[DataLen] = DrvI2C_ReadData(I2C_PORT0);
DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0);
EndFlag = 1;
}
} |