Uint16 I2CA_WriteData(struct I2CMSG *msg)
{
Uint16 i;
{
Uint16 i;
// 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;
// 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; //这代表I2C为主机模式
I2caRegs.I2CSAR = msg->SlaveAddress; //这代表I2C为主机模式
// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1) //代表接受到了起始信号
{
return I2C_BUS_BUSY_ERROR;
}
if (I2caRegs.I2CSTR.bit.BB == 1) //代表接受到了起始信号
{
return I2C_BUS_BUSY_ERROR;
}
// Setup number of bytes to send
// MsgBuffer + Address
I2caRegs.I2CCNT = msg->NumOfBytes+2; // 为一个设备地址,一个是数据地址
// MsgBuffer + Address
I2caRegs.I2CCNT = msg->NumOfBytes+2; // 为一个设备地址,一个是数据地址
// 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++)
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
// for (i=0; i<msg->NumOfBytes-2; i++)
for (i=0; i<msg->NumOfBytes; i++)
{
I2caRegs.I2CDXR = *(msg->MsgBuffer+i); //msg->MsgBuffer 是数组的首地址,则加1是下一个数组
}
I2caRegs.I2CDXR = *(msg->MsgBuffer+i); //msg->MsgBuffer 是数组的首地址,则加1是下一个数组
}
// Send start as master transmitter
I2caRegs.I2CMDR.all = 0x6E20; //0x0110 1110 0010 0000 理解位启动I2C
I2caRegs.I2CMDR.all = 0x6E20; //0x0110 1110 0010 0000 理解位启动I2C
return I2C_SUCCESS; //0x00
}
}
为什么写数据之前要判断stp==1? 状态,而不是首先发出start信号
还有就是,I2C例程里,怎么要加入这么多不同的状态——黄色部分

