Reading the DS1820 using OWIN_OWOUT.BSP
G McMurry
Posts: 134
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, [noparse][[/noparse]SkipROM, CvrtTmp] ' send convert temperatrue command
DO ' wait on conversion
PAUSE 25 ' small loop pad
OWIN DQ, 4, [noparse][[/noparse]tempIn] ' check status (bit transfer)
LOOP UNTIL (tempIn) ' 1 when complete
OWOUT DQ, 1, [noparse][[/noparse]SkipROM, RdSP] ' read DS1822 scratch pad
OWIN DQ, 2, [noparse][[/noparse]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
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, [noparse][[/noparse]SkipROM, CvrtTmp] ' send convert temperatrue command
DO ' wait on conversion
PAUSE 25 ' small loop pad
OWIN DQ, 4, [noparse][[/noparse]tempIn] ' check status (bit transfer)
LOOP UNTIL (tempIn) ' 1 when complete
OWOUT DQ, 1, [noparse][[/noparse]SkipROM, RdSP] ' read DS1822 scratch pad
OWIN DQ, 2, [noparse][[/noparse]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
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Greg
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
AUTOMATE EVERYTHING
http://www.trainyard.net
Greg
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
AUTOMATE EVERYTHING
http://www.trainyard.net