Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 15:27 25 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : One-wire protocol

Author Message
mookster1
Regular Member

Joined: 10/06/2011
Location: New Zealand
Posts: 81
Posted: 08:46pm 29 Feb 2012
Copy link to clipboard 
Print this post

Hi all,

Recently I've been experimenting with the one-wire protocol for the Maximite to get it to talk to a DS18B20 temperature sensor. I then discovered the "one-wire.bas" program in the latest program library, and ran it thinking it was going to tell me the temperature straight off. However, it's giving me the power-on temperature value stored in the scratchpad, despite the fact that the temperature sensor's given the "Convert T" command. As I have never used the one wire protocol before this is slightly confusing and according to the datasheet for the sensor, everything is kosher. Has anyone had the same issue?
Capacitance is futile - roll on 2012!
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 09:35pm 29 Feb 2012
Copy link to clipboard 
Print this post

That program looks good and should work for you.

Are you powering the device with parasitic power or are you using VCC? If parasitic then the DS18B20 may be not getting enough power for the temperature conversion and so when you read it it returns the power up temp of 85 degrees. If VCC then a beautiful theory is down the gurgler.
 
mookster1
Regular Member

Joined: 10/06/2011
Location: New Zealand
Posts: 81
Posted: 12:22am 01 Mar 2012
Copy link to clipboard 
Print this post

I'm using VCC off the Maximite's 5V rail (and when I was trying to write a one-wire program of my own found out the hard way that voltage regulators can supply enough current to cause magic smoke to temperature sensors connected the wrong way... of course using parasitic power would've saved me from this mess :( ). I agree with you; everything I've read points to it actually having to work but all I'm getting is the default scratchpad temp value. It's almost if the convert T command's been issued and either the temperature sensor's had a brief bout of amnesia and reset itself or there's something more sinister at play... surely if there's enough current to smoke a wrongly-connected sensor, there's enough current to read the temperature?
Capacitance is futile - roll on 2012!
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 03:21am 01 Mar 2012
Copy link to clipboard 
Print this post

Have you checked the 5 volts level? Try running it off 3.3 volts. It maxes out at 1.5mA so it's not much load.
 
mookster1
Regular Member

Joined: 10/06/2011
Location: New Zealand
Posts: 81
Posted: 11:35pm 01 Mar 2012
Copy link to clipboard 
Print this post

I tested the 5V rail with my multimeter and I got exactly 5.00V. When I've got time (time is a commodity of which I need more of right now) I'll try the sensor with 3.3V and see what happens. I was hoping to get it up and running by now because I wanted to see how the outside ambient temperature changes with the onset of an intense storm of which we should be getting tonight-tomorrowish (I was going to log it, stick it in CSV format and then graph it.)Edited by mookster1 2012-03-03
Capacitance is futile - roll on 2012!
 
iandaus

Newbie

Joined: 02/03/2012
Location: Australia
Posts: 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

 
mookster1
Regular Member

Joined: 10/06/2011
Location: New Zealand
Posts: 81
Posted: 08:33pm 04 Mar 2012
Copy link to clipboard 
Print this post

Hey Ian

I am 100% positive that I am using a DS18B20 (I ordered one online as that, and the markings on it appear to suggest that it is correct.) I've tinkered around with the original a bit but I kept a spare copy and that is having issues (it looks like it should work according to the datasheet, only that it doesn't work). I'll give your code above a go when I've got some spare time, it looks absolutely bullet-proof (I'll see that I'm a mug if it tells me it's another sensor type! ) so if that doesn't work, I honestly don't know what will. Thanks for the code!
Capacitance is futile - roll on 2012!
 
mookster1
Regular Member

Joined: 10/06/2011
Location: New Zealand
Posts: 81
Posted: 08:34pm 11 Mar 2012
Copy link to clipboard 
Print this post

Problem solved. The problem that I had was that the sensor was not getting enough power because, well, I was using it on a breadboard, and, well, there was sort of a loose ground connection... so the temperature sensor was refusing to do it's job. I have now written a nifty bit of code that samples the data automatically every 15 seconds and plots it on an automatically-scrolling graph, as well as storing the data in a CSV file for later analysis. It also calculates minimum and maximum temperatures and displays these at the bottom of the screen. I'll post the code on here sometime soonish.

Thanks for all your help!
Capacitance is futile - roll on 2012!
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024