【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;
}
就没问题了