PDA

View Full Version : Reading DS18S20 and DS18B20 In Same Program



Larryd
- 25th January 2013, 21:19
I'm new to PBP and have been working with some DS18S20 and DS18B20 Sensors.
I'm using a PIC 16F877 here is my loop that reads the DS18S20. Is there a way
to detirmine if the sensor is a DS18B20 the on the Pin instead of a DS18S20?
Evidently the 18B20 requires a different routine to read an determine the temperature.
I get +22.06C on all 3 of the DS18S20 senors, but +152.87C and 152.81 on my DS18B20
sensors.


Thanks In Advance
Larry






DEFINE OSC 10 ' We're using a 10MHz oscillator
BAUD CON 84 ' N9600 for serial
CR CON 13 ' CR serial
LF CON 10 ' N9600 for serial LCD
SO VAR PORTC.6 ' Pin to for serial Out
SI VAR PORTC.7 ' Pin to for serial In
' Allocate variables
command var byte ' Storage for command
i var byte ' Storage for loop counter
sn var byte 'Polling Sensor Number
Rsn var byte 'Real Sensor Number
TempR var word ' Storage for temperature
TempS var word [12] 'Array To Store and send all Temps at one time
'DS1 var PORTB.0 ' Alias DS1820 data pin
'DS2 var PORTB.1 ' Alias DS1820 data pin
'DS3 var PORTB.2 ' Alias DS1820 data pin
'PORTB.3 is Reserved
'DS4 var PORTB.4 ' Alias DS1820 data pin
'DS5 var PORTB.5 ' Alias DS1820 data pin
'DS6 var PORTB.6 ' Alias DS1820 data pin
'DS7 var PORTB.7 ' Alias DS1820 data pin

'DS8 var PORTC.0
'DS9 var PORTC.1 ' Alias DS1820 data pin
'DS10 var PORTC.2 ' Alias DS1820 data pin
'DS11 var PORTC.3 ' Alias DS1820 data pin
'DS12 var PORTC.4 ' Alias DS1820 data pin
'PORTB.0 = 0-7
'PORTC.0 = 8-15

'*****DS1820 VARS AND SETTINGS*******
count_remain var byte 'DS1820 counter of remain bits
count_per_c var byte 'DS1820 counter per C bits
TempNeg var byte 'temperature sign -/+
TpNeg var byte [12] 'temperature sign -/+
'**********************************
SEROUT2 SO,BAUD, ["PIC Controller Ready",CR,LF]
sn = 0
Rsn =1
Main:

SEROUT2 SO,BAUD, ["Reading Sensor ",DEC Rsn,CR,LF]
i = 0
if sn = 3 then sn = 4
gosub Read_Temp

if i=10 then ErrX
if TempR=$FFFF then Error

TempS[Rsn-1] = TempR
TpNeg[Rsn-1] = TempNeg
'gosub Disp_Temp
sn = sn + 1
Rsn = Rsn + 1
if sn = 13 then
gosub Disp_AllTemp
sn = 0
Rsn =1
endif
TempR=$FFFF

pause 200
SEROUT2 SO,BAUD, ["========================================",CR,LF]
SEROUT2 SO,BAUD, [CR,LF]
goto Main
Read_Temp:

OWOut sn, 1, [$CC, $44] ' Start temperature conversion
waitloop1:
OWIn sn, 4, [count_remain] ' Check for still busy converting
If count_remain = 0 Then
pause 100
i = i + 1
if(i = 10)then return
goto waitloop1
endif
OWOut sn, 1, [$CC, $BE] ' Read the temperature
OWIn sn, 0, [TempR.LowByte, TempR.HighByte, Skip 4, count_remain, count_per_c]

if TempR = $FFFF then return
if (TempR.HighByte = $FF) and (TempR.LowByte <> 0) then
TempNeg = "-"
TempR = TempR ^ $FFFF
TempR = ((TempR >> 1)*100) - 25 + ((count_per_c + count_remain) * 100) / count_per_c
else
if (TempR.HighByte < $FF) and (TempR.LowByte <> 0) then
TempNeg = "+"
TempR = ((TempR >> 1)*100) - 25 + ((count_per_c - count_remain) * 100) / count_per_c
else
'************************************************
TempR=$0000 ' THIS IS ONLY NEED TO DEFINE !!!
'************************************************
endif
endif
return
Disp_AllTemp:
SEROUT2 SO,BAUD,[2,","]
for i = 0 to 11
if TpNeg[i] = "E" then
SEROUT2 SO,BAUD, ["TS",dec i+1,",E/R|"]
elseif TpNeg[i] = "N" then
SEROUT2 SO,BAUD, ["TS",dec i+1,",N/R|"]
else
SEROUT2 SO,BAUD, ["TS",dec i+1,",",TpNeg[i], DEC (TempS[i] / 100),".", DEC2 TempS[i], "C|"]
endif
TempS[i] = 0
TpNeg[i] = 0
next


SEROUT2 SO,BAUD,[CR,LF]

'SEROUT2 SO,BAUD, ["Raw", IBIN16 TempR,CR,LF]
'SEROUT2 SO,BAUD, ["---------------------------------" ,CR,LF]

return
Error:
SEROUT2 SO,BAUD, ["ERROR - Reading Sensor:", dec Rsn ,CR,LF]
TempS[Rsn-1] = 0
TpNeg[Rsn-1] = "E"
sn = sn + 1
Rsn = Rsn + 1
if sn = 13 then
sn = 0
Rsn =1
endif

goto Main
ErrX:
' "!!!ERROR!!!", $fe, $C0, "Reading Temp."
SEROUT2 SO,BAUD, ["Sensor: ",dec Rsn," Not Reporting" ,CR,LF]
TempS[Rsn-1] = 0
TpNeg[Rsn-1] = "N"
sn = sn + 1
Rsn = Rsn + 1
if sn = 13 then
sn = 0
Rsn =1
endif
goto Main

end

Darrel Taylor
- 25th January 2013, 22:13
You can determine which device it is by reading the "Family" code.

Issue a READROM command ($33).
Then read back 1 byte.

$10 = DS18S20 or DS1820
$28 = DS18B20

Larryd
- 26th January 2013, 01:35
Thank you Darrel

Works great !

I was studying your code in this post
http://www.picbasic.co.uk/forum/showthread.php?t=6013&highlight=OWPIN%3FW


Will the code parameters be the same as before mod:

OWpins VAR WORD[16] ; assign pins for OneWire commands.
OWpins(0) = $0000 ; PORTA is 0, bit 0 ; Assignments can be in any order
OWpins(1) = $0002 ; bit 2 ; Shown sequentially here for clarity
OWpins(2) = $0100 ; PORTB is 1, bit 0
OWpins(3) = $0101 ; bit 1
OWpins(4) = $0102 ; bit 2
OWpins(5) = $0103 ; bit 3
OWpins(6) = $0104 ; bit 4
OWpins(7) = $0105 ; bit 5
OWpins(8) = $0106 ; bit 6
OWpins(9) = $0107 ; bit 7
...
OWpins(15) = $0307 ; PORTD is 3, bit 7

Will the code parameters be the same as before the mod:?

Before Mod:
OWOut sn, 1, [$CC, $BE] ' Read the temperature
After Mod:
OWOut OWpins(sn), 1, [$CC, $BE] ' Read the temperature

Thank you!
and thanks for sharing your knowledge.


I have pretty much went through reading Henrik's, Bruce's, your answers to questions for
the past two weeks and it has really helped me get an understand how powerful this PBP
compiler is.

Thanks again !
Larryd

Darrel Taylor
- 26th January 2013, 19:09
You're welcome Larryd.

Yes, the rest of the prarameters are still the same.
Only the pin assignment method changes.

I had forgotten about that thread.
But after looking at it again, and since you're using DS1820's ... You might want to set the pins in EEPROM.
Then the pin assignments can be changed later at Run-Time via a menu or serial commands.

Maybe somthing like ...
OWpins VAR WORD[10] ; default pins for OneWire commands.
Sensor0 DATA WORD $0000 ; PORTA.0
Sensor1 DATA WORD $0001 ; 1
Sensor2 DATA WORD $0100 ; PORTB.0
Sensor3 DATA WORD $0101 ; 1
Sensor4 DATA WORD $0103 ; 3
Sensor5 DATA WORD $0200 ; PORTC.0
Sensor6 DATA WORD $0201 ; 1
Inside DATA WORD $0204 ; 4
Outside DATA WORD $0205 ; 5
Bedroom DATA WORD $0206 ; 6


Idx VAR BYTE
Addr VAR WORD

Addr = Sensor0
FOR Idx = 0 TO 9 ; Initialize OW pins from EEPROM
READ Addr + Idx<<1, WORD OWpins(Idx)
NEXT Idx

You'll need PBP 2.60 or later.

Larryd
- 27th January 2013, 05:57
Thanks Darrel

I like having the ability to name the sensors, I will give this a try.
Thanks Again.

Ld