初中生
最后登录1970-1-1
在线时间 小时
注册时间2023-12-8
|
已在STM32F103ZET6核心板上测试,其中调用的函数和宏与例程相同,与原多字节写入函数I2C_EE_BufferWrite()相比,省去了对齐计算
uint32_t I2C_EE_BufferWrite2(u8* pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite)
{
uint16_t i;
for(i = 0; i < NumByteToWrite; i++, WriteAddr++)
{
if(i == 0 || WriteAddr % 8 == 0)//开始时或每页开始时产生起始信号、发送设备地址、写入地址
{
I2CTimeout = I2CT_LONG_TIMEOUT;
while(I2C_GetFlagStatus(EEPROM_I2Cx, I2C_FLAG_BUSY))
if((I2CTimeout--) == 0)
return I2C_TIMEOUT_UserCallback(4);
I2C_GenerateSTART(EEPROM_I2Cx, ENABLE);
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_MODE_SELECT))
if((I2CTimeout--) == 0)
return I2C_TIMEOUT_UserCallback(5);
I2C_Send7bitAddress(EEPROM_I2Cx, EEPROM_ADDRESS, I2C_Direction_Transmitter);
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
if((I2CTimeout--) == 0)
{
return I2C_TIMEOUT_UserCallback(6);
}
I2C_SendData(EEPROM_I2Cx, WriteAddr);
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
if((I2CTimeout--) == 0)
return I2C_TIMEOUT_UserCallback(7);
}
I2C_SendData(EEPROM_I2Cx, pBuffer[i]);
I2CTimeout = I2CT_FLAG_TIMEOUT;
while(!I2C_CheckEvent(EEPROM_I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
if((I2CTimeout--) == 0)
return I2C_TIMEOUT_UserCallback(8);
if(i == NumByteToWrite - 1 || (WriteAddr + 1) % 8 == 0)//结束时或每页结束时产生停止信号、等待响应
{
I2C_GenerateSTOP(EEPROM_I2Cx, ENABLE);
I2C_EE_WaitEepromStandbyState();//页写入后必须等待响应
}
}
return 1;
}
|
|