B4R Question OneWire Library Always output 'No more addresses.'

santook

Member
I try to use the OneWire library to read the value of the DS18B20 sensor, and in full accordance with the example, why always output 'No more addresses.'


#Region Project Attributes
#AutoFlushLogs: True
#CheckArrayBounds: True
#StackBufferSize: 300
#End Region

Sub Process_Globals
Public Serial1 As Serial
Private onewire As OneWire
Private timer1 As Timer
Private bc As ByteConverter
Private address(8) As Byte
Private type_s As Boolean
End Sub

Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
onewire.Initialize(3)
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(address, 7) <> address(7) Then
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(0x44, True)
'give it 1 second to read the temperature
CallSubPlus("ReadTemparature", 1000, 0)
End Sub

Private Sub ReadTemparature (u As Byte)
onewire.Reset
onewire.Select(address)
onewire.Write(0xBE, False)
Dim data(12) As 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, 0, 2), " Celsius, " _
, NumberFormat(fahrenheit, 0, 2), " Fahrenheit")
End Sub
 

Cableguy

Expert
Licensed User
Longtime User
I placed the 4.7K resistor on the DQ port.
Now I can read the temperature, but the output is not very stable.

How so? How Big is the variation in the read values? You must keep in mind the sensor's accuracy, which will have a big impact in the readings.
Also I have found that PSU fluctuations also play a major role in the overall behaviour of the sketch.
Try different values for the pull up resistor...
 
Upvote 0

santook

Member
In Proteus simulation, the same HEX file can't operate correctly with 16MHz crystal oscillator. It needs to modify MCU to 8MHz crystal oscillator, and reduce the baud rate by half to find DS18B20 accurately.
 
Upvote 0
Top