Menu

Dallas 1-Wire Temperature Sensor Network

kent_twt4
2014-02-22
2014-02-26
  • kent_twt4

    kent_twt4 - 2014-02-22

    Here is a method to development a Dallas 1-Wire, single bus powered, temperature sensor network. The MatchRom command is required to address the individual sensors in the network. Here is the link on how to readout individual 64 bit ROM codes: https://sourceforge.net/p/gcbasic/discussion/579125/thread/8fb0ca75/#6044/35f9

    The example should be expandable to other 1-Wire devices. Using the family device code, as in the example, identifies the handling of the two DS18B20 devices, from the single DS18S20 device.

    Edit: Put back "end sub" of OWoutRom subroutine which was left out during initial cut and paste. GCBasic had still managed to put a "Return" in without it during compile.

    'This is an example for reading out three networked Dallas 1-wire Temp Devices.
    'The network is using bus power and include (2) DS18B20, and (1) DS18S20.
    'The 64 bit ROM codes have been manually read, and tables developed to use MATCHROM command.
    'KS 2/22/2014
    
    'Chip model, Oscillator frequency FOSC
    #chip 18f26k22,8
    #config OSC=INTIO67
    
    'Setup 4 bit LCD
    #define LCD_IO 4
    #define LCD_DB4 PORTB.4
    #define LCD_DB5 PORTB.5
    #define LCD_DB6 PORTB.6
    #define LCD_DB7 PORTB.7
    #define LCD_RS PORTC.5
    #define LCD_RW PORTC.6
    #define LCD_Enable PORTC.7
      '#include <NewReadtemp.h>
      '#define ds18b20
      '#define OWDevice ds18b20
      '#define ReadTemp_9Bits       'Want to read 1/2 degree resolution
    'make use of the ReadTemp_Neg (True) Flag for negative temperatures
    Dim DummyRead As Word
    Dim HighLow As Word
    Dim OWTempNet As word
    #define DQ PortA.2
    #define SkipRom 204        '0xCC (204) Command for addressing all devices simultaneously
    #define MatchRom 85        '0x55 '(085)Command for master to identify a specific slave code
    'Function Commands for for 1-Wire DS18S20
    #define ConvertT 68      '0x44 (68) Protocol for Single Temp Conversion
    #define ReadScratch 190  '0xBE (190) Protocol for Read Scratchpad
    
    Main:
    For count = 1 to 3
      DummyRead = OWTempNet
      If count = 1 Then Locate 0,0
      If count = 2 Then locate 0,8
      If count = 3 Then Locate 1,0
      If ReadTemp_Neg = True & DummyRead <> 0 Then
        Print "-"
      Else
        Print " "
      End if
      If (DummyRead / 2) < 100 Then Print " "
      Print (DummyRead / 2)  'Print whole number from 9 bit format
      If DummyRead.0 = 1 Then
        Print ".5 "
      Else
        Print ".0 "
      End If
      Print "C "
    Next
    'wait 2 s
    goto Main
    
    '-----SUBS-----------------------------------------
    
    Sub MasterRST 'Master reset low for minimum 480 us
      Dir DQ In
      Dir DQ Out
      Set DQ Off
      wait 50 10us
      Dir DQ In    'HiZ the bus line for reading presence pulse
    end sub
    
    Sub PPulse    'Master receive presence pulse
      wait 70 us
      Dir DQ In
      wait 430 us
    end sub
    
    function OWTempNet
      MasterRST
      PPulse
      OWout MatchRom
      OWoutRom
      OWout ConvertT
      MasterRead_TimeSlots        'start polling for response from device
      MasterRST
      PPulse
      OWout MatchRom
      OWoutRom
      OWout ReadScratch
      OWin16                      'read first two bytes from scratch pad
    
      If HighLow.15 = 1 Then      'Test for sign bit
        ReadTemp_Neg = True
        If DevFamily = 40 Then
          HighLow = HighLow / 8
        end if
        OWTempL = HighLow         'Split word into bytes so byte size
        OWTempH = HighLow_H       'assembler operations can be used
        comf  OWTempL       'Invert Low byte bits in the Register
        incf  OWTempL       'Add One to Low byte to turn into 2's compliment
        comf  OWTempH       'Invert High byte bits
        OWTempNet = OWTempL     'Reassemble bytes back into word
        OWTempNet_H = OWTempH
        goto NegNum
      Else
        ReadTemp_Neg = False
      End if
    
      If DevFamily = 40 Then      'Check for DS18B20 Devices
         OWTempNet = HighLow / 8  'Make a 9 bit result
      Else
         OWTempNet = HighLow      'Its a DS1820 or DS18S20 device
      End if
    NegNum:
    end function
    
    sub MasterRead_TimeSlots
      #IfDEF PAR
             Set ReadTemp_Pullup On
      #EndIf
      #IFNDEF PAR   'start master read time slots until ds18x20 returns a "1"
      Do
          dir DQ out
          Set DQ Off
          wait 4 us
          Dir DQ In
          wait 7 us
          If DQ = 1 Then Exit Do
          wait 60 us    'minimum 60 us time slots between reads 
      Loop
      #ENDIF
      #IfDEF PAR
             wait 750 ms    'Need approx. 750 ms for full conversion
             Set ReadTemp_Pullup Off
      #EndIf
    end sub
    
    Sub OWin16 (OUT HighLow as Word)
    HighLow = 0
    For bits = 1 to 16
      Rotate HighLow Right  'The one wire devices xmit data LSB first
      Dir DQ Out
      Set DQ Off                'Start Read time slot
      wait 4 us
      Dir DQ In             'Release bus for one wire Reception
      wait 7 us
      If DQ On  Then Set HighLow.15 1  'Must read bus within 15us slot
      If DQ Off Then Set HighLow.15 0
      wait 50 us
    Next
    end sub
    
    Sub OWoutRom
    For EightBytes = 1 to 8
      If count = 1 Then ReadTable SensorOne,EightBytes, SerialNum
      If count = 2 Then ReadTable SensorTwo,EightBytes, SerialNum
      If count = 3 Then ReadTable SensorThree,EightBytes, SerialNum
    
      If EightBytes = 1 Then
        DevFamily = SerialNum
        'Print DevFamily
        'Print " "
      End if
      OWout SerialNum
    Next
    end sub
    
    Sub OWout (Command) #NR
     For Clocks = 1 to 8
      Dir DQ Out
      Set DQ Off
      wait 3 us             'Need to release bus within 15 us
      If Command.0 On then
      Dir DQ In
      End if
      wait 60 us
      Dir DQ In             'HiZ the bus if still pulled low
      wait 3 us
      ROTATE Command Right  'The one wire devices xmit data LSB first
     Next
    end sub
    
    Sub OWinRom
    For EightBytes = 1 to 8
     For Clocks = 1 to 8
      Rotate RxData Right 'The DS18s20 wants data LSB first
      Dir DQ Out
      Set DQ Off  'Read time slot
      wait 4 us
      Dir DQ In   'Release bus for one wire transmission
      wait 7 us
      If DQ On  Then Set RxData.7 1
      If DQ Off Then Set RxData.7 0
      wait 50 us
     Next
    Next
    end sub
    
    Table SensorOne
    16
    37
    128
    228
    0
    8
    0
    92
    End Table
    
    Table SensorTwo
    40
    248
    47
    97
    3
    0
    0
    161
    End Table
    
    Table SensorThree
    40
    40
    5
    152
    3
    0
    0
    194
    End Table
    
     

    Last edit: kent_twt4 2014-02-26
  • kent_twt4

    kent_twt4 - 2014-02-22

    Here's a pic, two sensors in the freezer...brrrr.

     
  • ofuzzy1

    ofuzzy1 - 2014-02-26

    Nice work!

    I see a nRF24L01 have you gotten that to work yet?
    would you like a pcb that works with that and a few other parts?
    I sent some to Hugh and Evan. PM me if you want a 8/14/18/20 pin + LCD board
    or a soic-28 + usb board

     

    Last edit: ofuzzy1 2014-02-26
    • kent_twt4

      kent_twt4 - 2014-02-26

      Thanks, hope someone finds it useful.

      For a brief period of time, got a single channel RX/TX NRF24L01P pair working. Sadly when I tried to resurrect the project last year, it had seemed communication was lost ... :(. I had updated GCBasic during this time, tried going back to previous version, still no go. Was running these off a 9V battery and a wall wart, and that could have been the problem too.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.