自学内容网 自学内容网

【MCU】CH591用软件 I2C 出现的 bug

计算问题

写软件 I2C 时不支持

byte |= (0x80 >> i);

详细:

/**
 * @brief Receive one byte using I2C
 * @param XACK
 * @retval Byte
 */
uint8_t SW_I2C_ReceiveByte(uint8_t XACK)
{
SW_I2C_SDA_SetInput();
uint8_t byte = 0x00;
for (uint8_t i = 0; i < 8; i++)
{
SW_I2C_Delay();
SW_I2C_SCL_WriteBit(1);
if (SW_I2C_SDA_ReadBit() == 1)
{
byte |= (0x80 >> i);
}
SW_I2C_Delay();
SW_I2C_SCL_WriteBit(0);
SW_I2C_Delay();
}
SW_I2C_SDA_SetOutput();
// transmit ACK
SW_I2C_SDA_WriteBit(XACK);
SW_I2C_Delay();
SW_I2C_SCL_WriteBit(1);
SW_I2C_Delay();
SW_I2C_SCL_WriteBit(0);
// release SDA
SW_I2C_SDA_WriteBit(1);
SW_I2C_Delay();
return byte;
}

如果改成

/**
 * @brief Receive one byte using I2C
 * @param XACK
 * @retval Byte
 */
uint8_t SW_I2C_ReceiveByte(uint8_t XACK)
{
SW_I2C_SDA_SetInput();
uint8_t byte = 0x00;
for (uint8_t i = 0; i < 8; i++)
{
byte<<=1;
SW_I2C_Delay();
SW_I2C_SCL_WriteBit(1);
if (SW_I2C_SDA_ReadBit() == 1)
{
byte |= 0x01;
}
SW_I2C_Delay();
SW_I2C_SCL_WriteBit(0);
SW_I2C_Delay();
}
SW_I2C_SDA_SetOutput();
// transmit ACK
SW_I2C_SDA_WriteBit(XACK);
SW_I2C_Delay();
SW_I2C_SCL_WriteBit(1);
SW_I2C_Delay();
SW_I2C_SCL_WriteBit(0);
// release SDA
SW_I2C_SDA_WriteBit(1);
SW_I2C_Delay();
return byte;
}

就没问题了


原文地址:https://blog.csdn.net/weixin_51550287/article/details/145262040

免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!