PDA

View Full Version : Reading the DS1820 using OWIN_OWOUT.BSP



G McMurry
02-26-2010, 12:12 AM
OK --

My latest project is to build a temperature sensor. As usual I like to start with experiments or examples in my PBasic manuals.

This time, I am using OWIN_OWOUT.bsp from the "Basic Stamp Syntax and Reference Manual". Simple enough, I followed the directions for wiring (not rocket science) then cut the code from the PDF and pasted it into my PBasic Editor.

The code runs fine but comes up with strange readings. Sitting here in my office, I see 3 degrees C and 37 degrees F.

The code is working as I added a little blinking LED to toggle during each read loop. Also, if I pinch the DS1820 between my fingers, the readings go up then return when I let go.

My handy digital thermometer sitting next to the sensor reads 74 degrees F (its a little warm in my studio today)

Just for fun, I added the raw tempIn variable to the DEBUG. It is reading 39. I thought the DS18S20 was supposed to read temp directly in C

I am really confused. Any ideas would be appreciated.

Greg

(PS I have tried three different sensors with the same results I am using the actual DS1820 not the DS18S20 replacement because I had a few of them already. They are supposed to be basically the same.)



This is my DEBUG

DS1822
--------

3 C
37 F
52 tempIn


Here is the PBasic from the book (plus a couple of lines I added myself)

'Demo Program (OWIN_OWOUT.bsp)
' OWIN_OWOUT.bsp
' This program demonstrates interfacing to a Dallas Semiconductor DS1822
' 1-Wire Digital Thermometer chip using the BS2p's 1-Wire commands. Connect
' the BS2p, BS2pe or BS2px to the DS1822 as shown in the diagram in the
' OWIN or OWOUT command description. This program uses a simplified
' approach that ignores the fractional portion of the temperature.
' {$STAMP BS2p}
' {$PBASIC 2.5}

DQ PIN 0 ' 1-Wire buss pin

RdROM CON $33 ' read serial number
MatchROM CON $55 ' match SN -- for multiple devices
SkipROM CON $CC ' ignore SN -- use for one device
CvrtTmp CON $44 ' start temperature conversion
RdSP CON $BE ' read DS1822 scratch pad
HIGH 2


tempIn VAR Word ' raw temperature
sign VAR tempIn.BIT11 ' 1 = negative temperature
tLo VAR tempIn.BYTE0
tHi VAR tempIn.BYTE1
tSign VAR Bit ' saved sign bit
tempC VAR Word ' final Celsius temp
tempF VAR Word ' final Fahrenheit temp
Main:
DO


GOSUB Get_Temperature ' read temperature from DS1822

DEBUG HOME, ' display
"DS1822", CR,
"------", CR,
SDEC tempC, " C ", CR,
SDEC tempF, " F ", CR,
SDEC tempIn, " tempIn" 'and I added this just to see the raw value from the DS1820

TOGGLE 1 ' I added this just to blink an LED on my Stampworks Board
LOOP
END

Get_Temperature:
OWOUT DQ, 1, [SkipROM, CvrtTmp] ' send convert temperatrue command

DO ' wait on conversion

PAUSE 25 ' small loop pad

OWIN DQ, 4, [tempIn] ' check status (bit transfer)

LOOP UNTIL (tempIn) ' 1 when complete

OWOUT DQ, 1, [SkipROM, RdSP] ' read DS1822 scratch pad

OWIN DQ, 2, [tLo, tHi] ' get raw temp data

tSign = sign ' save sign bit
'tempC = tempIn >> 1 ' round to whole degrees
tempC = tempIn >> 4 ' round to whole degrees

tempC.BYTE1 = $FF * tSign ' correct twos complement bits
tempF = (ABS tempC) * 9 / 5 ' start F conversion
IF (tSign) THEN ' finish F conversion
tempF = 32 - tempF ' C was negative
ELSE
tempF = tempF + 32 ' C was positive
ENDIF

RETURN

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
AUTOMATE EVERYTHING
http://www.trainyard.net

Franklin
02-26-2010, 04:57 AM
1820s and 1822s are not the same chips check the datasheet and the code to make sure the numbers you are getting are the ones you want. Seems to me I had to change things around a bit to get mine working correctly.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

G McMurry
02-26-2010, 06:37 AM
I was just going by the notes on the Maxim website.

http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3021

However, I am using the DS1820 that is asked for in the Parallax Sample Code... (not the newer s model)

I don't have any problem changing the code to make the numbers work, its just that I figured I must have been doing something wrong.

Greg

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
AUTOMATE EVERYTHING
http://www.trainyard.net

Franklin
02-26-2010, 11:24 AM
Again I say the 1822 (which the code seems to be written for) and the 1820 do NOT use the same output values. Look at the two datasheets to see the difference in the data returned for a specific temperature.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

G McMurry
02-26-2010, 11:38 AM
THANKS I REALLY appreciate your advice....

Greg

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
AUTOMATE EVERYTHING
http://www.trainyard.net

G McMurry
02-26-2010, 11:39 AM
I now have two of the 1820s running at the same time with different readings. You are soo right...

Greg

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
AUTOMATE EVERYTHING
http://www.trainyard.net