我正在尝试从STC3100电池监测芯片中读取数值,但我得到的数值不正确。根据数据手册所述:
The temperature value is coded in 2’s complement format, and the LSB value is 0.125° C.
REG_TEMPERATURE_LOW, address 10, temperature value, bits 0-7
REG_TEMPERATURE_HIGH, address 11, temperature value, bits 8-15
这是数据表格:http://www.st.com/internet/com/TECHNICAL_RESOURCES/TECHNICAL_LITERATURE/DATASHEET/CD00219947.pdf 我的代码里有以下内容:
__u8 regaddr = 0x0a; /* Device register to access */
__s32 res_l, res_h;
int temp_value;
float temperature;
res_l = i2c_smbus_read_word_data(myfile, regaddr);
regaddr++;
res_h = i2c_smbus_read_word_data(myfile, regaddr);
if (res_l < 0) {
/* ERROR HANDLING: i2c transaction failed */
} else {
temp_value = (res_h << 8)+res_l;
temperature = (float)temp_value * 0.125;
printf("Temperature: %4.2f C\n", temperature);
}
我做错了什么?我不应该这样将2的补码值复制到int中吗?
res_l
和res_h
),你期望得到什么? - Michael Burri2c_smbus_read_word_data()
,你正在读取另外两个寄存器。请参见我的答案以获取更多详细信息。 - Michael Burr