我试图从STC3100电池监视器IC中读取值,但是我得到的值不正确。什么是数据表说:如何从两个寄存器中读取2的补码值为int
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?
您能打印单个字节reg_l和reg_h以确保它们按预期方式出现吗? – cnicutar 2012-08-12 07:31:12
你能解释你所得到的值是不正确的吗?你得到了什么(寄存器的值是'res_l'和'res_h'),你期望什么? – 2012-08-12 07:31:13
该死的。删除我愚蠢的评论。绝对是一个好主意,分别打印res_l和res_h以查看它们是什么。 – 2012-08-12 07:39:15