interfacing DS1820 with BS2
Sourabh
Posts: 5
I am having toruble interfacing the DS1820 with BS2. I am trying to display the temperature on a 2x16 serial LCD which I bought from parallax itself. I have connected the data pin of DS1820 to pin 11 of BS2 using a 4.7k resistor. It is similar to Jon William's code to interface the 1820 and BS Sx plus.
Here is my code:
' {$STAMP BS2}
' {$PBASIC 2.5}
LCDpin CON 0 ' data on pins 4 - 7
DS1820pin CON 11
B_19200 CON 32
NoCmd CON $00 ' just print
DDRam CON $80 ' Display Data RAM control
Line1 CON $80 ' address of line 1
Line2 CON $C0 ' address of line 2
DegSym CON 223 ' degrees symbol
ReadROM CON $33 ' read ID, serial num, CRC
MatchROM CON $55 ' look for specific device
SkipROM CON $CC ' skip rom (one device)
ConvertTemp CON $44 ' do temperature conversion
ReadScratch CON $BE ' read DS1820 scratchpad
' ----[noparse][[/noparse] Variables ]
'
idx VAR Byte ' loop counter
romData VAR Byte(8) ' ROM data from DS1820
tempIn VAR Word ' raw temperature
sign VAR tempIn.BIT8 ' 1 = negative temperature
tInLow VAR tempIn.LOWBYTE
tInHigh VAR tempIn.HIGHBYTE
tSign VAR Bit
tempC VAR Word ' Celsius
tempF VAR Word ' Fahrenheit
Main:
HIGH LCDpin
PAUSE 100
TempDemo:
SEROUT LCDpin,B_19200,[noparse][[/noparse]"CURRENT TEMP:"]
ShowTemp:
SEROUT DS1820pin,B_19200,[noparse][[/noparse]SkipROM]
SEROUT DS1820pin,B_19200,[noparse][[/noparse]ConvertTemp]
PAUSE 500 ' give time for conversion
SEROUT DS1820pin,B_19200,[noparse][[/noparse]SkipROM]
SEROUT DS1820pin,B_19200,[noparse][[/noparse]ReadScratch]
PAUSE 500
SERIN DS1820pin,16780,[noparse][[/noparse]tInLow,tInHigh] ' read temperature
tSign = sign ' save sign bit
tempIn = tempIn/2 ' round to whole degrees
IF tSign = 0 THEN NoNeg1
tempIn = tempIn | $FF00 ' extend sign bits for negs
NoNeg1:
tempC = tempIn ' save Celsius value
tempIn = tempIn */ $01CC ' multiply by 1.8
IF tSign = 0 THEN NoNeg2 ' if neg, extend sign bits
tempIn = tempIn | $FF00
NoNeg2:
tempF = tempIn+32 ' finish C -> F conversion
' display temps
'
SEROUT LCDpin,Line2,[noparse][[/noparse]SDEC tempC, DegSym, " C"]
SEROUT LCDpin,NoCmd,[noparse][[/noparse]" / ", SDEC tempF, DegSym, " F"]
SEROUT LCDpin,NoCmd,[noparse][[/noparse]REP " "\6]
PAUSE 1500
'GOTO ShowTemp ' update temp display
Thanks,
Sourabh
Here is my code:
' {$STAMP BS2}
' {$PBASIC 2.5}
LCDpin CON 0 ' data on pins 4 - 7
DS1820pin CON 11
B_19200 CON 32
NoCmd CON $00 ' just print
DDRam CON $80 ' Display Data RAM control
Line1 CON $80 ' address of line 1
Line2 CON $C0 ' address of line 2
DegSym CON 223 ' degrees symbol
ReadROM CON $33 ' read ID, serial num, CRC
MatchROM CON $55 ' look for specific device
SkipROM CON $CC ' skip rom (one device)
ConvertTemp CON $44 ' do temperature conversion
ReadScratch CON $BE ' read DS1820 scratchpad
' ----[noparse][[/noparse] Variables ]
'
idx VAR Byte ' loop counter
romData VAR Byte(8) ' ROM data from DS1820
tempIn VAR Word ' raw temperature
sign VAR tempIn.BIT8 ' 1 = negative temperature
tInLow VAR tempIn.LOWBYTE
tInHigh VAR tempIn.HIGHBYTE
tSign VAR Bit
tempC VAR Word ' Celsius
tempF VAR Word ' Fahrenheit
Main:
HIGH LCDpin
PAUSE 100
TempDemo:
SEROUT LCDpin,B_19200,[noparse][[/noparse]"CURRENT TEMP:"]
ShowTemp:
SEROUT DS1820pin,B_19200,[noparse][[/noparse]SkipROM]
SEROUT DS1820pin,B_19200,[noparse][[/noparse]ConvertTemp]
PAUSE 500 ' give time for conversion
SEROUT DS1820pin,B_19200,[noparse][[/noparse]SkipROM]
SEROUT DS1820pin,B_19200,[noparse][[/noparse]ReadScratch]
PAUSE 500
SERIN DS1820pin,16780,[noparse][[/noparse]tInLow,tInHigh] ' read temperature
tSign = sign ' save sign bit
tempIn = tempIn/2 ' round to whole degrees
IF tSign = 0 THEN NoNeg1
tempIn = tempIn | $FF00 ' extend sign bits for negs
NoNeg1:
tempC = tempIn ' save Celsius value
tempIn = tempIn */ $01CC ' multiply by 1.8
IF tSign = 0 THEN NoNeg2 ' if neg, extend sign bits
tempIn = tempIn | $FF00
NoNeg2:
tempF = tempIn+32 ' finish C -> F conversion
' display temps
'
SEROUT LCDpin,Line2,[noparse][[/noparse]SDEC tempC, DegSym, " C"]
SEROUT LCDpin,NoCmd,[noparse][[/noparse]" / ", SDEC tempF, DegSym, " F"]
SEROUT LCDpin,NoCmd,[noparse][[/noparse]REP " "\6]
PAUSE 1500
'GOTO ShowTemp ' update temp display
Thanks,
Sourabh
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Thanks,
Sourabh
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Post Edited (Jon Williams (Parallax)) : 12/1/2005 12:37:41 AM GMT