B4R Library OneWire - Dallas 1-wire protocol

Discussion in 'B4R Libraries' started by Erel, May 8, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This library is based on the following open source project: http://www.pjrc.com/teensy/td_libs_OneWire.html

    License: https://github.com/PaulStoffregen/OneWire/blob/master/OneWire.cpp

    It implements Dallas 1-wire protocol.

    Example of reading the temperature:
    Depends on rOneWire and rRandomAccessFile libraries.
    Code:
    Sub Process_Globals
       
    Public Serial1 As Serial
       
    Private onewire As OneWire
       
    Private timer1 As Timer
       
    Private bc As ByteConverter
       
    Private address(8As Byte
       
    Private type_s As Boolean
    End Sub

    Private Sub AppStart
       Serial1.Initialize(
    115200)
       
    Log("AppStart")
       onewire.Initialize(
    10)
       timer1.Initialize(
    "timer1_Tick"2000)
       timer1.Enabled = 
    True
    End Sub

    Sub Timer1_Tick
       
    If onewire.Search(address) = False Then
         onewire.ResetSearch
         
    Log("No more addresses.")
         
    Return
       
    End If
       
    Log("ROM = ", bc.HexFromBytes(address))
       
    If onewire.CRC8(address7) <> address(7Then
         
    Log("CRC is not valid!")
         
    Return
       
    End If
       
    Select address(0)
         
    Case 0x10
           
    Log("Chip = DS18S20")
           type_s = 
    True
         
    Case 0x28
           
    Log("Chip = DS18B20")
           type_s = 
    False
         
    Case 0x22
           
    Log("Chip = DS1822")
           type_s = 
    False
         
    Case Else
           
    Log("Device is not a DS18x20 family device.")
           
    Return
       
    End Select
       onewire.Reset
       onewire.Select(
    address)
       onewire.Write(
    0x44True)
       
    'give it 1 second to read the temperature
       CallSubPlus("ReadTemparature"10000)
    End Sub

    Private Sub ReadTemparature (u As Byte)
       onewire.Reset
       onewire.Select(
    address)
       onewire.Write(
    0xBEFalse)
       
    Dim data(12As Byte
       onewire.ReadBytes(data, 
    9)
       
    Log("Data = ", bc.HexFromBytes(data))
       
    Log("CRC = ", bc.HexFromBytes(Array As Byte(onewire.CRC8(data, 8))))
       
    Dim raw As Int = Bit.Or(Bit.ShiftLeft(data(1), 8), data(0))
       
    If type_s Then
         raw = 
    Bit.ShiftLeft(raw, 3)
         
    If data(7) = 0x10 Then
           raw = 
    Bit.And(raw, 0xFFF0) + 12 - data(6)
         
    End If
       
    Else
         
    Dim cfg As Byte = Bit.And(data(4), 0x60)
         
    If cfg = 0 Then
           raw = 
    Bit.And(raw, Bit.Not(7))
         
    Else if cfg = 0x20 Then
           raw = 
    Bit.And(raw, Bit.Not(3))
         
    Else if cfg = 0x40 Then
           
    Bit.And(raw, Bit.Not(1))
         
    End If
       
    End If
       
    Dim celsius As Double = raw / 16
       
    Dim fahrenheit As Double = celsius * 1.8 + 32
       
    Log("Temperature = "NumberFormat(celsius, 02), " Celsius, " _
         , 
    NumberFormat(fahrenheit, 02), " Fahrenheit")
    End Sub
    Note that unlike the original C example, the main thread is not blocked here while waiting for the data to be ready.
     

    Attached Files:

    Mostez, GMan, raphaelcno and 2 others like this.
  2. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

    just to confirm the above code successfully tested. Log:
    Code:
    AppStart
    ROM = 28FF5E1804150334
    Chip = DS18B20
    Data = 780155003FFF3F10CF051000
    CRC = CF
    Temperature = 
    23.50 Celsius, 74.30 Fahrenheit
    No more addresses.
    ...
     
    Erel likes this.
  3. rbghongade

    rbghongade Active Member Licensed User

    Confirmed! Works great . Tested in simulation environment though!
     

    Attached Files:

    jinyistudio likes this.
  4. BaGRoS

    BaGRoS Member Licensed User

    What software did you use for the simulation?
     
  5. pappicio

    pappicio Active Member Licensed User

    Proteus, with arduino library addins
     
  6. BaGRoS

    BaGRoS Member Licensed User

Loading...