有个项目,m0516 用i/o模拟i2c 连接FPGA 中的i2c Slave模块,通信时 ,循环读与写几百次中有发现错误(FPGA内的i2c slave模块与siliconlab的 C8051f330 同样的通信没出现问题),所以测试了以前认为没有问题的i2c接口连接的EEPROM AT24C02,先用内带的i2c硬件模式测试,4000次左右就有一次读出一个字节数据出错,然后关闭硬件i2c,用i/o口模拟i2c与 AT24C02连接,发现同样问题(i/o口模拟i2c 的程序应该没有问题,其它MCU中测试正常),不清楚是什么原因?
下面为i2c硬件模式的读程序:
BYTE iicread( BYTE addr )
{
BYTE c;
uint16_t i;
DrvI2C_Open(100000); /* Open I2C0 and set clock = 100Kbps */
DrvI2C_Ctrl(1, 0, 0, 0); //set start
while (I2C->CON.SI == 0); //poll si flag
I2C->DATA = 0XA0; //send writer command
DrvI2C_Ctrl(0, 0, 1, 0); //clr si
while( I2C->CON.SI == 0 ); //poll si flag
I2C->DATA = addr; //send address low
DrvI2C_Ctrl(0, 0, 1, 1); //clr si and set ack
while( I2C->CON.SI == 0 ); //poll si flag
DrvI2C_Ctrl(1, 0, 1, 0); //clr si and send start
while( I2C->CON.SI == 0 ); //poll si flag
I2C->DATA = 0XA1; //send read command
DrvI2C_Ctrl(0, 0, 1, 0); //clr si
while( I2C->CON.SI == 0 ); //poll si flag
//I2C->DATA = 0xff;
DrvI2C_Ctrl(0, 0, 1, 0); //clr si
while( I2C->CON.SI == 0 ); //poll si flag
c = I2C->DATA; //读取数据
DrvI2C_Ctrl(0, 1, 1, 0); //clr si and set stpo
delay(500);
DrvI2C_Close();
delay(500);
return c;
}
望高手解答.... |