Temperature calculation - Page 1
Forum section: electronics / projects

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 21, 2013, 10:19:57 pm
content: How would you calculate the temperature with this characteristics:"Temperature data (1LSb/deg - 8-bit resolution).The value is expressed astwo’s complement."If I do a straight two's complement to decimal, the values make no sense, because when I heat the sensor the raw result decreases.And I'm doing this:Temperature = (256 - (double)Data) / 10;But the temperature is off compared to other two thermometers I have here. So I don't know if the sensor is off or my calculations are not correct.Please help.David.

author: Bored@Work
author_rank: Super Contributor
timestamp: March 21, 2013, 10:31:31 pm
content: First thing you should clarify if the data is in F or C.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 21, 2013, 10:34:30 pm
content: It's on C#.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 21, 2013, 10:42:36 pm
content: Hahahahahaha !Ah Celsius man.You know we have F# and C#. I didn't understand you question at the first place.

author: amspire
author_rank: Super Contributor
timestamp: March 21, 2013, 10:43:27 pm
content: What is the temperature sensor?Why is it in two's compliment - can it be both positive and negative? Most temperature sensors will involve an A/D reading a positive voltage from a sensor (an integer) that it converts into a temperature. In this case, two's compliment would just waste the most significant digit.Why are you subtracting the data from 256? Two's compliment numbers look like integers for positive values. If it is 8 bit two's complement, then if it is 128 or higher, then you have to subtract 256 from the data number. If it is less then 128, you just use the data as a positive number directly.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 21, 2013, 10:54:23 pm
content: The temperature sensor is a LSM330.I know about the two's compliment. But as I mentioned before, the raw data looks weird. If I heat the IC a little bit the raw data will decrease.For example reading ambient temperature I got the raw data reading of: 0x18.If I heat the IC the reading will be: 0x12I get the measurements using I2C. I have checked other registers and their values are correct, so the I2C settings looks correct.From the datasheet:OUT_TEMP_G (26h)Temperature data output register (r).Table 118. OUT_TEMP_G register:Temp7 Temp6 Temp5 Temp4 Temp3 Temp2 Temp1 Temp0Table 119. OUT_TEMP_G register description:Temp [7:0] Temperature data (1LSb/deg - 8-bit resolution).The value is expressed astwo’s complement.David.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 21, 2013, 10:56:02 pm
content: And yes the sensor can sense from -40 to +85 CI have tried two evaluation boards and both behave similar.David.

author: ecat
author_rank: Frequent Contributor
timestamp: March 21, 2013, 11:24:56 pm
content: One possibility:The temperature value returned by the sensor is not based around 0 degC but around some other value. For example, the sensor reading of 0 indicates 40 degC, so, a return value of 0x12 (18 dec) means the actual temperature is 40 - 18 = 22 degC and 0x18 (24 dec) would be 40 - 24 = 16 degC.Bizarrely, this would mean temperatures > 40 would return as negative values but, hay, the maths works...

author: DavidDLC
author_rank: Frequent Contributor
timestamp: March 21, 2013, 11:39:09 pm
content: ecat, thanks a lot, yes that maybe one possibility, I'm looking if the temperature is related to a value.

author: amspire
author_rank: Super Contributor
timestamp: March 22, 2013, 12:45:42 am
content: The data sheet is pretty pathetic - in one place they say it is -1 digit/C, in another 1digit/C. No mention of an offset.Since they are using twos complement, I would assume that 0 = 0 degC.Wouldn't it be this simple?signed char data ;double temperature;data = (temperature register value from LSM330);temperature = (double) data;Let the compiler handle all the twos complement conversion.Since they have gone to the trouble to convert a positive voltage read from a temp sensor into a signed voltage with 1 digit = 1C, I find it had to believe they wouldn't make the digit count match the temperature.

author: kxenos
author_rank: Frequent Contributor
timestamp: March 23, 2013, 12:09:13 am
content: Trysbyte temp = (sbyte) data;orsbyte temp = ((sbyte) data) - 40;