请问有CC2538硬件I2C多字节收发程序啊?
弄了很久的硬件I2C了,突然发现它不能多字节接收,I2C的接收程序如下:
bool I2C_Recv(uint8 slave_addr,uint8 registerId,uint8* string,uint8 len)
{
if(len < 1 | I2CMasterBusBusy() || NULL == string)
{
return false;
}
I2CMasterSlaveAddrSet(slave_addr, false);
I2CMasterDataPut(registerId);
I2CMasterControl(I2C_MASTER_CMD_SINGLE_SEND);
while(I2CMasterBusy());
I2CMasterSlaveAddrSet(slave_addr, true);
if(len == 1)
{
I2CMasterControl(I2C_MASTER_CMD_SINGLE_RECEIVE);
while(I2CMasterBusy());
*string = (uint8)I2CMasterDataGet();
}
else
{
I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_START);
for(uint8 count = 0;count < len;count++)
{
string[count] = (uint8)I2CMasterDataGet();
if(count < (len-1))
{
while(I2CMasterBusy());
I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_CONT);
}
else
{
I2CMasterControl(I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
}
}
}
return true;
}
多字节收发是没有问题的;
只是每次只收发一个字节,多字节拆分之后也都能正常。
搞定了,弄反了一个顺序,应该把while(I2CMasterBusy());放在接收数据前面的