• PicŪ Basic


  • Simple but accurate DS18S20 Interface

    I know there are lots of sample programs for the DS18S20 device on the forum, but most of them produce values that can only be displayed and not actually a full temperature (including the .5 remainder) that can be compared etc... The below program also handles negative temperatures readily.

    The program is fully compatible with standard 14-bit core devices, enhanced 14-bit core devices, 18F devices and PIC24 and dsPIC devices.

    Hopefully it is useful to some users in this winter time.

    Code:
    ' Read a DS18S20 1-wire temperature sensor and display the temperature on the serial terminal
    '
    ' Circuit diagram
    '
    ' VCC 3.3 Volts or 5 Volts
    ' ------------o----
    '             |   |
    '             |  .-.
    '             |  | |
    '             |  | | 4.7K
    '             |  '-'
    '       ____  |   |
    '      / VCC|--   |
    '      | DQ |-----o--> To PORTB.0
    '      \ GND|--
    '       ----  |
    '  DS18S20    |
    '            --- Gnd
    '        
        Include "Amicus18.inc"                          ' Configure the compiler to use a 18F25K20 at 64MHz. i.e the Amicus18 board
    '
    ' Allocate variables
    '
        Dim wRaw_Temperature As SWord                   ' Holds the raw temperature value from the DS18S20
        Dim fTemperature As Float                       ' Holds the final temperature
    
        Symbol DQ = PORTB.0                             ' One-wire data pin
    
    '-------------------------------------------------------------------------------------
        GoTo Main                                       ' Jump over the subroutine to the main code
    '-------------------------------------------------------------------------------------
    ' Read the DS18S20 chip
    ' Input     : None
    ' Output    : fTemperature holds the temperature value
    ' Notes     : None
    '    
    $define Get_Temperature() GoSub _GetTemperature
    
    
    _GetTemperature:
        OWrite DQ, 1, [$CC, $44]                        ' Start a temperature conversion
        While ORead DQ, 4 = 0                           ' \
            DelayMS 10                                  ' | Check for still busy converting
        Wend                                            ' /
        OWrite DQ, 1, [$CC, $BE]                        ' Setup to read the temperature
        ORead DQ, 0, [wRaw_Temperature.Byte0, wRaw_Temperature.Byte1] ' Read the temperature
    
    
        fTemperature = wRaw_Temperature / 2.0           ' Convert the temperature to a float
        If fTemperature < 0 Then                        ' Is the temperature below 0?
            fTemperature = fTemperature + 0.5           ' Yes. So adjust the value returned
        EndIf
        Return
    
    '-------------------------------------------------------------------------------------
    ' The main program starts here
    '
    Main:    
        While                                           ' Create an infinite loop
            Get_Temperature()                           ' Get the temperature from the chip
            HRSOut Dec1 fTemperature, " Degrees C\r"    ' Transmit the temperature serially
        Wend                                            ' Do it forever
    This article was originally published in forum thread: Simple but accurate DS18S20 Interface started by Les View original post
  • Recent Activity

    towlerg-21522

    PIC18F1330 PLL problem

    Thread Starter: rcurl

    I'm just starting out on a new project that uses a PIC18F1330. I haven't used this specific chip before, so I began by trying to blink an LED just...

    towlerg Yesterday, 16:26 Go to last post
    towlerg-21522

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    towlerg Yesterday, 16:25 Go to last post
    palamont-28794

    MID$ Problem with a PIC18F25K22

    Thread Starter: palamont

    Hi everybody, I encounter a strange problem : I have a string of 230 characters. This string is divided into 10 segments of 23 characters, with a...

    palamont Yesterday, 10:08 Go to last post