最近用SM39R08A5的硬件I2C,做SLAVE,用它自带的工具生成的代码,也不正确。测试的目的是读什么地址,就回什么数据。实际是第一个字节回的是上次回数据最后的数据
例。我读一次读 01,02数据,再接着读03,04数据,按原意是回 01,02,03,04的数据,但实际回的是00,02,02,04.就是是读03的时候,回的是上次读的最后一位数据。
图片传不上,不能直观的反映现象。
找不到原因。
void IIC_interrupt() interrupt d_IIC_Vector
{
if(RXIF)
{
if((IICA1 & 0x01) | (IICA2 & 0x01))
{
n_RW = RW;// save r/W state
// IIC bus ready
IICEBT = d_CMD_RW;
if (n_RW == d_Write)
{
n_Next_Step = d_SAVE_ADDR;
n_Addr = 0x00;
}
else if (n_RW == d_Read)
{
}
}
else if(n_RW == d_Write)
{
if (n_Next_Step == d_SAVE_ADDR)
{
n_Addr = IICRWD;
g_bRxIF = 1;
n_Next_Step = d_SAVE_DATA;
IICEBT = d_CMD_RW;
}
else if (n_Next_Step == d_SAVE_DATA)
{
}
}
RXIF = 0;
}
if (TXIF)
{
g_bTxIF=1;
TXIF = 0; // Clear interrupt flag
}
}
void IIC_init_slave()
{
IICS = 0x00; // init IICS
IRCON = 0x00; // init IRCON
IICEBT = d_CMD_RW; // IIC bus ready
IICA1 = d_IIC_Adress_1; // Control Byte 1
IICA2 = d_IIC_Adress_2; // Control Byte 2
IEN1 |= 0x20; // Enable interrupt IIC
IEN0 |= 0x80; // Enable interrupt All
IICCTL = 0x80; // Enable IIC module, slave mode, use IICA1
}
/
void Feedback_Data(void)
{
bACKS=d_NACK;
while(n_RW==d_Read)
{
if((RXAK == d_NACK)&(bACKS==d_ACK)) // End if recive NACK
{
n_RW = d_null;
bACKS = d_NACK;
}
else
{
// if(g_bRxIF)
{ g_bRxIF = 0;
IICRWD = n_Addr;
IICEBT = d_CMD_RW;
n_Addr++;
bACKS = d_ACK;
while(g_bTxIF==0);
g_bTxIF=0;
}
}
}
if(MPIF)
{
MPIF=0; // Clear interrupt flag
n_RW = 0x00;
}
}
void main(void)
{
IIC_init_slave();
while(1)
{
Feedback_Data();
}
}
|