Menu

ReadTemp Header File Redo

kent_twt4
2014-02-17
2014-08-16
  • kent_twt4

    kent_twt4 - 2014-02-17

    Time to redo the internal ReadTemp function. I will be tinkering with the old one to update the Dallas one-wire code for the ds18s20, ds18b20 and PAR (parasitic) devices. If anyone wants to add features, or test the example code, jump right in.

    I will edit the header file while going thru the process.

    O.K. here is an example using the DS18B20, I haven't tested the negative function in the freezer yet:

    '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
    
    '***************************************
    '    DS18B20 One Wire Temp
    '***************************************
    #include <NewReadtemp.h>
    #define ds18b20
    Dim DummyRead As Word
    #define DQ PortA.2
    '***************************************
    
    Main:       
    DummyRead = OWTemp
    cls
        If DummyRead.8 1 Then
            Print "-"
            DummyRead = DummyRead - 128  'Negative two's compliment reading
        End if
            'LCDInt DummyRead
        Print DummyRead
        If DummyRead.0 1 Then Print ".5"
        If DummyRead.0 0 Then Print ".0"
        Print " Deg C "
        locate 1,0
        Print "NewReadTemp.h"
    wait 1 s
    goto Main
    
     

    Last edit: kent_twt4 2014-02-17
    • kent_twt4

      kent_twt4 - 2014-02-17

      Header file named NewReadTemp:

      '=========ROM Commands for 1-Wire DS18S20 DS18B20======================================
      #define SearchRom 240       '0xF0 Identify Slave Rom codes
      #define ReadRom 51          '0x33 Command for single slave
      #define MatchRom 85         '0x55 To identify a specific slave code
      #define SkipRom 204         '0xCC Address all devices simultaneously
      #define AlarmSearch 236     '0xEC Search Rom to identify any alarm flags
      '==========Function Commands for for 1-Wire DS18S20 DS18B20=============================
      #define ConvertT 68         '0x44 Single Temp Conversion
      #define WriteScratch 78     '0x4E Write Scratchpad
      #define ReadScratch 190     '0xBE Read Scratchpad
      #define CopyScratch  72     '0x48 Copying Scratchpad TH and TL registers to EEPROM
      #define RecallE2 187        '0xB8 Recalling alarm trigger values from EEPROM 
      
      Dim HighLow As Word
      Dim OWTemp As Word
      
      function OWTemp
      MasterRST
      OWout SkipRom
      OWout ConvertT
      #IfDEF PARasitic
          Set Pullup On
      #EndIf
      wait 750 ms                 'Need approx. 750 ms for 12 bit conversion
      #IfDEF PARasitic
          Set Pullup Off
      #EndIf
      MasterRST
      OWout SkipRom
      OWout ReadScratch
      Owin
      #IfDEF ds18b20              'Assumes factory default 12bit resolution
          HighLow  = HighLow / 16 'Right shift temperature MSB into lower byte
      #Endif
      OWTemp = HighLow
      If HighLow.8 1 Then         'Test for sign bit
          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
          OWTemp = OWTempL        'Reassemble bytes back into word 
          OWTemp_H = OWTempH
      End if
      end function
      
      '-----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
      wait 48 10us                'wait for presence pulse from one-wire device 
      end sub
      
      Sub OWout (Command) #NR
      Clocks = 0
      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              '60 us per AN2420
          Dir DQ In               'HiZ the bus if still pulled low
          wait 3 us
          ROTATE Command Right    'The DS18s20 wants data LSB first
      Next
      end sub
      
      Sub OWin
      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 6 us  
          Dir DQ In               'Release bus for one wire Reception
          wait 4 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
      
       

      Last edit: kent_twt4 2014-02-17
  • Anobium

    Anobium - 2014-02-17

    Can we retain the existing header file and add the new functionality to the existing file?

    We should retain the existing function ReadTemp. You add a new function ReadTemp12 for the newer devices or add some parameters to control the method.

    Cheers.

     
  • kent_twt4

    kent_twt4 - 2014-02-17

    Yes, the existing function would be retained. The NewReadTemp is required till testing is complete; don't want to confuse myself or the compiler :). The new header saves several variables, and deletes an extra sub. More work required.

    I thought I would use 9bit as default. We could have other resolutions with optional #defines.

     
  • kent_twt4

    kent_twt4 - 2014-02-18

    O.K. here is a revised header file with some #defines. Should be backward compatible with the DS18s20.

    Will be working up an example on how to read the internal 8 byte of ROM (i.e. family code, 6 byte serial no., and CRC) from the ReadRom command. Getting the serial number allows a multidrop device scenario on a single data line.

    2/18/14 Refactored waits in Read Time Slot because of sketchy first reads depending on OSC. Freezer tested for negative numbers.

    '=========ROM Commands for 1-Wire DS18S20 DS18B20======================================
    #define SearchRom 240       '0xF0 Identify Slave Rom codes
    #define ReadRom 51          '0x33 Command for single slave
    #define MatchRom 85         '0x55 To identify a specific slave code
    #define SkipRom 204         '0xCC Address all devices simultaneously
    #define AlarmSearch 236     '0xEC Search Rom to identify any alarm flags
    '==========Function Commands for for 1-Wire DS18S20 DS18B20=============================
    #define ConvertT 68         '0x44 Single Temp Conversion
    #define WriteScratch 78     '0x4E Write Scratchpad
    #define ReadScratch 190     '0xBE Read Scratchpad
    #define CopyScratch  72     '0x48 Copying Scratchpad TH and TL registers to EEPROM
    #define RecallE2 187        '0xB8 Recalling alarm trigger values from EEPROM 
    
    Dim HighLow As Word
    Dim OWTemp As  Word
    
    function OWTemp
    MasterRST
    OWout SkipRom
    OWout ConvertT
    #IfDEF PAR
        Set ReadTemp_Pullup On
    #EndIf
    #IFNDEF PAR                 'start master read time slots until ds18x20 returns a "1"
    Do Until ConvertDone = True
        dir DQ out
        Set DQ Off
        wait 4 us
        Dir DQ In
        wait 7 us
        If DQ On Then
        ConvertDone = True
        Else
        ConvertDone = False
        End If
        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
    MasterRST
    OWout SkipRom
    OWout ReadScratch
    Owin
    If HighLow.15 1 Then        'Test for sign bit
        Call NegNumber
        ReadTemp_Neg = True
    Else
        ReadTemp_Neg = False
    End If
    #IFDEF ds18b20              'Assumes factory default 12bit resolution
        #IFDEF ReadTemp_8Bits
            HighLow  = HighLow / 16 'Right shift temperature MSB into lower byte
        #ENDIF
        #IFDEF ReadTemp_9Bits
            HighLow  = HighLow / 8
        #ENDIF
        #IFDEF ReadTemp_10Bits
            HighLow  = HighLow / 4
        #ENDIF
        #IFDEF ReadTemp_11Bits
            HighLow  = HighLow / 2
        #ENDIF
        '#IFDEF ReadTemp_12Bits (DS18b20 default) leaves HighLow unchanged
    
    #ENDIF
    #IFNDEF ds18b20
        #IFNDEF ReadTemp_9Bits
            HighLow  = HighLow / 2  'Right shift temperature MSB into lower byte
        #ENDIF
        'IFDEF ReadTemp_9Bits (DS18s20 default) leaves HighLow unchanged
    #ENDIF
    OWTemp = HighLow
    
    end function
    
    '-----SUBS-----------------------------------------
    sub NegNumber
        ReadTemp_Neg = True 
        #IFDEF PIC 
            comf  HighLow       'Invert Low byte bits in the Register
            incf  HighLow       'Add One to Low byte to turn into 2's compliment 
            comf  HighLow_H     'Invert High byte bits
        #ENDIF
        #IFDEF AVR 
            neg  HighLow        'Invert Low byte bits in the Register
            inc  HighLow        'Add One to Low byte to turn into 2's compliment 
            neg  HighLow_H      'Invert High byte bits
        #ENDIF
    end sub
    
    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
    wait 48 10us                'wait for presence pulse from one-wire device 
    end sub
    
    Sub OWout (IN Command)
    Clocks = 0
    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              '60 us per AN2420
        Dir DQ In               'HiZ the bus if still pulled low
        wait 3 us
        ROTATE Command Right    'The DS18s20 wants data LSB first
    Next
    end sub
    
    Sub OWin (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
    
     

    Last edit: kent_twt4 2014-02-19
    • kent_twt4

      kent_twt4 - 2014-02-19

      HOW TO READ DSx1820 INTERNAL ROM CODE

      Here is an example on how to read a bus powered single DS18b20 internal ROM code. Once individual ROM codes are learned, then a multi drop network can be effected on a single Data pin.

      'This is an example for reading out a single bus powered DS18b20's ROM code.
      'family code (1 byte, 0x28), serial number (6 bytes), CRC (1 byte)
      'A typical example: 40 40 5 152 3 0 0 194
      
      '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 ReadTemp_9Bits     'Want to read 1/2 degree resolution
      'make use of the ReadTemp_Neg (True) Flag for negative temperatures
      'Dim DummyRead As Word
      #define DQ PortA.2
      
      Main:
      cls
      MasterRST                     'start initialization process
      OWout ReadRom
      OWinRom
      wait 2 s
      goto Main
      
      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
        If EightBytes = 5 Then Locate 1,0
        Print RxData: Print "  "
        wait 1 s
      Next
      end sub
      
       
  • Anobium

    Anobium - 2014-02-19

    Cant wait to test at the weekend!

     
  • Anobium

    Anobium - 2014-02-19

    @kent_twt4: Questions.

    From my research of DS1820, DS18S20 and the DS18B20.

    Device Capability
    DS1820 Temp returned as a fixed 9 Bit resolution
    DS18S20 Temp returned as a 9 Bit resolution but 12 bit resolution can be calculated
    DS18B20 Temp returned as 9 to 12 Bit resolution configured by designer

    Will the new header support all three devices?

    This is table of the capabilities per device.

    See the attachment - gave up formatting the table!

    Will we be able perform all the operations above in the new header? (I had to create this table as I could not find this consolidated information on the web....I am sure it exists somewhere!). Did I miss any capability?

     

    Last edit: Anobium 2014-02-19
    • kent_twt4

      kent_twt4 - 2014-02-19

      The question always is how much functionality to bring to the table in a header, as opposed to showing the methods used to obtain a result? I am open to discussion.

      Another question I have is; does the ReadTemp, and the much larger category of Dallas 1-Wire devices deserve a separate header file (say 1-Wire.h or OneWire.h?), as opposed to a System.h? (or wherever ReadTemp is currently stashed).

      1. Supposedly the DS18S20 is meant to be a drop in replacement for the DS1820 per Maxim/Dallas DS18S20 DS18B20 comparison pdf. The exception being the increased resolution and the conversion time of the DS18S20 http://pdfserv.maximintegrated.com/en/an/AN4377.pdf I don't foresee any problems with the DS1820 device since the ReadTemp is polling the conversion time.

      2. I have not planned to use the DS18S20 in the increased resolution mode. Bloated maths, and not matching the accuracy of +/- 0.5C of the device to it's resolution thing for me.

      3. The Table looks good.

      4. Setting and reading alarms is easy enough to incorporate.

      5. I don't have any ready code for the SearchRom command. My thoughts are if you don't know what devices you have, or their location in the chain, you have got problems? Perhaps I could be persuaded though :).

      6. Instead of doing software bit resolution for the DS18B20, we could make those as hardware EEProm R1, R0 bit configuration register settings. This would make for MUCH quicker ConvertT times for bus powered devices. Put a timer on ConvertT and get around 600+ ms 12 bit conversion, while a 9 bit conversion is "supposed" to be just under 100 ms.

       
  • Anobium

    Anobium - 2014-02-19

    Good point regarding header and method. Can we provide read temp for 9 bit and 12 bit, set / read alarm and read unique serial #/read unique device? The rest can be derived anyway from these. I think you are pretty close to this anyway.
    I like the idea of a new OneWire.h as this will have non device specific methods and we should place all the DS18x20 methods in the old DS18S20.h file. So, to use you would need <onewire.h> and <ds18s20.h>

    Re your list.

    1. Agree re this point.
    2. We should do a 12 bit – readtemp12 sounds like a good name for the method. I have some maths I can share.
    3. I will update the table to show what is support and what people have to hand crank.
    4. Good.
    5. I think if you cracked the searchrom we would have something better than a few other compilers – please, please.
    6. If it works – then any valid approach is good.

      FYI: I just checked the current ds18s20.h it is your code dated Feb 12, 2007, Rev0.

    :-)

     
  • kent_twt4

    kent_twt4 - 2014-02-22

    Put up a Dallas 1-Wire temperature sensor network example in the Contributors section. The link: https://sourceforge.net/p/gcbasic/discussion/629990/thread/0d52cc35/#6609

    Not much usable from the header file, so it is self contained so to speak. Also it uses the manual ROM discovery method as shown in previous post.

    I really tried to use integer values, but having a heck of time with the syntax. I am not sure if a function can handle Integers?

     
  • kent_twt4

    kent_twt4 - 2014-03-05

    Ughhh....Finally got the SearchRom code. Had to sneak a peek at the Dallas appnote 187 to get things going in the right direction. I was able to get three in a row on my own and then a total roadblock.

    Anyway, how many devices should the header file be able to discover? 16 devices might fit in a smallish 4KByte flash, 256Byte Ram device, OR???

     
  • Anobium

    Anobium - 2014-08-11

    @kent. I have finally got to the DS18B20 testing.

    Do I need the <NewReadtemp.h> file to make the READROM work correctly?

    Anobium

     
    • kent_twt4

      kent_twt4 - 2014-08-16

      Evan,

      No need for the NewReadTemp.h include.  Perhaps the OWinRom routine would be of help though.  I'm pretty sure I put up an example in the contributor section.  The OWin bytes need to be printed out or saved to a buffer.  Here's a recap:

      cls
      MasterRST                     'start initialization process
      OWout ReadRom
      OWinRom
      wait 2 s
      goto Main

      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
        If EightBytes = 5 Then Locate 1,0
        Print RxData: Print "  "
        wait 1 s
      Next
      end sub

      On Monday, August 11, 2014 8:30 AM, Anobium evanvennn@users.sf.net wrote:

      @kent. I have finally got to the DS18B20 testing.
      Do I need the <NewReadtemp.h> file to make the READROM work correctly?
      Anobium


      ReadTemp Header File Redo


      Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/gcbasic/discussion/579125/
      To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

       

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.