Using the TMP100 series of I2C temperature sensor from Texas Instruments


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2007
    Location
    Paris
    Posts
    101

    Default Using the TMP100 series of I2C temperature sensor from Texas Instruments

    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.



    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:
    Code:
    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.
    Code:
    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!):
    Code:
    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!
    Last edited by aberco; - 21st September 2010 at 16:46.

  2. #2
    Join Date
    Feb 2006
    Location
    Gilroy, CA
    Posts
    1,530


    Did you find this post helpful? Yes | No

    Default

    Nice one Aberco.

    Promoted to article here: http://www.picbasic.co.uk/forum/cont...as-Instruments
    http://www.scalerobotics.com

  3. #3
    Join Date
    Dec 2007
    Location
    Paris
    Posts
    101


    Did you find this post helpful? Yes | No

    Default

    Bug report on the conversion routine... should be:
    Code:
    TempL = TempL + 16 'adding 1 temperature unit
    Wiki article was fixed

    Code:
    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:

    Code:
    TempDec = TempL * 625
    Not sure what takes the longest to compute, use whichever you like.
    Last edited by aberco; - 27th September 2010 at 16:36.

  4. #4
    Join Date
    Dec 2007
    Location
    Paris
    Posts
    101


    Did you find this post helpful? Yes | No

    Default

    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.

    Code:
    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

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts