我用的nuc120的I2C作为从机只能发送和接收一个正确的数据,大于一个数据接受到主机的数据就不正确,主机用的是模拟的I2C,从机用的是自带的I2C,我用串口打印出主机信息,显示是主机在发送完第二个数据后没收到从机的应答 ,我看新塘的数据手册上有这样一段话file:///D:/QQ/Users/350744253/Image/pgkc6kavg0y%7Ddod2%7B%25tfdks.jpg
是不是他发送或接受到一个数据之后就不会发送AA应答了 ,
下面是我的I2C初始化和中断程序
void I2C_init(void)
{
/* Set I2C I/O */
DrvGPIO_InitFunction(E_FUNC_I2C0);
DrvI2C_Open(I2C_PORT0, 400000);
DrvI2C_SetAddress( I2C_PORT0,0,Device_Addr>>1,0 );
// 设置I2C 地址1,并将广播命令关闭(高七位为地址,最低一位为读写位)
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 0, 1);
//设置为从机模式
DrvI2C_EnableInt(I2C_PORT0);
// 使能I2C_PORT
DrvI2C_InstallCallback(I2C_PORT0, I2CFUNC, I2C0_Callback_Slave);
//使能中断
}
void I2C0_Callback_Slave(uint32_t status)
{
// Power_Off();
if ((status == 0x60) || (status == 0x68)) /* SLA+W has been received and ACK has been returned */
{//从机接收模式
DataLen1 = 0;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1); //产生应答,清中断SI
}
else if (status == 0x80) /* DATA has been received and ACK has been returned */
{
Addr1[DataLen1++] = DrvI2C_ReadData(I2C_PORT0);
if (DataLen1 == 2)
{
aaa = 0;
// DataLen1=2;
}
if ((DataLen1 == 5))
{
aaa = 1;
DataLen1 = 0;
}
// if(DataLen1==1||DataLen1==2)
DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1);
// else
// DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0);
}
else if ((status == 0xB0) || (status == 0xA8)) /* SLA+R has been received and ACK has been returned */
{ //从机传输模式
DrvI2C_WriteData(I2C_PORT0, Addr2[DataLen2++]);
if(DataLen2>=3)DataLen2=0;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);
}
else if (status == 0xC0) /* DATA has been transmitted and NACK has been received */
{
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);
}
else if (status == 0xA0) /* STOP or Repeat START has been received */
{
DataLen1 = 0;
DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);
}
else
{
printf("Status 0x%x is NOT processed\n", status);
}
} |