void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite)
{
u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;
//写入地址是每页的第几位
Addr = WriteAddr % I2C_PageSize;
//在开始的一页要写入的个数
count = I2C_PageSize - Addr;
//要写入的页数
NumOfPage = NumByteToWrite / I2C_PageSize;
//不足一页的个数
NumOfSingle = NumByteToWrite % I2C_PageSize;
//EEPROM设为待命状态
I2C_EE_WaitEepromStandbyState();
//写入地址是页的开始
if (Addr == 0)
{
//数据小于一页
if (NumOfPage == 0)
{
//写少于一页的数据
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM设为待命状态
I2C_EE_WaitEepromStandbyState();
}
//数据大于等于一页
else
{
while (NumOfPage--)//要写入的页数
{
//写一页的数据
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
//EEPROM设为待命状态
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_PageSize;
pBuffer += I2C_PageSize;
}
//剩余数据小于一页
if (NumOfSingle!=0)
{
//写少于一页的数据
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM设为待命状态
I2C_EE_WaitEepromStandbyState();
}
}
}
//写入地址不是页的开始
else
{
//数据小于一页
if (NumOfPage== 0)
{
//写少于一页的数据
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM设为待命状态
I2C_EE_WaitEepromStandbyState();
}
//数据大于等于一页
else
{
NumByteToWrite -= count;
//重新计算要写入的页数
NumOfPage = NumByteToWrite / I2C_PageSize;
//重新计算不足一页的个数
NumOfSingle = NumByteToWrite % I2C_PageSize;
if (count != 0)
{
//将开始的空间写满一页
I2C_EE_PageWrite(pBuffer, WriteAddr, count);
//EEPROM设为待命状态
I2C_EE_WaitEepromStandbyState();
WriteAddr += count;
pBuffer += count;
}
//要写入的页数
while (NumOfPage--)
{
//写一页的数据
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
//EEPROM设为待命状态
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_PageSize;
pBuffer += I2C_PageSize;
}
//剩余数据小于一页
if (NumOfSingle != 0)
{
//写少于一页的数据
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM设为待命状态
I2C_EE_WaitEepromStandbyState();
}
}
}
}
void I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr)
{
//产生 I2C2传输 START条件
I2C_GenerateSTART(I2C2, ENABLE);
//通过START的产生,确定主机模式是否成功(检测EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//发送器件地址(写)
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
//检测主机传输模式是否成功(检测EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//通过外设 I2C2发送地址
I2C_SendData(I2C2, WriteAddr);
//检测主机发送的字节是否成功(检测EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//通过外设 I2C2发送数据
I2C_SendData(I2C2, *pBuffer);
//检测主机发送的字节是否成功(检测EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//产生 I2C2传输 STOP条件
I2C_GenerateSTOP(I2C2, ENABLE);
}
void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
//产生 I2C2传输 START条件
I2C_GenerateSTART(I2C2, ENABLE);
//通过START的产生,确定主机模式是否成功(检测EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//发送器件地址(写)
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
//检测主机传输模式是否成功(检测EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));