首页 > 解决方案 > 试图将精确温度降至小数点后 2 位

问题描述

您好,我正在运行展示集成温度传感器的 TI CC3220SF 介绍程序。与该传感器的通信使用 I2C,这很方便,因为我想使用相同的协议实现不同的更精确的传感器。在 TI 提供的示例中,他们将温度测量为 int,这会阻止精确测量。我尝试输入温度值,但它似乎不起作用。谁能帮我缓解这个问题?

/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)

{
  unsigned int    i;
  uint16_t        temperature;
  uint16_t        temperature_f;
  uint8_t         txBuffer[1];
  uint8_t         rxBuffer[2];
  I2C_Handle      i2c;
  I2C_Params      i2cParams;
  I2C_Transaction i2cTransaction;

/* Call driver init functions */
Display_init();
GPIO_init();
I2C_init();

/* Configure the LED pin */
GPIO_setConfig(Board_GPIO_LED0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);

/* Open the HOST display for output */
display = Display_open(Display_Type_UART, NULL);
if (display == NULL) {
    while (1);
}

/* Turn on user LED */
GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);
Display_printf(display, 0, 0, "Starting the i2ctmp006 example\n");

/* Create I2C for usage */
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(Board_I2C_TMP, &i2cParams);
if (i2c == NULL) {
    Display_printf(display, 0, 0, "Error Initializing I2C\n");
    while (1);
}
else {
    Display_printf(display, 0, 0, "I2C Initialized!\n");
}

/* Point to the T ambient register and read its 2 bytes */
txBuffer[0] = TMP006_DIE_TEMP;
i2cTransaction.slaveAddress = Board_TMP_ADDR;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.writeCount = 1;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.readCount = 2;

/* Take samples and print them out onto the console */
for (i = 0; i <= 5; i++) {
    if (I2C_transfer(i2c, &i2cTransaction)) {
        /* Extract degrees C from the received data; see TMP102 datasheet */
        temperature = (rxBuffer[0] << 6) | (rxBuffer[1] >> 2);

        /*
         * If the MSB is set '1', then we have a 2's complement
         * negative value which needs to be sign extended
         */
        if (rxBuffer[0] & 0x80) {
            temperature |= 0xF000;
        }
       /*
        * For simplicity, divide the temperature value by 32 to get rid of
        * the decimal precision; see TI's TMP006 datasheet
        */
        temperature /= 32;
        temperature_f = ((temperature * 9 / 5) + 32);

        Display_printf(display, 0, 0, "Sample %u: %d (C)", i, temperature);
        Display_printf(display, 0, 0, "       %u: %d (F)\n", i, temperature_f);
    }
    else {
        Display_printf(display, 0, 0, "I2C Bus fault\n");
    }

    /* Sleep for x seconds */
    sleep(1);
}

/* Deinitialized I2C */
I2C_close(i2c);
Display_printf(display, 0, 0, "I2C closed!\n");

return (0);
}

标签: c

解决方案


这两行进行整数数学运算,从而摆脱了精度

temperature /= 32;
temperature_f = ((temperature * 9 / 5) + 32);

您需要定义一个新变量并更改temperature_f类型。

float ftemperature = 0.0f;
float temperature_f = 0.0f;
...
ftemperature = temperature / 32.0f;
temperature_f = ((ftemperature * 9.0f / 5.0f) + 32.0f);

更明确地说,您可以使用类似的演员表temperature

ftemperature = ((float) temperature) / 32.0f;

但是,这32.0f足以告诉编译器隐式地进行转换。

ftemperature现在您在和中获得了您想要的精度temperature_f。然后您可以在显示功能中使用它。

Display_printf(display, 0, 0, "Sample %u: %f (C)", i, ftemperature);
Display_printf(display, 0, 0, "       %u: %f (F)\n", i, temperature_f);

%f格式可能需要一点帮助。您可能也想使用%g。有关详细信息,请参阅printf文档。


推荐阅读