温湿度正常

This commit is contained in:
stark1898y 2024-12-13 19:04:18 +08:00
parent f1b88a53ba
commit 09c5acf69f
2 changed files with 70 additions and 24 deletions

View File

@ -167,7 +167,7 @@ int main(void)
BSP_UART1_Init(1500000);
// logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logDebug("Start @ChipID=%02X\n", R8_CHIP_ID);
logError("中文测试");
logError("中文测试 %2.2f", 123.456);
#endif
DelayMs(1000 * 3);

View File

@ -9,7 +9,7 @@
#undef LOG_TAG
#define LOG_TAG "I2C"
#define CONFIG_I2C_DEBUG
// #define CONFIG_I2C_DEBUG
#ifdef CONFIG_I2C_DEBUG
#define I2C_DBG(...) logDebug(__VA_ARGS__)
@ -42,7 +42,7 @@ static struct i2c_slave_cb *slave_cb = NULL;
#define MASTER_ADDR 0x42
uint8_t i2c_tx_data[2];
uint8_t i2c_rx_data[8];
uint8_t i2c_rx_data[6];
void BSP_I2C_Init(uint8_t address)
{
@ -298,45 +298,84 @@ void GXHTC3C_GetStart(void)
GXHTC3C_SendCmd(GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT);
}
uint8_t CRC_8(uint8_t *p_crc, uint8_t len)
{
uint8_t crc_value = 0xFF;
uint8_t i = 0, j = 0;
// 定义CRC-8参数
#define POLYNOMIAL 0x31 // 生成多项式x8+x5+x4+1 (省略最高位1)
#define INIT_VALUE 0xFF // 初始化值
#define REFLECT_IN false // 不反射输入
#define REFLECT_OUT false // 不反射输出
for (i = 0; i < len; i++)
uint8_t GXHTC3C_CRC_8(uint8_t *p_crc, uint8_t len)
{
crc_value ^= *(p_crc + i);
for (j = 0; j < 8; j++)
uint8_t crc = INIT_VALUE;
for (size_t i = 0; i < len; ++i)
{
if (crc_value & 0x80)
crc_value = (crc_value << 1) ^ 0x31;
crc ^= p_crc[i]; // 异或当前字节
for (uint8_t j = 0; j < 8; ++j)
{
if (crc & 0x80)
{ // 如果最高位是1
crc = (crc << 1) ^ POLYNOMIAL; // 左移一位并异或多相式
}
else
crc_value = (crc_value << 1);
{
crc <<= 1; // 否则只左移一位
}
// 确保crc保持在8位内
crc &= 0xFF;
}
}
return crc_value;
// 最终返回CRC值
return crc;
}
// GXHTC3C_CMD_LOW_CLK_STRE_OFF_HUMI_FRONT
void GXHTC3C_GetTempHumi(void)
uint8_t GXHTC3C_GetTempHumi(float *humi, float *temp)
{
int ret;
ret = I2C_Read(GXHTC3C_ADDR, (uint8_t *)&i2c_rx_data, 6, true, 1000);
logDebug("read %d byte(s) from %#x", ret, GXHTC3C_ADDR);
if (ret >= 0)
if (ret != 6)
{
logError("read failed");
logHexDumpAll(i2c_rx_data, ret);
return 1;
}
// 低功耗、Clock stretching关闭、湿度在前
uint16_t humi = (i2c_rx_data[0] << 8) | i2c_rx_data[1];
uint8_t crc_humi = CRC_8(humi, 2);
uint16_t temp = (i2c_rx_data[3] << 8) | i2c_rx_data[4];
uint8_t crc_temp = CRC_8(temp, 2);
if (crc == i2c_rx_data[3])
uint8_t humi_raw_data[2];
uint8_t temp_raw_data[2];
humi_raw_data[0] = i2c_rx_data[0];
humi_raw_data[1] = i2c_rx_data[1];
uint16_t raw_humi = (humi_raw_data[0] << 8) | humi_raw_data[1];
uint8_t crc_humi = GXHTC3C_CRC_8(humi_raw_data, 2);
temp_raw_data[0] = i2c_rx_data[3];
temp_raw_data[1] = i2c_rx_data[4];
uint16_t raw_temp = (temp_raw_data[0] << 8) | temp_raw_data[1];
uint8_t crc_temp = GXHTC3C_CRC_8(temp_raw_data, 2);
if ((crc_humi == i2c_rx_data[2]) && (crc_temp == i2c_rx_data[5]))
{
logDebug("temp crc ok");
logDebug("crc ok");
logHexDumpAll(i2c_rx_data, 6);
*humi = (100.0 * raw_humi) / 65536.0; // 湿度真实值
*temp = (175.0 * raw_temp) / 65536.0 - 45.0; // 温度真实值
// logDebug("humi %f, temp %f", *humi, *temp);
}
else
{
logHexDumpAll(i2c_rx_data, 6);
logError("crc_temp 0x%02x, crc_humi 0x%02x", crc_temp, crc_humi);
logError("crc error");
return 2;
}
return 0;
}
void GXHTC3C_Init(void)
@ -346,12 +385,19 @@ void GXHTC3C_Init(void)
int ret;
GXHTC3C_Sleep();
DelayMs(2);
DelayMs(100);
GXHTC3C_Wakeup();
DelayMs(1);
DelayMs(2);
GXHTC3C_GetStart();
DelayMs(4);
float humi, temp;
ret = GXHTC3C_GetTempHumi(&humi, &temp);
if (ret == 0)
{
logDebug("humi %.2f %, temp %.2f C", humi, temp);
}
#endif
}