初级会员

- 积分
- 54
- 金钱
- 54
- 注册时间
- 2019-8-10
- 在线时间
- 5 小时
|

楼主 |
发表于 2019-9-1 09:56:27
|
显示全部楼层
硬件IIC已经测试通过了,很好用,不需要还用延时模拟时序,直接让硬件自己去发送,方便多了。
经过测试,硬件IIC并无BUG,可能之前有,新的芯片修复了。
结论:可以大胆用硬件IIC,很方便,很稳定(并不像书中写的不稳定)。测试了读写上万次EEPROM与10分钟写PCF8574 IO扩展,程序都正常。
测试程序代码:
uint32_t I2C_EE_ByteWrite(uint8_t *pBuffer, uint8_t WriteAddr, uint8_t NumByteToWrite)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Write(&hi2c2, 0XA0, (uint16_t)WriteAddr, I2C_MEMADD_SIZE_8BIT, pBuffer, NumByteToWrite, 100);
/* Check the communication status */
if(status != HAL_OK)
{
/* Execute user timeout callback */
}
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY)
{
}
/* Check if the EEPROM is ready for a new operation */
while (HAL_I2C_IsDeviceReady(&hi2c2, 0XA0, 300, 300) == HAL_TIMEOUT);
/* Wait for the end of the transfer */
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY)
{
}
return status;
}
uint32_t I2C_IOE_ByteWrite(uint8_t dat)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Master_Transmit(&hi2c2, 0x40, &dat, 1, 100);
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY)
{
}
return status;
}
uint8_t I2C_IOE_ByteRead(void)
{
uint8_t dat;
HAL_I2C_Master_Receive(&hi2c2, 0x40, &dat, 1, 100);
while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY)
{
}
return dat;
}
uint32_t I2C_EE_BufferRead(uint8_t* pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
{
HAL_StatusTypeDef status = HAL_OK;
status = HAL_I2C_Mem_Read(&hi2c2,0XA0,ReadAddr, I2C_MEMADD_SIZE_8BIT, (uint8_t *)pBuffer, NumByteToRead,10);
return status;
}
模拟IIC需要关闭中断保证时序,硬件IIC时序由硬件生产,CPU可以相应中断,当然,更好的方法中断模式IIC,可以完全让CPU无等待,极大提高系统性能。
|
|