MEL PICBASIC Forum - Using the TMP100 series of I2C temperature sensor from Texas Instruments


  • 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
    
    '*******************************************************
    '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 = 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
    
    LCDOUT $FE,1, TempSign, #TempH, ".", #TempDec DIG 3, #TempDec DIG 2, #TempDec DIG 1, #TempDec DIG 0, "°C"
    
    RETURN
    Hope it will help, enjoy!
    This article was originally published in forum thread: Using the TMP100 series of I2C temperature sensor from Texas Instruments started by aberco View original post