各位大家好:
最近在用28035来进行I2c通讯,但是根据例程修改的I2C代码有点问题,如果我的从设备坏了(从地址没有应答),I2C总线就会一直返回忙,这样我想发数据给其他的从设备也发送不了,不知大家是怎么解决的?还希望大家能够指点我一下,谢谢。
mangui zhang:
你好,从你这么简单描述来看,无法确定问题出到哪儿了
代码问题有很大的可能 不知你的IIC上有没有上拉电阻
各位大家好:
最近在用28035来进行I2c通讯,但是根据例程修改的I2C代码有点问题,如果我的从设备坏了(从地址没有应答),I2C总线就会一直返回忙,这样我想发数据给其他的从设备也发送不了,不知大家是怎么解决的?还希望大家能够指点我一下,谢谢。
liangheng feng:
回复 mangui zhang:
mangui zhang:
你好。感谢你的回复。
IIC上有上拉电阻。下面是我的初始化代码:
// Initialize I2C I2caRegs.I2CSAR = 0x0050; // Slave address – EEPROM control code
I2caRegs.I2CPSC.all = 6; // Prescaler – need 7 – 12Mhz on module clk I2caRegs.I2CCLKL = 10; // NOTE: must be non zero I2caRegs.I2CCLKH = 5; // NOTE: must be non zero //I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset // Stop I2C when suspended
I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
然后是使用这个函数来发送:
Uint16 I2CA_WriteData(struct I2CMSG *msg){ Uint16 i;
if(CurrentMsgPtr->MsgStatus != I2C_MSGSTAT_RESTART) { // Wait until the STP bit is cleared from any previous master communication. // Clearing of this bit by the module is delayed until after the SCD bit is // set. If this bit is not checked prior to initiating a new message, the // I2C could get confused. if (I2caRegs.I2CMDR.bit.STP == 1) //停止条件位,停止才发送 { return I2C_STP_NOT_READY_ERROR; }// }
// Setup slave address I2caRegs.I2CSAR = msg->SlaveAddress;
// Check if bus busy if (I2caRegs.I2CSTR.bit.BB == 1) //检测总线忙,返回 { return I2C_BUS_BUSY_ERROR; }
// Setup number of bytes to send // MsgBuffer + Address I2caRegs.I2CCNT = msg->NumOfBytes;
// Send start as master transmitter //I2caRegs.I2CMDR.all = 0x6E20;
//while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //等待发送数据结束 // Setup data to send //I2caRegs.I2CDXR = msg->MemoryHighAddr; //I2caRegs.I2CDXR = msg->MemoryLowAddr;// for (i=0; i<msg->NumOfBytes-2; i++) for (i=0; i<msg->NumOfBytes; i++) {
// while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //等待发送数据结束 I2caRegs.I2CDXR = *(msg->MsgBuffer+i);
}
I2caRegs.I2CMDR.all = 0x6E20; }
return I2C_SUCCESS;}
当我发送一个总线上没有的从地址之后,就一直跑到这段代码,把地址改回来也不行。
if (I2caRegs.I2CSTR.bit.BB == 1) //检测总线忙,返回{return I2C_BUS_BUSY_ERROR;}
是不是可以手动把bus_busy位清掉?这种情况怕到时一个从设备坏了或没有响应就会导致IIC不能使用。