| Posted: 11:32am 21 May 2026 |
Copy link to clipboard |
 Print this post |
|
As Jim's test program seems to work, further down the thread linked to by @dddns there is a more compact Function based on Jim's program.
Function DS18x20(PinNbr As INTEGER) As FLOAT ' Extracted from DS18x20 test code by TassyJim ' Run that program and find the values that get your device to work. ' Copy those values into this Function Local INTEGER t, T1, T2, done Local FLOAT Value=0
OneWire RESET PinNbr ' reset before command OneWire WRITE PinNbr, 8, 2, &hcc, &h44 ' start conversion
t = Timer Do 'There should be a result in less than 1000mS If Timer - t > 1000 Then Value = 1000 'standard error code Print "*** Timeout ***", Exit Do EndIf
OneWire READ PinNbr, 4 , 1 , done ' conversion done? Loop Until done ' Print Timer - t;" mS conversion time", 'un-comment this to see how long it took If Value = 0 Then ' we have not timed out yet, so assume data is valid OneWire WRITE PinNbr, 1, 2, &hcc, &hbe ' command read data OneWire READ PinNbr, 2, 2, T1, T2 ' get the data
OneWire RESET PinNbr If T2 And &b1000 Then 'negative temp 'make 2s complement (1s complement+1) T2 = (T2 Xor &hFF) T1 = (T1 Xor &hFF)+1
If T1=1 Then T2=T2+1 'add the carry if required Value = -((T2 And &b111) * 256 + T1) / 16 'adjust this if the temp. is wrong Else 'positive temp Value = ((T2 And &b111) * 256 + T1) / 16 'adjust this if the temp. is wrong EndIf EndIf DS18x20 = Value End Function Edited 2026-05-21 21:36 by phil99 |