你好,我在学习I2C协议时参考了你的MINI板的裸机I2C-EEPROM的例程,发现似乎有点错误,在I2C_EE_BufferWrite()函数中,源代码如下:
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; //求出在新的页还要写入的字节数
/* If WriteAddr is I2C_PageSize aligned */
if(Addr == 0) //当刚好在一个页的开始处
{
if(NumOfPage == 0) //如果在一页的最开始处,同时一页够写
{
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
I2C_EE_WaitEepromStandbyState();
}
else
{
while(NumOfPage--) //如果在一页的开头,但是一页不够写
{
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_PageSize;
pBuffer += I2C_PageSize;
}
if(NumOfSingle!=0) //多余的字节数
{
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
I2C_EE_WaitEepromStandbyState();
}
}
}
else //存储地址不在一个页的开头
{
if(NumOfPage== 0) //例如:有可能只需要7的字节,但是开始的地址在一个页的第三个字节处,似乎就不能这样直接写入了
{
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
I2C_EE_WaitEepromStandbyState();
}
else
{
NumByteToWrite -= count;
NumOfPage = NumByteToWrite / I2C_PageSize;
NumOfSingle = NumByteToWrite % I2C_PageSize;
if(count != 0)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, count);
I2C_EE_WaitEepromStandbyState();
WriteAddr += count;
pBuffer += count;
}
while(NumOfPage--)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, I2C_PageSize);
I2C_EE_WaitEepromStandbyState();
WriteAddr += I2C_PageSize;
pBuffer += I2C_PageSize;
}
if(NumOfSingle != 0)
{
I2C_EE_PageWrite(pBuffer, WriteAddr, NumOfSingle);
I2C_EE_WaitEepromStandbyState();
}
}
}
}
也许是我错吧,希望大神能指导下,谢谢!
同时,我有点疑问:以前直接把外设裸机的驱动代码加载进uc/oss和enWin的工程中就可以使用了,但是就只这EEPROM的加载进去后执行不了。 |