iandaus
Newbie
Joined: 02/03/2012 Location: AustraliaPosts: 3 |
Posted: 11:33pm 02 Mar 2012 |
Copy link to clipboard |
Print this post |
|
Mookster1
I too have been tinkering with the DS18x20 devices and they seem to work fine with
the brilliant little MM and the OneWire functions included in MMBasic 3.1.
The ONEWIRE.BAS program in the library works provided you are using external
power and using a DS18B20 and the temperature is above zero. So it sounds like
it should work for your setup. Below zero I think the calculation for negative
temps may be incorrect. The following program is based on ONE-WIRE.BAS and seems to
work for me with external or parasite power on various forms of the DS18x20.
Regarding your issue, all I can come up with:
Are you sure you have a DS18B20?
If you have amended ONE-WIRE.BAS, have you issued a Reset before the Conversion command? (Happened to me!)
Perhaps the code below will be of some use. Note that you can have only one sensor
on the cable. By noting the ROM Codes reported by the program you can then
also use multiple sensors with some changes by passing the ROM Codes to the subroutine
and using MatchROM commands instead of the SkipROMs (cunningly avoiding the need for a
SearchROM routine).
Cheers
Ian
=========================================================
'TEMPALL.BAS March 2012
PinNbr = 20
GetTemp PinNbr, Temp, power$, device$, code$
Print "The temperature is:" format$(Temp,"% 1.2f"); " degrees C"
print device$;code$;power$
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Subroutine to get the temperature from a Dallas DS18B20/DS18S22
' or DSS20/DS1820 externally or parasite powered.
' Reports temperature, device family, ROM Code and Power connection.
' Only 1 device allowed on the cable.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetTemp (PinNbr, Value,power$,device$, code$)
Local T1, T2, t, a1,a2,a3,a4,a5,a6,a7,a8, device, presence, power
Local Countrem, T1C, TConv, T1t, a,b,c,d,e,f,g,h
Tconv=750 'max time mS for temp conversion in parasite mode
OWReset PinNbr,presence ' reset
if presence = 0 then ' no device
temp = 999.99
device$ = "No device found!"
code$ = ""
power$ = ""
exit sub
endif
'check whether power ext/parasitic
OWReset PinNbr,presence
OWWrite PinNbr, 1,2,&hcc, &hb4
OWRead PinNbr,4,1,power
if power = 1 then
power$ = " using External Power."
else
power$ = " using Parasitic Power."
endif
'get ROM Code
OWWrite PinNbr,1,1,&h33 'read ROM code
OWRead PinNbr,0,8,a1,a2,a3,a4,a5,a6,a7,a8
code$ = " with ROM Code "+ str$(a1)+" "+str$(a2)+" "+str$(a3)+" "+str$(a4)+" "
code$ = code$ + str$(a5)+" "+str$(a6)+" "+str$(a7)+" "+str$(a8)
'determine device family
if a1=16 then
device$ = "on DS1820/DS18S20"
elseif a1=34 then
device$ = "on DS18S22"
elseif a1=40 then
device$ = "on DS18B20"
else
device$ = "Not known"
endif
owreset PinNbr ' reset before command (or use flag=9)
OWWrite PinNbr, 8, 2, &hcc, &h44 ' start conversion
'read external when bit goes hi, for parasitic just wait
If power = 0 Then
Pause Tconv
Else
t = Timer
Do
If Timer - t > 1000 Then Error "Sensor not responding"
OWRead PinNbr, 4 , 1 , b ' conversion done?
Loop Until b = 1
EndIf
'read temperature from scratchpad and convert to degrees C + or -
OWWrite PinNbr, 1, 2, &hcc, &hbe ' command read data
OWRead PinNbr, 2, 2, T1, T2 ' get the data
OWReset PinNbr
'Need to analyse type of chip to calculate temp from T1 and T2
'T2 is MSB containing sign, T1 is LSB
if a1=34 or a1=40 then 'DS18S22 or DS18B20 is 12 bit
If T2 And &b1000 Then 'negative temp
'make 2s complement (1s complement+1)
T2 = (T2 xor &b11111111)
T1 = (T1 xor &b11111111)+1
if T1=1 then T2=T2+1 'add the carry if required
Value = ((T2 And &b111) * 256 + T1) / 16
Value = -Value
else 'positive temp
Value = ((T2 And &b111) * 256 + T1) / 16
endif
elseif a1=16 then 'DS18S20 or DS1820 is 9bit or calc to 12bit
if T2 AND &b10000000 then 'if MSB of T2=1 then negative
'Read 12bit resolution and adjust
'read scratchpad using matchrom to get Count Remaining @byte 7
OWWrite PinNbr,1,9,&h55,a1,a2,a3,a4,a5,a6,a7,a8 'read from scratchpad
OWWrite PinNbr,0,1,&hbe
OWRead PinNbr,0,8,a,b,c,d,e,f,g,h
COUNTREM=g
'truncate 0.5deg value
T1t = T1 AND &b11111110
T1t=T1t / 2 'make whole degrees
'add compensation values read from scratchpad
T1t16=T1t*16 'make lsb 1/16 degree
Value12 = T1t16 -4 +(16 - COUNTREM) 'add 12 bit value
'take 2s complement
T1C = (Value12 XOR &b11111111111) + 1
Value = T1C/16 'make decimal value in degrees
Value = -Value
else 'positive temp
Value = T1 / 2 '9bit value
'Read 12bit resolution and adjust
'read scratchpad using matchrom
OWWrite PinNbr,1,9,&h55,a1,a2,a3,a4,a5,a6,a7,a8 'read from scratchpad
OWWrite PinNbr,0,1,&hbe
OWRead PinNbr,0,8,a,b,c,d,e,f,g,h
COUNTREM=g
T1C = T1 AND &b11111110 'truncate 0.5deg
Value = t1C/2
Value = Value - 0.25 + (16 - COUNTREM) /16 '12 bit value
endif
endif
End Sub
|