• PicŪ Basic


  • Work with LM75A

    contributed by Olivier de Broqueville.
    The LM75A is a very easy to use and robust small I2C temperature device. It is able to measure temperature in the range of -55° to +125°. Even if working with an I2C bus, I was able to use it in a very noisy environment (Central Heating system) at distance of between 5 to 10 meters using a shielded cable (standard 4 wires alarm shielded cable) and adding a small 100nF cap. directly on the LM75A.



    Here, you can find the DATASHEET

    http://www.semiconductors.philips.co...ts/LM75A_1.pdf

    Example
    You will find 2 examples:

    Only integer values. No floating point calculation.
    First we read the data from the sensor (Sensor address H00 and sensor register address 0 for Temperature Data)

    As the LM75A is giving temp data on 11 bits, we have to suppress the last 5 bits (zeroes). To do this, we will shift right (>>)

    The sign is retrieved from the first (MSB) bit. If zero, the sign is positive. As we shifted the DATA, the sign bit is now Bit 10. To get a usable temperature data, the calculation is then: DATA*0.125, which is the same as DATA/8. Divide by 8 is in fact just shifting again 3 bits right. Here we are... No floating point math!

    Code:
    THERM= BUSIN %10010001,0
    THERM=THERM>>5                                             ' Suppress the last zeroes (>>5)
    SIGN=THERM.10                                                  ' Get the first MSB bit. Needed to interpret the sign
     
    IF SIGN=0 THEN
        THERM=THERM>>3                                         ' Divide by 8 for positive value (same as multiply by 0.125)
        PRINT AT 1,1,"Temp= "
    ELSE
        THERM=(%11111111111-THERM)>>3            'Adapt calculation for negative value (2's complement)
        PRINT AT 1,1,"Temp=-"
    ENDIF
     
    PRINT AT 1,7,DEC2 THERM,%11011111           ' And print 2 digits
    Floating point calculation.
    Here again, we read data. But we do not use shifting to divide the DATA. We use the multiplying factor suggested by the DATASHEET. We also use new Floating Point variable . Note that the readings will not be increasing or decreasing linearly as each 'step' is 0.125°

    Code:
    THERM= BUSIN %10010001,0
    THERM=THERM>>5                                                    'Suppress the last zeroes (>>5)
    SIGN=THERM.10                                                         ' Get the first bit. Needed to interpret the sign
     
    IF SIGN=0 THEN
        VAL_THERM=THERM*0.125                                'We multiply by 0.125 to get decimals
       PRINT AT 1,1,"Temp= "
    ELSE
        VAL_THERM=(%11111111111-THERM)*0.125  '  Adapt calculation for negative value (2's complement)
       PRINT AT 1,1,"Temp=-"
    ENDIF
     
    PRINT AT 1,7,DEC1 VAL_THERM

    Conclusion

    Those 2 examples show you also the cost of floating point math.

    The first one compiles (PROTON+ 2.1.3) in 455 Words and 21 Variables used in the 16F628 from a possible 224 The second one (PROTON+ 2.1.3) in 835 Words and 51 Variables used in the 16F628 from a possible 224 The difference does not need any comments...

    The program without floating point calculation

    Code:
    ' Using a LM75A
    ' No Floating Point
     
    ' Olivier de Broqueville
     
            Device 16F628
     
            REMARKS On
     
            REMINDERS OFF
            Config INTRC_OSC_NOCLKOUT,PWRTE_ON,CP_OFF,WDT_ON,LVP_OFF,MCLRE_OFF,BODEN_ON
            REMINDERS On
     
            ALL_DIGITAL On
     
            Declare XTAL=4
     
            Declare WATCHDOG On
     
            Declare LCD_INTERFACE 4
            Declare LCD_LINES 2
            Declare LCD_DTPIN PORTB.0
            Declare LCD_ENPIN PORTA.4
            Declare LCD_RSPIN PORTA.3
     
            Declare SDA_PIN PORTA.1
            Declare SCL_PIN PORTA.2
            Declare SLOW_BUS On
     
     
     
            Dim THERM As Word
            Dim SIGN As Bit
     
            DelayMS 100                                                               ' Let PICŪstabilize
     
            Clear
     
            Cls
    MAIN:
             While 1=1       ' Do forever...
                       THERM= BusIn %10010001,0
                       THERM=THERM>>5                                         ' Suppress the last zeroes (>>5) 
                       SIGN=THERM.10                                          ' Get the first bit. Needed to interpret the sign
     
                       If SIGN=0 Then
                          THERM=THERM>>3                              ' Divide by 8 for positive value (same as multiply by 0.125)
                              Print At 1,1,"Temp= "
                       Else
                              THERM=(%11111111111-THERM)>>3   'Adapt calculation for negative value (2's complement)
                              Print At 1,1,"Temp=-"
                       EndIf
     
                       Print At 1,7,DEC2 THERM,%11011111
     
                       DelayMS 200
             Wend
     
            End

    The program with floating point calculation


    Code:
    ' Using a LM75A
    ' Floating point
    ' Olivier de Broqueville
     
            Device 16F628
     
            REMARKS On
     
            REMINDERS OFF
            Config INTRC_OSC_NOCLKOUT,PWRTE_ON,CP_OFF,WDT_ON,LVP_OFF,MCLRE_OFF,BODEN_ON
            REMINDERS On
     
            ALL_DIGITAL On
     
            Declare XTAL=4
     
            Declare WATCHDOG On
     
            Declare LCD_INTERFACE 4
            Declare LCD_LINES 2
            Declare LCD_DTPIN PORTB.0
            Declare LCD_ENPIN PORTA.4
            Declare LCD_RSPIN PORTA.3
     
            Declare SDA_PIN PORTA.1
            Declare SCL_PIN PORTA.2
            Declare SLOW_BUS On
     
     
     
            Dim THERM As Word
            Dim SIGN As Bit
            Dim VAL_THERM As Float
     
            Clear
     
            Cls
    MAIN:
             While 1=1       ' Do forever...
                       THERM= BusIn %10010001,0
                       THERM=THERM>>5                                                       'Suppress the last zeroes (>>5)
                       SIGN=THERM.10                                                        ' Get the first bit. Needed to interpret the sign
     
                       If SIGN=0 Then
                              VAL_THERM=THERM*0.125
                              Print At 1,1,"Temp= "
                       Else
                              VAL_THERM=(%11111111111-THERM)*0.125 'Adapt calculation for negative value (2's complement)
                              Print At 1,1,"Temp=-"
                       EndIf
     
                       Print At 1,7,DEC1 VAL_THERM
     
                       DelayMS 200
             Wend
     
             End


  • Recent Activity

    See_Mos-247

    Mysterious PORTB problem

    Thread Starter: xldaedalus

    I'm using Proton+ to develop firmware for a product with switches. The MCU is an 18F26K22. Most of the switches reside on PORTB. I am NOT using a...

    See_Mos Today, 17:16 Go to last post
    joesaliba-353

    Watchdog timer

    Thread Starter: joesaliba

    I have a code that basically looks for four input and four outputs, depends on various timing and input conditions. I use interrupt and some delays...

    joesaliba Today, 12:10 Go to last post