Help with DS18B20 Code
Sassy
Posts: 21
Hi All and Merry Christmas,
I have purchased a PicAxe one-wire Controller from Mr Anderson. I am using his example program to display Temperatures. The Code is fine for
displaying Temperatures above 32 Degrees, but when it gets below or at 32 Degrees Two things happen. SignBit for Negative Temperatures gets displayed at or Below·32 Degrees and the second problem I have is that when Temperatures are below 32 Degrees the Program starts Adding upwards as the Temperature decrease below 32 Degrees. I realize that Mr Andersons program is dealing with Degrees C, but I did insert x=x+3200 to try and convert to F. It does produce fairly accurate temperatures aboue 32 Degrees.... Any Help to solve these two problems would be appreciated.
Thank You in advance
Sassy
'Now do the Calculations
··· SignBit = X / 256 / 128
··· IF SignBit = 1 THEN ' its negative
······ X = X ^ $ffff + 1 ' take the two's comp
··· ENDIF
'Multiply by 11.25 - This is 100 * Tf
··· Y = X / 4··································································· '0.25
··· X = X * 11 + Y
··· x = x + 3200································································ 'Add 32 degrees·************
··· Whole = X / 100
··· Fract = X // 100
I have purchased a PicAxe one-wire Controller from Mr Anderson. I am using his example program to display Temperatures. The Code is fine for
displaying Temperatures above 32 Degrees, but when it gets below or at 32 Degrees Two things happen. SignBit for Negative Temperatures gets displayed at or Below·32 Degrees and the second problem I have is that when Temperatures are below 32 Degrees the Program starts Adding upwards as the Temperature decrease below 32 Degrees. I realize that Mr Andersons program is dealing with Degrees C, but I did insert x=x+3200 to try and convert to F. It does produce fairly accurate temperatures aboue 32 Degrees.... Any Help to solve these two problems would be appreciated.
Thank You in advance
Sassy
'Now do the Calculations
··· SignBit = X / 256 / 128
··· IF SignBit = 1 THEN ' its negative
······ X = X ^ $ffff + 1 ' take the two's comp
··· ENDIF
'Multiply by 11.25 - This is 100 * Tf
··· Y = X / 4··································································· '0.25
··· X = X * 11 + Y
··· x = x + 3200································································ 'Add 32 degrees·************
··· Whole = X / 100
··· Fract = X // 100
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
The PicAxe is the remote 1-wire device. Here is the completes Code for a Basic Stamp that Ph Anderson uses as an example. Problem I have I want to be able to view below 32 degrees and negative Temperatures.
Basic Stamp (BS2) Routines
DS18B20_1.BS2
' {$STAMP BS2}' {$PBASIC 2.5}' DS18B20_1.BS2 (Basic Stamp 2)'' Illustrates the use of the ONEWIRE PIC in interfacing with a DS18B20 on' Channel 0.'' BS2-IC ONEWIRE PIC +5 VDC' |' P8 (term 13)
> "RX" (term 5) 4.7K DS18B20' P7 (term 12) <
"TX" (term 6) |' "0" (term 13)
DQ (term 2)' OPEN --- "BAUD" GRD ---- (term 1)' GRD ---- (term 3)'' 9600 baud.'' Copyright, Peter H. Anderson, Baltimore, MD, Nov 14, '06 BaudMode CON 84 '9600 True X VAR Word Y VAR Word SignBit VAR Byte Whole VAR Byte Fract VAR Byte DIR7 = 0 ' serial input DIR8 = 1 ' serial output OUT8 = 0 ' be sure SerOut pin is stable at zero PAUSE 1000 DOAGN: SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "S044"] 'perform temp meas ' note strong pullup PAUSE 1100 ' wait for conversion to complete SEROUT 8, BaudMode, 10, [noparse][[/noparse]"P0", "W0cc", "W0be"] ' send temperature data SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R0"] ' fetch data SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.LOWBYTE] PAUSE 100 SEROUT 8, BaudMode, 10, [noparse][[/noparse]"R0"] ' fetch data SERIN 7, Baudmode, 1500, TimeOut, [noparse][[/noparse]DEC X.HIGHBYTE] PAUSE 100 ' now do the calculations SignBit = X / 256 / 128 IF SignBit = 1 THEN ' its negative X = X ^ $ffff + 1 ' take the two's comp ENDIF ' multiply by 6.25 - This is 100 * Tc Y = X / 4 ' 0.25 X = X * 6 + Y Whole = X / 100 Fract = X // 100 IF SignBit = 1 THEN DEBUG "-", DEC Whole, ".", DEC2 Fract, CR ELSE DEBUG DEC Whole, ".", DEC2 Fract, CR ENDIF PAUSE 3000 ' wait 3 secs LOOPTimeOut: DEBUG "Timeout Error", CR GOTO AGN
Your initial code has problems below 32 deg. F and can be fixed by
adding the following snippet:
IF SignBit = 1 THEN ' its negative
X = X ^ $ffff + 1 ' take the two's comp
ENDIF
As well as changing the name of the raw temperature value read
from the DS18B20 from X to Temp_Raw
Change the code to read:
=====================================
'Now do the Calculations
' Note that the original raw temperature is stored in a variable named
' Temp_Raw and X is a completely different variable
SignBit = Temp_Raw/ 256 / 128
IF SignBit = 1 THEN ' its negative
Temp_Raw = Temp_Raw ^ $ffff + 1 ' take the two's comp to make it positive
ENDIF
'Multiply by 11.25 - This is 100 * Tf
Y = Temp_Raw / 4 '0.25
X = Temp_Raw * 11 + Y
IF SignBit = 1 THEN ' its negative
X = X ^ $ffff + 1 ' take the two's comp to make X negative, if needed
ENDIF
x = x + 3200 'Add 32 degrees ************
Whole = X / 100
Fract = X // 100
====================================
and it should work down to 0 degrees F.
phil kenny
Post Edited (phil kenny) : 12/15/2007 8:47:48 AM GMT
Attached is code that works over the full range from -55 C to +124 C.
It has been tested using the values shown in Table 2 of the DS18B20
data sheet.
The code can be simplified, but I felt it would be easier to understand
the logic if it was more verbose.
Hope this helps.
phil kenny
Post Edited (phil kenny) : 12/16/2007 8:42:37 PM GMT
Here is a routine that reads the DS18B20 and calculates degC and degF in two lines. Note that the sign of each is defined as an alias to bit15 in each variable.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Thanks for showing the 'Offset trick'. Always looking for ways
to reduce the code, Nice technique.
phil
I tried your code today on a DS18B20 and the program runs fine. However the temp readings I am getting are not correct. At 67 degrees F I am reading 33 degrees F. I looked over the code 20 times and I do not see why the readings are off? Everything looks correct. Do you have an idea?
Dave
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
D. A. Wreski
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Can you use the DS18B20 in 'power steal mode' where pins 1 and 3 are tied to ground?
Thanks
Scott
The Stamp does not have the capability to switch over to the "strong pullup" needed during the conversion. This came up in another thread recently. PhiPi suggested an word-around for a two-wire implementation, where a diode and capacitor local at each node would store the necessary power to operate the sensor in 3-wire mode.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Thanks as always for you help.
Scott
I got the DS18b20 working. Some site does not show the pull-up resister. Finally in the OWIN help file, it shows I must use
the pull-up 4.7k for the 1 wire. Yes got it working !
Now, I'll be using many 18B20, and trying to get the ID off of it.
I issue a OWOUT 0,1,[noparse][[/noparse]$33]
then OWIN 0,2,[noparse][[/noparse]serial]
and all i get is a (
hum... serial is defined as a var Word.
I'm using the BS2px ...
I'm totally lost here...
Sorry to bring that old thread.. but I'm way out of my league here...
Any help ..pretty please!
Cheers
Yosh
Ok found the solution! Not very clear but all sorted out!
On this thread: http://forums.parallax.com/forums/default.aspx?f=5&m=89924
A fairly well explained (not for a noob and old fart like me) post , that is giving the code for it.
For the noob , the file owsearch2 is only a part of what you need.
If you read carefully, you have to get the code on the nuts and volts provided in the same thread.
Be carefull ! for which BS you use, so modify your directive accordingly.
Mine had to be set to BS2px and pbasic 2.5 ..and the file extension was change to .BPX .
So, to help, my searchdemo.pbx directive was set to:
STAMP BS2px, OWSEARCH2.BPX,DSNAMES.BPX
PBASIC 2.5
So it works... finally
Just helping anyone who might be getting frustrated...
Cheers
Yosh