void I2C_EE_BufferWrite(u8* pBuffer, u8 WriteAddr, u16 NumByteToWrite){
u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0;
//寫入地址是每頁的第幾位
Addr = WriteAddr % I2C_PageSize;
//在開始的一頁要寫入的個數(shù)
count = I2C_PageSize - Addr;
//要寫入的頁數(shù)
NumOfPage = NumByteToWrite / I2C_PageSize;
//不足一頁的個數(shù)
NumOfSingle = NumByteToWrite % I2C_PageSize;
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
//寫入地址是頁的開始
if (Addr == 0)
{
//數(shù)據(jù)小于一頁
if (NumOfPage == 0)
{
//寫少于一頁的數(shù)據(jù)
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
}
//數(shù)據(jù)大于等于一頁
else
{
while (NumOfPage--)//要寫入的頁數(shù)
{
//寫一頁的數(shù)據(jù)
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_PageSize;
pBuffer += I2C_PageSize;
}
//剩余數(shù)據(jù)小于一頁
if (NumOfSingle!=0)
{
//寫少于一頁的數(shù)據(jù)
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
}
}
}
//寫入地址不是頁的開始
else
{
//數(shù)據(jù)小于一頁
if (NumOfPage== 0)
{
//寫少于一頁的數(shù)據(jù)
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
}
//數(shù)據(jù)大于等于一頁
else
{
NumByteToWrite -= count;
//重新計算要寫入的頁數(shù)
NumOfPage = NumByteToWrite / I2C_PageSize;
//重新計算不足一頁的個數(shù)
NumOfSingle = NumByteToWrite % I2C_PageSize;
if (count != 0)
{
//將開始的空間寫滿一頁
I2C_EE_PageWrite(pBuffer, WriteAddr, count);
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
WriteAddr += count;
pBuffer += count;
}
//要寫入的頁數(shù)
while (NumOfPage--)
{
//寫一頁的數(shù)據(jù)
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_PageSize;
pBuffer += I2C_PageSize;
}
//剩余數(shù)據(jù)小于一頁
if (NumOfSingle != 0)
{
//寫少于一頁的數(shù)據(jù)
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
}
}
}
}
void I2C_EE_ByteWrite(u8* pBuffer, u8 WriteAddr)
{
//產(chǎn)生 I2C2傳輸 START條件
I2C_GenerateSTART(I2C2, ENABLE);
//通過START的產(chǎn)生,確定主機模式是否成功(檢測EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//發(fā)送器件地址(寫)
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
//檢測主機傳輸模式是否成功(檢測EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//通過外設(shè) I2C2發(fā)送地址
I2C_SendData(I2C2, WriteAddr);
//檢測主機發(fā)送的字節(jié)是否成功(檢測EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//通過外設(shè) I2C2發(fā)送數(shù)據(jù)
I2C_SendData(I2C2, *pBuffer);
//檢測主機發(fā)送的字節(jié)是否成功(檢測EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//產(chǎn)生 I2C2傳輸 STOP條件
I2C_GenerateSTOP(I2C2, ENABLE);
}
void I2C_EE_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)
{
//產(chǎn)生 I2C2傳輸 START條件
I2C_GenerateSTART(I2C2, ENABLE);
//通過START的產(chǎn)生,確定主機模式是否成功(檢測EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//發(fā)送器件地址(寫)
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
//檢測主機傳輸模式是否成功(檢測EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//通過外設(shè) I2C2發(fā)送地址
I2C_SendData(I2C2, WriteAddr);
//檢測主機發(fā)送的字節(jié)是否成功(檢測EV8)
while (! I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//寫數(shù)據(jù)
while (NumByteToWrite--)
{
//通過外設(shè) I2C2發(fā)送數(shù)據(jù)
I2C_SendData(I2C2, *pBuffer);
//指針++
pBuffer++;
//檢測主機發(fā)送的字節(jié)是否成功(檢測EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
//產(chǎn)生 I2C2 傳輸 STOP條件
I2C_GenerateSTOP(I2C2, ENABLE);
}
void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
//EEPROM設(shè)為待命狀態(tài)
I2C_EE_WaitEepromStandbyState();
//產(chǎn)生 I2C2傳輸 START條件
I2C_GenerateSTART(I2C2, ENABLE);
//通過START的產(chǎn)生,確定主機模式是否成功(檢測EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//在一個單一的數(shù)據(jù)傳輸情況下讀取數(shù)據(jù)之前禁用的ACK
if (NumByteToRead==1)
{
I2C_AcknowledgeConfig(I2C2, DISABLE);//失能I2C2 的應(yīng)答功能
}
//向指定的從 I2C設(shè)備傳送地址字,選擇發(fā)送方向
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
//檢測主機傳輸模式是否成功(檢測EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//使能I2C外設(shè)
I2C_Cmd(I2C2, ENABLE);
//通過外設(shè) I2C2發(fā)送地址
I2C_SendData(I2C2, ReadAddr);
//檢測主機發(fā)送的字節(jié)是否成功(檢測EV8)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//產(chǎn)生 I2C2 傳輸 START條件
I2C_GenerateSTART(I2C2, ENABLE);
//通過START的產(chǎn)生,確定主機模式是否成功(檢測EV5)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//向指定的從 I2C設(shè)備傳送地址字,選擇接收方向
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Receiver);
//檢測主機接收模式是否成功(檢測EV6)
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
//讀取數(shù)據(jù)
while (NumByteToRead)
{
//檢測主機接收的字節(jié)是否成功(檢測EV8)
if (I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if (NumByteToRead == 2)
{
//使能或者失能指定 I2C的應(yīng)答功能
I2C_AcknowledgeConfig(I2C2, DISABLE);
}
if (NumByteToRead == 1)
{
//產(chǎn)生 I2C2 傳輸 STOP條件
I2C_GenerateSTOP(I2C2, ENABLE);
}
//返回通過 I2C2最近接收的數(shù)據(jù)
*pBuffer = I2C_ReceiveData(I2C2);
//指向下個地址
pBuffer++;
NumByteToRead--;
}
}
//使能I2C2 的應(yīng)答功能
I2C_AcknowledgeConfig(I2C2, ENABLE);
}
void I2C_EE_WaitEepromStandbyState(void)
{
vu16 SR1_Tmp = 0;
do
{
//產(chǎn)生 I2C2傳輸 START條件
I2C_GenerateSTART(I2C2, ENABLE);
//讀取指定的 I2C寄存器 I2C_SR1 并返回其值
SR1_Tmp = I2C_ReadRegister(I2C2, I2C_Register_SR1);
//向指定的從 I2C設(shè)備傳送地址字 ,選擇發(fā)送方向
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
} while (!(I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0x0002));//地址發(fā)送結(jié)束
//清除 I2Cx的應(yīng)答錯誤標志位
I2C_ClearFlag(I2C2, I2C_FLAG_AF);
評論