DS18B20 interface problem
Larrydog
Posts: 3
I have been trying to connect a DS18B20 to BS2p24.· I have tried a couple sample programs and have the same results on both programs.· The output reads 85C· / 185F.· Any help or pointers.
Thanks Larry
Sample one:
' {$STAMP BS2p}
' {$PBASIC 2.5}
' Tracy Allen, EME Systems
' DS18B20 (12 bit) program displays temperature, -55 to +125 Celsius +/- 0.1 degree
' For a single DS18B20 on the one-wire buss--the skip ROM command is used here.
' If more than one is on the buss, then the match ROM command must be used.
' Note that this routine is not good for the older 8-bit DS1820 nor the DS18S20.
degC VAR Word
signC VAR degC.BIT15·· ' alias for sign bit
degF VAR Word
signF VAR degF.BIT15·· ' alias for sign bit
DO
· OWOUT 0,1,[noparse][[/noparse]$CC, $44]
· DO : OWIN 0,4,[noparse][[/noparse]degC] : LOOP UNTIL degC· ' wait for done
· OWOUT 0,1,[noparse][[/noparse]$CC, $BE]
· OWIN 0,2,[noparse][[/noparse]degC.BYTE0, degC.BYTE1]
· degC = -signC ^ (ABS degC */ 160) + signC·· ' convert to degC*10, good for -degC too.
· degF = degC + 1000 * 9 / 5 - 1800·· ' convert to F using offset trick
· DEBUG "degC=", REP "-"\signC, DEC ABS degC/10, ".", DEC1 ABS degC,TAB
· DEBUG "degF=", REP "-"\signF, DEC ABS degF/10, ".", DEC1 ABS degF, CR
· PAUSE 1000
LOOP
Sample two:
' OWIN_OWOUT.bsp
' 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 scratch pad
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,
"DS18B20", CR,
"
", CR,
SDEC tempC, " C ", CR,
SDEC tempF, " F "
PAUSE 1000
LOOP
END
Get_Temperature:
OWOUT DQ, 1, [noparse][[/noparse]SkipROM, CvrtTmp] ' send convert temperature 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 >> 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
Thanks Larry
Sample one:
' {$STAMP BS2p}
' {$PBASIC 2.5}
' Tracy Allen, EME Systems
' DS18B20 (12 bit) program displays temperature, -55 to +125 Celsius +/- 0.1 degree
' For a single DS18B20 on the one-wire buss--the skip ROM command is used here.
' If more than one is on the buss, then the match ROM command must be used.
' Note that this routine is not good for the older 8-bit DS1820 nor the DS18S20.
degC VAR Word
signC VAR degC.BIT15·· ' alias for sign bit
degF VAR Word
signF VAR degF.BIT15·· ' alias for sign bit
DO
· OWOUT 0,1,[noparse][[/noparse]$CC, $44]
· DO : OWIN 0,4,[noparse][[/noparse]degC] : LOOP UNTIL degC· ' wait for done
· OWOUT 0,1,[noparse][[/noparse]$CC, $BE]
· OWIN 0,2,[noparse][[/noparse]degC.BYTE0, degC.BYTE1]
· degC = -signC ^ (ABS degC */ 160) + signC·· ' convert to degC*10, good for -degC too.
· degF = degC + 1000 * 9 / 5 - 1800·· ' convert to F using offset trick
· DEBUG "degC=", REP "-"\signC, DEC ABS degC/10, ".", DEC1 ABS degC,TAB
· DEBUG "degF=", REP "-"\signF, DEC ABS degF/10, ".", DEC1 ABS degF, CR
· PAUSE 1000
LOOP
Sample two:
' OWIN_OWOUT.bsp
' 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 scratch pad
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,
"DS18B20", CR,
"
", CR,
SDEC tempC, " C ", CR,
SDEC tempF, " F "
PAUSE 1000
LOOP
END
Get_Temperature:
OWOUT DQ, 1, [noparse][[/noparse]SkipROM, CvrtTmp] ' send convert temperature 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 >> 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
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Thanks for the help.
Larry
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Thanks
Larry