SourceForge has been redesigned. Learn more.
Close

Dallas DS18s20 One Wire Code

2007-02-18
2013-05-30
  • Nobody/Anonymous

    Heres some code to get your Dallas One Wire stuff going. Enjoy.

    Kent

    '================================================================================
    ' This is a Dallas DS18s20 temperature device device program for Great Cow Basic. '
    ' This is the minimal code to get a positive temperature reading in degrees       '
    ' Celcius.  Has not been tested in Parasitic mode or with long lengths of wire.   '
    '                                                                                 '
    ' Kent Schafer Feb 12, 2007                                                       '
    ' Rev0                                                                            '
    '=================================================================================

    'Chip model
    #chip 16f648a,20
    #mem 256
    #config     _CP_OFF & _DATA_CP_OFF  &_WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _LVP_OFF

    '=========ROM Commands for 1-Wire DS18S20======================================
    #define SearchRom 240 '0xF0        '(240)Command for identifying Slave Rom codes,
                                    ' use as many times as needed
    #define ReadRom 51 '0x33        '(051)Command for single slave
    #define    MatchRom 85 '0x55        '(085)Command for master to identify a specific
                                    ' slave code
    #define    SkipRom 204 '0xCC        '(204)Command for addressing all devices
                                    ' simultaneously
    #define    AlarmSearch 236 '0xEC    '(236)Command is same as Search Rom to identify
                                    ' any alarm flags
    '==========Function Commands for for 1-Wire DS18S20=============================
    #define    ConvertT 68 '0x44        '(68)Protocol for Single Temp Conversion
    #define    WriteScratch 78 '0x4E    '(78)Protocol for Write Scratchpad
    #define    ReadScratch 190 '0xBE    '(190)Protocol for Read Scratchpad
    #define CopyScratch  72 '0x48    '(72)Protocol for copying Scrtchpad TH and TL
                                    ' registers to EEPROM
    #define RecallE2 187 '0xB8        '(187)Protocol for recalling alarm trigger values
                                    ' from EEPROM
    '---------------------------------------------------

    #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 PORTA.2
    #define LCD_RW PORTA.3
    #define LCD_Enable PORTA.1
    #define DQ PortA.0

    dir PortA.1 out
    dir PortA.2 out
    dir PortA.3 out
    'Dim RxData As Word (will need this for neg. no.)

    Main:
    wait 1 sec
    MasterRST
    PPulse
    wait 1 ms
    Command = SkipRom
    OWout
    wait 1 ms
    Command = ConvertT
    OWout
    wait 1 s  'Need at least 1/2 sec for conversion?

    MasterRST
    PPulse
    wait 1 ms
    Command = SkipRom
    OWout
    wait 1 ms
    Command = ReadScratch
    OWout
    wait 1 ms
    Owin
    wait 1 ms

    cls
    LCDInt (RxData/2)
    If RxData.0 1 Then Print ".5"
    If RxData.0 0 Then Print ".0"
    Print " Deg C"
    Wait 5 sec
    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
    end sub

    Sub PPulse  'Master receive DS18s20 presence pulse
      wait 70 us
      'If DQ 0 Then
      'cls
      'Print "OW Device ACK" 
      'End if
      wait 43 10us
      Dir DQ In
    end sub

    Sub OWout
    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
    For Clocks = 1 to 8
      Rotate RxData Right 'The DS18s20 xmits data LSB first
      Dir DQ Out
      Set DQ Off  'Read time slot
      wait 6 us 
      Dir DQ In   'Release bus for one wire Reception
      wait 4 us 
      If DQ On  Then Set RxData.7 1
      If DQ Off Then Set RxData.7 0
      wait 50 us
    Next
    end sub

     
  • kent_twt4

    kent_twt4 - 2011-09-14

    Darn it, it's the spam bot again, so have to close.  Refer questions to help forum.

     

Log in to post a comment.