TMP102 Temperature Sensor
Started by Eppetiano, May 05 2012 02:21 PM
7 replies to this topic
#1
Posted 05 May 2012 - 02:21 PM
Hello all, first of all thanks for this amazing forum.
I really need your help on this, i'm using the TMP102 temperature sensor, that returns data in a two byte format, but temperature value it's only on the 12 msb.
So my question is, how can i convert this two hexadecimal byte format to a Celsius value.
I got byte[1] = 224 and byte[0] = 22 which are two strange values...
I have my code as attachments.
Thanks!!
#2
Posted 05 May 2012 - 02:52 PM
var temperature = ((byte[0] << 4) | (byte[1] >> 4))*0.0625;The result for sample values is 22.875ºC.
#3
Posted 05 May 2012 - 03:13 PM
Hello CW2, first things first, THANKS A LOT for four quick and useful answer.
But, can you explain me how you came to that code?
Thanks!
#4
Posted 05 May 2012 - 06:51 PM
As you already know, the temperature is 12-bit value stored in two bytes
So, by shifting byte[1] four bits to the right those four zeros are replaced with T3 - T0, and the result is of type int
Please note this expression does not handle negative temperature.
bit# B7 B6 B5 B4 B3 B2 B1 B0 Byte[0] T11 T10 T9 T8 T7 T6 T5 T4 Byte[1] T3 T2 T1 T0 0 0 0 0To manipulate bits shift and logical operators are often used. Also, one important thing to note is that those operators are not really defined for byte type, so the operands are automatically converted to int, and thus the result is also int (C# specification 7.2.6.2).
So, by shifting byte[1] four bits to the right those four zeros are replaced with T3 - T0, and the result is of type int
bit# |B31..B12 B11 B10 B9 B8 B7 B6 B5 B4 B3 B2 B1 B0 Byte[0] | T11 T10 T9 T8 T7 T6 T5 T4 Byte[1] >> 4 | 0 .. 0 0 0 0 0 0 0 0 0 T3 T2 T1 T0In order to be able to combine two bytes into one 12-bit value, byte[0] has to be shifted four bits to the left, so T7-T4 precedes T3 - T0 (they overlap); the result is also int
bit# |B31..B12 B11 B10 B9 B8 B7 B6 B5 B4 B3 B2 B1 B0 Byte[0] << 4 | 0 .. 0 T11 T10 T9 T8 T7 T6 T5 T4 0 0 0 0 Byte[1] >> 4 | 0 .. 0 0 0 0 0 0 0 0 0 T3 T2 T1 T0The two integers (results of shift operations) are merged together by logical OR operator ('|')
bit# |B31..B12 B11 B10 B9 B8 B7 B6 B5 B4 B3 B2 B1 B0 Byte[0] << 4 | 0 .. 0 T11 T10 T9 T8 T7 T6 T5 T4 0 0 0 0 Byte[1] >> 4 | 0 .. 0 0 0 0 0 0 0 0 0 T3 T2 T1 T0 ------------------------------------------------------------ OR-ed | 0 .. 0 T11 T10 T9 T8 T7 T6 T5 T4 T3 T2 T1 T0The result is then multiplied by 0.0625, which is the resolution of the sensor, as specified in the datasheet.
Please note this expression does not handle negative temperature.
- Gutworks likes this
#5
Posted 05 May 2012 - 07:20 PM
The following expression handles negative temperatures too:
var temp = ((short)((byte0 << 8) | byte1))/256F;The analysis is left as an exercise to the reader (or available upon request)
#6
Posted 05 May 2012 - 10:23 PM
CW2, i just have to say one thing: THANK YOU SO MUCH!!!
What an incredible explanation, man!
It is now clear for me!
At last.
I'm really grateful for your help!
Keep it that way!
#7
Posted 15 May 2012 - 10:34 AM
Hi, could you share how the TMP102 is connected to Netduino? Thanks.
#8
Posted 26 May 2012 - 09:33 PM
I connected mine via I2C
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users