DS18B20

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

DS18B20

Post by ohararp » Thu Apr 02, 2009 5:35 pm

Gents,

I am using David's OW and DS18B20 sample and module files in attempts to talk to http://www.ibuttonlink.com/tstring.aspx. This T-string has 15 DS18B20's at 1 meter spacing. The T-String is configured for parasitic power mode and is causing me some troubles.

Running the code below in ISIS with a 18F452 and the T-String shows the code to be working fine and identifies each of the sensors on the bus. However, when I am putting this into practice with the actual hardware I am having some issues. I am currently running a 1K pullup on the line and can only identify a single DS18B20 on the line (there should be 14 more and a DS2431). Any suggestions would be appreciated.

Code: Select all

// if device and clock are omitted, then the compiler defaults to 
// 18F452 @ 20MHz - they are just used here for clarity...
Device = 18F452
Clock = 20

// import modules...
Include "ow.bas"
Include "convert.bas"
Include "usart.bas"

// on find event handler...
Event OnFind()
   Dim Index As Byte
   USART.Write("FAMILY $", HexToStr(SearchFamily,2))
   USART.Write(" ($",HexToStr(SearchID(7),2),")")
   USART.Write(" ($")
   Index = 6
   Repeat
      USART.Write(HexToStr(SearchID(Index),2))
      Dec(Index)
   Until Index = 0
   USART.Write(")",13,10)
End Event
 
// working variables...
Dim DeviceCount As Byte   

// program start...
SetBaudrate(br9600)
SetPin(PORTC.0)
DeviceCount = Search(owSearchROM, OnFind)
USART.Write(DecToStr(DeviceCount), " device(s) found", 13, 10)
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Post by ohararp » Wed May 13, 2009 3:01 am

This may help others who are working with a T-String or multiple DS18B20's all on the same 2 lines and in parasitic mode

Code: Select all

// if device and clock are omitted, then the compiler defaults to 
// 18F452 @ 20MHz - they are just used here for clarity...
Device = 18F452
Clock = 10

//DEVICE FUSE CONFIG
'Config
'    OSC 	= HS,		// HS Oscillator
'    FCMEN	= OFF,		// Failsafe Clock Monitor Disabled
'    IESO	= OFF,		// Int/Ext Oscillator Switch Over Disabled
'    PWRT	= OFF,		// Power Up Timer Disabled
'    BOREN	= OFF,		// Brownout Reset Disabled
'    WDT     = OFF,		// Watchdog Timer Disabled
'    MCLRE	= On,		// MCLR Enabled
'    WDTPS   = 256,       // 15000 x 4mS  = 60 seconds
'    LVP     = OFF,      // Low_Voltage Programming
'    PBADEN  = OFF       // PORTB Digital        
    
// import modules...
Include "convert.bas"
Include "string.bas"
Include "SUART.bas"
Include "DS2431.bas"
Include "DS18B20.bas"
Include "OW.bas"
Include "math.bas"
Dim DataPin As PORTC.0

// working variables...
Dim DeviceCount As Byte   
Dim TMEM(144) As Byte

Dim TempA As ShortInt
Dim TempB As Word
Dim TempC As Float
Dim TempF As Float

Dim IDX_MAIN As Byte

// display ROM ID for search item...
Sub DisplayRomID()
   Const ArrayA(8) As Byte = (7,6,5,4,3,2,1,0)
   Dim IDX As Byte
   UART.Write("RomID  ($")
   
   For IDX = 0 To 7 
      UART.Write(HexToStr(RomID(ArrayA(IDX)),2),32)
   Next
   UART.Write(")",13,10)
End Sub

Sub CheckAvailable()
    If DS18B20.Available() = TRUE Then
        UART.Write(" ID Matches",13,10)            
    Else
        UART.Write(" ID Doesn't Match",13,10)    
    EndIf
End Sub

Sub DisplayMem()
   Dim IDX_A As Byte
   Dim IDX_B As Byte
   
   
   For IDX_A = 0 To 17
   UART.Write("Memory ($")
       For IDX_B = 0 To 7 
           UART.Write(HexToStr(TMEM(IDX_B+(IDX_A * 8) - 1),2),32)
       Next
       UART.Write(")",13,10)
   Next 
End Sub

Sub DisplayTemp()
    Dim BUFF1 As Byte
    Dim BUFF2 As LongWord
    BUFF1 = Length(DecToStr(TempB))
    BUFF2 = Pow(10,BUFF1)
    TempC = TempB / (BUFF2 + 0.0)
    TempC = TempA + TempC
    TempF = (TempC * 9 / 5) + 32
    UART.Write("TEMP",DecToStr(IDX_MAIN),32,DecToStr(TempA),46,DecToStr(TempB),32,FloatToStr(TempC),32,FloatToStr(TempF),32,13,10)
End Sub 

Sub LoadSensorROM()
    DS2431.Find()               //Find the EEPROM
    DS2431.ReadMemory(TMEM)     //Read 144 bytes from EPROM and Load into TMEM
End Sub

Sub LoadRomID(pSENSOR As Byte)  //Spec Sensor to Talk To 
    Dim OFFSET As Byte
    Dim BUFF(8) As Byte
    OFFSET = (pSENSOR + 1) * 8
    BUFF(7) = TMEM(OFFSET)
    BUFF(6) = TMEM(OFFSET + 1)
    BUFF(5) = TMEM(OFFSET + 2)
    BUFF(4) = TMEM(OFFSET + 3)
    BUFF(3) = TMEM(OFFSET + 4)
    BUFF(2) = TMEM(OFFSET + 5)
    BUFF(1) = TMEM(OFFSET + 6)
    BUFF(0) = $28
    RomID = BUFF   
End Sub

'Configure Software UART for Debugging
UART.SetTX(PORTB.7)
UART.SetRX(PORTB.6)
UART.SetBaudrate(sbr9600)
UART.SetMode(umTrue)
    
// program start...
High(PORTC.0)
Low(PORTC.1)
DelayMS(2000)


SetPin(DataPin)
    
While True
    DeviceCount = Search(owSearchROM)', OnFind)
    UART.Write(DecToStr(DeviceCount), " device(s) found", 13, 10)
    
    If DS2431.Find() = TRUE Then
        DisplayRomID()
        UART.Write(")",13,10)
        DS2431.ReadMemory(TMEM)     //Read 144 bytes from EPROM and Load into TMEM
    Else
        UART.Write("EEPROM Not Found",13,10) 
    EndIf

    DisplayMem()
    UART.Write(13,10)    
    
    For IDX_MAIN = 0 To 0
        LoadRomID(IDX_MAIN)
        DisplayRomID()
        
        CheckAvailable()
        
        DS18B20.Convert()
        
        High(PORTC.1)
        DelayMS(750)
        Low(PORTC.1)
        
        DS18B20.GetTemp(TempA, TempB)
        DisplayTemp()
    Next 
    UART.Write(13,10)      
       
Wend
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

Intellisenc
Registered User
Registered User
Posts: 30
Joined: Mon Nov 22, 2010 3:08 am

Post by Intellisenc » Mon Nov 22, 2010 3:46 am

Could you post your module DS2431.bas? I am working to understand the DS2438 and thought it might help.

User avatar
ohararp
Posts: 194
Joined: Tue Oct 03, 2006 11:29 pm
Location: Dayton, OH USA
Contact:

Post by ohararp » Sun May 01, 2011 8:34 pm

Code: Select all

{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2009 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 4/4/2009                                                       *
*  Version : 1.0                                                            *
*  Notes   :                                                                *
*          :                                                                *
*****************************************************************************
}
Module DS2431
// import modules...
Include "OW.bas"
Include "convert.bas"

// DS2431 ROM and function commands...
Const
   owWriteSP = $0F,
   owReadSP = $AA,
   owCopySP = $55,
   owReadMem = $F0,
   owResume = $A5,
   owODSkip = $3C,
   owODMatchROM = $69
   
Dim
   FSP(9) As Byte,                 // scratch pad
   FID(8) As Byte,                 // device ROM ID
   FOnSearch As TOnSearch          // search event handler

Public Dim
   SetPin As OW.SetPin,            // make OW.SetPin() available for this interface
   SearchRomID As OW.SearchID,     // search ROM ID
   SearchAbort As OW.SearchAbort,  // abort search flag
   RomID As FID                    // public device ROM ID 
{
****************************************************************************
* Name    : ReadSP (PRIVATE)                                               *
* Purpose : Read the scratch pad                                           *
****************************************************************************
} 
Function ReadSP() As Boolean
   OW.Reset
   OW.WriteByte(owMatchROM)
   OW.WriteArray(FID)
   OW.WriteByte(owReadSP)
   Result = OW.ReadArray(FSP)
End Function
{
****************************************************************************
* Name    : WriteSP (PRIVATE)                                              *
* Purpose : Write the scratch pad                                          *
****************************************************************************
} 
Sub WriteSP()
   OW.Reset
   OW.WriteByte(owMatchROM)
   OW.WriteArray(FID)
   OW.WriteByte(owWriteSP)
   OW.WriteByte(FSP(2))
   OW.WriteByte(FSP(3))
   OW.WriteByte(FSP(4))
End Sub
{
****************************************************************************
* Name    : Available                                                      *
* Purpose : Returns true if the current module device ID is connected to   *
*         : the bus                                                        *
****************************************************************************
} 
Public Function Available() As Boolean
   Result = ReadSP
End Function
{
****************************************************************************
* Name    : MatchID (OVERLOAD)                                             *
* Purpose : Returns true if the current module device ID matches pID       *
****************************************************************************
}   
Public Function MatchID(ByRef pID() As Byte) As Boolean
   Dim Index As Byte 
   Result = true
   Index = Bound(FID)
   While Index <= Bound(FID) And Result
      Result = FID(Index) = pID(Index)
      Dec(Index)
   Wend   
End Function
{
****************************************************************************
* Name    : MatchID (OVERLOAD)                                             *
* Purpose : Returns true if the current module device ID matches pID       *
****************************************************************************
}   
Public Function MatchID(ByRefConst pID() As Byte) As Boolean
   Dim Index As Byte  
   Result = true
   Index = Bound(FID)
   While Index <= Bound(FID) And Result
      Result = FID(Index) = pID(Index)
      Dec(Index)
   Wend   
End Function
{
****************************************************************************
* Name    : OnFindFirst (PRIVATE)                                          *
* Purpose : Find() event handler                                           *
****************************************************************************
}   
Event OnFindFirst()
   If OW.SearchFamily = $2D Then
      OW.SearchAbort = true
      FID = OW.SearchID
   EndIf
End Event
{
****************************************************************************
* Name    : Find                                                           *
* Purpose : Returns true if a DS2431 device is found, false otherwise. If  *
*         : found, the module ID holds the device ROM code that has been   *
*         : located                                                        *
****************************************************************************
}   
Public Function Find() As Boolean
   OW.Search(owSearchROM,OnFindFirst)
   Result = OW.SearchAbort
End Function
{
****************************************************************************
* Name    : OnFindAll (PRIVATE)                                            *
* Purpose : FindAll() event handler                                        *
****************************************************************************
}   
Event OnFindAll()
   If OW.SearchID(0) <> $2D Then
      OW.SearchIgnore = true
   Else
      FOnSearch()
   EndIf
End Event
{
****************************************************************************
* Name    : FindAll                                                        *
* Purpose : Find all DS2431 devices connected to the bus                   *
****************************************************************************
}   
Public Function FindAll(pOnSearch As TOnSearch) As Byte
   FOnSearch = pOnSearch
   Result = OW.Search(owSearchROM,OnFindAll)
End Function
{
****************************************************************************
* Name    : Count                                                          *
* Purpose : Returns the number of DS2431 devices connected to the bus      *
****************************************************************************
}   
Public Function Count() As Byte
   FOnSearch = 0
   Result = OW.Search(owSearchROM,OnFindAll)
End Function

{
****************************************************************************
* Name    : ReadMemory
* Purpose : Read the Entire Contents of the EEPROM
****************************************************************************
}   
Public Sub ReadMemory(ByRef pMemory() As Byte)
   Dim IDX As Byte

   OW.Reset
   OW.WriteByte(owMatchROM)
   OW.WriteArray(FID)
   OW.WriteByte(owReadMem)
   OW.WriteByte($00)
   OW.WriteByte($00)
   
   For IDX = 0 To 143
        pMemory(IDX) = OW.ReadByte()
   Next 
End Sub
Thanks Ryan
$25 SMT Stencils!!!
www.ohararp.com/Stencils.html

Post Reply