CMM2: DS18B20 Temp sensor problem
| Author | Message | ||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3379 |
For anyone with a DS18x20 device that doesn't work with Tempr() but does give sensible results with Jim's test program here is a Function derived from Jim's program. Usage:- Print DS18x20(pin); "`C" Or Temp = DS18x20(mm.info(pinno GPxx)) : Print Temp; "`C" There are some comments to help adapt it to your device. 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 Footnote added 2026-05-25 19:07 by phil99 There is a version of the above function specifically for the DS18S20 (Picomite) here |
||||