PDA

View Full Version : Using the TMP100 series of I2C temperature sensor from Texas Instruments



aberco
- 21st September 2010, 15:41
Here is bits and pieces of code to help in interfacing a TMP100 series temperature sensor to your PIC project! They are nice and easy to use sensors, with 2 to 0.5°C accuracy and possibility of triggering alarms on low/high temp limit (TMP101). They are however quite small, in SOT23-6 package.

http://aberco.free.fr/imagesdivers/tmp100.jpg

Wire SDA and SDC to your pic (using 4.7k pullups) and ground A0 and A1 (or change the address in the code). For TMP101, A1 is replaced by ALERT which you can wire to an interrupt pin of the PIC.

When powered up, the TMP100 does automatic sampling. The following line can be sent to configure the sensor to 12bit resolution:


SCLpin VAR PORTB.6
SDApin VAR PORTB.4
TempH VAR Byte
TempL VAR Byte

I2CWRITE SDApin,SCLpin,$90,$01,[%01100100] 'Set continuous sampling at 12bit resolution

I2CREAD SDApin,SCLpin,$90,$00,[TempH,TempL] 'Read temperature sample


To save power between samples, the temperature sensor can be set to power off, and conversion can be triggered by sending a one shot sample request.


SCLpin VAR PORTB.6
SDApin VAR PORTB.4
TempH VAR Byte
TempL VAR Byte

I2CWRITE SDApin,SCLpin,$90,$01,[%01100101] 'Set 12 bit resolution, turn sensor off

SamplingLoop:
I2CWRITE SDApin,SCLpin,$90,$01,[%11100101] 'one-shot sample
PAUSE 350
I2CREAD SDApin,SCLpin,$90,$00,[TempH,TempL] 'Read temperature sample
GOTO SamplingLoop


Remember that a 12bit conversion does take 320ms to complete. Therefore after 1 shot sampling, you need to wait or you will read the previous value.

Temperature is read as a 16 bit value (TempH being MSB and TempL LSB). Here is the conversion routine to display the value on LCD (initialize your LCD first!):


TempSign VAR Byte
TempDec VAR Word
TempDec3 VAR Byte
TempDec2 VAR Byte
TempDec1 VAR Byte
TempDec0 VAR Byte

'************************************************* ******
'Convert temperature to LCD
'************************************************* ******
ConvertTemp:

IF TempH.7 = 1 THEN
TempSign = "-"
TempL = TempL ^ %11110000 'bitwise invert on the 4 MSB
TempL = TempL + 8 'adding 1 temperature unit
TempH = TempH ^ %11111111 'bitwise invert
IF TempL = 0 THEN
TempH = TempH + 1
ENDIF
ELSE
TempSign = "+"
ENDIF

TempDec = 0
IF TempL.7 = 1 THEN TempDec = 5000
IF TempL.6 = 1 THEN TempDec = TempDec + 2500
IF TempL.5 = 1 THEN TempDec = TempDec + 1250
IF TempL.4 = 1 THEN TempDec = TempDec + 625

TempDec3 = TempDec DIG 3
TempDec2 = TempDec DIG 2
TempDec1 = TempDec DIG 1
TempDec0 = TempDec DIG 0

LCDOUT $FE,1, TempSign, #TempH, ".", #TempDec3,#TempDec2,#TempDec1,#TempDec0, "°C"

RETURN


Hope it will help, enjoy!

ScaleRobotics
- 21st September 2010, 16:33
Nice one Aberco.

Promoted to article here: http://www.picbasic.co.uk/forum/content.php?r=242-Using-the-TMP100-series-of-I2C-temperature-sensor-from-Texas-Instruments

aberco
- 27th September 2010, 15:31
Bug report on the conversion routine... should be:

TempL = TempL + 16 'adding 1 temperature unit
Wiki article was fixed


TempDec = 0
IF TempL.7 = 1 THEN TempDec = 5000
IF TempL.6 = 1 THEN TempDec = TempDec + 2500
IF TempL.5 = 1 THEN TempDec = TempDec + 1250
IF TempL.4 = 1 THEN TempDec = TempDec + 625

can also be replaced by:


TempDec = TempL * 625

Not sure what takes the longest to compute, use whichever you like.

aberco
- 29th September 2010, 14:45
And more code cleaning... I don't need yet the part to display to LCD so that's why I haven't paid much attention to it. Works the same way but with much less resources.



TempSign VAR Byte
TempDec VAR Word

'************************************************* ******
'Convert temperature to LCD
'************************************************* ******
ConvertTemp:

IF TempH.7 = 1 THEN
TempSign = "-"
TempL = TempL ^ %11110000 'bitwise invert on the 4 MSB
TempL = TempL + 16 'adding 1 temperature unit
TempH = TempH ^ %11111111 'bitwise invert
IF TempL = 0 THEN
TempH = TempH + 1
ENDIF
ELSE
TempSign = "+"
ENDIF

TempDec = TempL * 625

LCDOUT $FE,1, TempSign, #TempH, ".", #TempL, "°C"

RETURN