PDA

View Full Version : DS18B20 ID saving To and Reading From EEPROM



snuff28
- 26th November 2013, 23:46
I need a push in the right direction, the answer is probably very simple.

What I want to do is read the DS18B20 ID, save it to the EEPROM, read it back from the EEPROM into a variable to use in the program.

I will have up to 5 DS18B20's that I want to save the address to the eeprom incase one fails I can change on the fly with out reprogramming.

I know I'm going about this all wrong, BUT...

the typical read DS18B20 ID:


Start_Convert:
owout dq, 1, [$33] ' issue read rom command
ID_Loop:
owin dq, 0, [str id\8] ' Read 64-bit device data into the 8-byte array "ID"


From there I save it to the EEPROM:



write 106, ID[0]
write 107, ID[1]
write 108, ID[2]
write 109, ID[3]
write 110, ID[4]
write 111, ID[5]
write 112, ID[6]
write 113, ID[7]


Then I can read it from the EEPROM at program start




READ 98, Tempsensor1[0]
READ 99, Tempsensor1[1]
READ 100, Tempsensor1[2]
READ 101, Tempsensor1[3]
READ 102, Tempsensor1[4]
READ 103, Tempsensor1[5]
READ 104, Tempsensor1[6]
READ 105, Tempsensor1[7]
READ 106, Tempsensor2[0]
READ 107, Tempsensor2[1]
READ 108, Tempsensor2[2]
READ 109, Tempsensor2[3]
READ 110, Tempsensor2[4]
READ 111, Tempsensor2[5]
READ 112, Tempsensor2[6]
READ 113, Tempsensor2[7]
READ 114, Tempsensor3[0]
READ 115, Tempsensor3[1]
READ 116, Tempsensor3[2]
READ 117, Tempsensor3[3]
READ 118, Tempsensor3[4]
READ 119, Tempsensor3[5]
READ 120, Tempsensor3[6]
READ 121, Tempsensor3[7]
and so on...


And then I get totaly confused as to how I can select the right sensor and insert the corresponding ID

Right now I am trying with a select case lookup, but clearly thats not working...



Begin:
sensor= TEMPSENSOR 'I am reading 5 DS18b20 sensors...

FOR hexbyte=0 TO 7 'each sensor address is 8 bytes
GOSUB GetID 'go look up each sensors address
ID[hexbyte]=col 'load the ID array with the retrieved address byte
NEXT hexbyte 'go get the rest of the address bytes
OWOUT DQ, 1, [$55,STR ID\8,$44] 'instructs sensors to match[$55] this[ID] rom code and
'initiates[$44] temperature conversion on matching sensor
CkAgn:
OWIN DQ, 4, [Busy] ' Check for still busy converting
IF Busy = 0 THEN CkAgn ' Still busy?, then loop
OWOUT DQ,1,[$55,STR ID\8,$BE] 'instructs sensors to match[$55] this[ID] and start sending back scratchpad[$BE]
OWIN DQ, 2, [Raw.LOWBYTE,Raw.HIGHBYTE]' Read two temperature bytes, then end communications
Dummy = 1125 * Raw
TempF = DIV32 100
TempF = TempF + 3200

GetID:
SELECT CASE sensor
CASE 1 :LOOKUP hexbyte,[HEX Tempsensor1[0],HEX Tempsensor1[1],HEX Tempsensor1[2],HEX Tempsensor1[3],HEX Tempsensor1[4],_HEX Tempsensor1[5],HEX Tempsensor1 [6],HEX Tempsensor1[7]], col
CASE 2 :LOOKUP hexbyte,[Tempsensor2[0],[Tempsensor2[1],[Tempsensor2[2],[Tempsensor2[3],_
[Tempsensor2[4],[Tempsensor2[5],[Tempsensor2 [6],[Tempsensor2[7]], col
CASE 3 :LOOKUP hexbyte,[Tempsensor3[0],Tempsensor3[1],Tempsensor3[2],Tempsensor3[3],_
Tempsensor3[4],Tempsensor3[5],Tempsensor3[6],Tempsensor3[7],], col




;*** Temp Sensor 1************************************************* *********
ReadTemp1:
TEMPSENSOR = 1
GOSUB Begin
RETURN
;*** Temp Sensor 2 ************************************************** *******
ReadTemp2:
TEMPSENSOR = 2
GOSUB Begin
RETURN
;*** Temp Sensor3 ************************************************** *******
ReadTemp3:
TEMPSENSOR = 3
GOSUB Begin
RETURN
and so on..



I think the array thing is confusing me for some reason.

Heckler
- 27th November 2013, 00:17
take a look here...


http://www.picbasic.co.uk/forum/content.php?r=374-How-to-read-a-one-wire-DS18B20-temperature-sensor-or-nine-of-them

snuff28
- 27th November 2013, 00:48
Thanks Dwight, I originally used your code and it worked great! but now I want it all done in program. I want to read the device ID, Save it to EEPROM (incase of power failure or what not), and at program start read all the device ID arrays (pre-load it) for use in the program.
That way if I need to change one I wont have to load the read device ID program, get the ID, write it down, go into main program change the string, complie and program. It will be kind of like a hot swap with a selection in the menu to install a new sensor.

Heckler
- 27th November 2013, 03:36
snuff28,

Sorry for my short reply... I tossed it out while at work.


'================================================= ============
' Read Temp Sensor and convert to deg F
'================================================= ============
GetTemp:
OWOUT Comm_Pin, 1, [$CC, $4E, 0, 0, DS18B20_9bit] 'set resolution of sensor
Start_Convert:
OWOUT Comm_Pin, 1, [$CC, $44]' Skip ROM search & do temp conversion

Wait_Up:
OWIN Comm_Pin, 4, [Busy] ' Read busy-bit
IF Busy = 0 THEN Wait_Up ' Still busy..?, Wait_Up..!
OWOUT Comm_Pin, 1, [$CC, $BE]' Skip ROM search & read scratchpad memory
OWIN Comm_Pin, 2, [Raw.byte0, Raw.byte1]' Read two bytes / end comms

'-------------- Convert_Temp: -----------------------
Sign="+"
IF Cold_Bit = 1 THEN 'it's below zero Celsius
C=(ABS Raw>>4)*-10 'so shift the ABS value right and mult X -10
ELSE
C=(Raw>>4)*10 'else shift value right and mult X 10
ENDIF

@ CtoF _C, _F ; Convert Celsius to Fahrenheit
'converted value will be X 10, ie. 756=75.6 deg F
'so devide by 10 to get whole degrees

IF f.15=1 THEN Sign="-" 'if converted value is below zero F then sign is "-"
TempF = (ABS f)/10 'take tha ABS value and /10
IF f//10 >4 THEN TempF=TempF+1 'check remainder, if >4 then round up
return 'with TempF containing current temperature


here is a snippet of code that I use in a little project. It's been a while since I wrote it (mostly borrowed from Bruce at Rentron and some help I think from Darrell) and I havent studied it lately either.

But it is agnostic of the device (DS18b20) serial number.
It works with any sensor I install.
I'm guessing that I am able to do that because I am only reading one sensor.

I suspect that if you are trying to read multiple sensors then you have to use the device serial number to address the specific sensor.

Unless you attach only one sensor to a given PIC pin. Then I think you could ignore the device ID.

Charlie
- 27th November 2013, 13:49
If you simply want to be able to swap devices in case of failure (talk to one device at a time only) then don't bother with the serial number at all. You can distinguish the temperature sensor from other types of one wire devices by the family code (if you are building a weather station or something). If you need to talk to a few devices, separate one wire busses on different pins will work for a small number of devices.

Ioannis
- 5th December 2013, 09:35
Maxim has an application note for searching serial numbers of unknown devices. Please see the attached pdf file. It includes the algo and C code (sorry no PBP code yet).

Hope this helps,
Ioannis

Dave
- 5th December 2013, 11:55
Ioannis, Attached is a routine I wrote some time ago that searches for any 1 Wire device and displays them no mater how many are on the bus. Enjoy....

Ioannis
- 5th December 2013, 12:28
Hi Dave,

nice job and thanks for posting. Snuff28 and others, I am sure will be happy!

Ioannis

snuff28
- 6th December 2013, 13:50
Thanks Dave! The routine is awesome! Hopefully I will have some time this weekend to play with it.

Ioannis: thanks for pointing out the AN187 Search Serial number.pdf, it‎ made Daves routine make sense.

Charlie
- 7th December 2013, 14:32
So suppose I want to make indoor/ outdoor thermometers for all my family and friends as gifts. I'll have 2 DS18B20's on a bus in the design. How do I resolve which is the indoor one and which is the outdoor one?

Demon
- 8th December 2013, 03:39
Wouldn't it be obvious to them which temp was which? lol I don't know where your relatives live, but it's real easy to figure out here in Quebec. :D

Why burden yourself with labelling the temps? (if that is the reason of the question)

Robert

Acetronics2
- 8th December 2013, 13:35
So suppose I want to make indoor/ outdoor thermometers for all my family and friends as gifts. I'll have 2 DS18B20's on a bus in the design. How do I resolve which is the indoor one and which is the outdoor one?

I do not see other method than reading the sensors ID and addressing them by their ID ...

but have to reprogram the chips if you change the sensor

the clever methods here are...

1) to use two ports instead of only one !!! :rolleyes:

2) to have a routine that reads ONE sensor ID you have placed alone on the bus and makes it indoor one , then reads the second sensor you place later and make it the outdoor one ...

and so-on if a sensor change should occur one day. :D

3) use a normal sensor for outside and a ghost supply one for inside : it's possible to recognize them by software ... :p

Alain

Charlie
- 8th December 2013, 14:01
I put up my hypothetical question because I'm trying understand why you would ever use the whole serial number. If you want to build something that does not require human intervention on every restart, with multiple sensors and with the ability to swap one out if it fails, you can't use the full serial number as the method. So why would you bother to read anything beyond the family code? (other than academic interest)

snuff28
- 5th March 2014, 19:01
I'm back to try and resolve this. I can not assign each temp sensor (6 of them) a different pin as the motherboard is already produced, I am just trying to improve things a little. So all I need to figure out is how to get an array into the select case:
I have tried:

SELECT CASE sensor
CASE 1 :LOOKUP hexbyte,[STR sensor1\8], col but that does not work

In my orginal code I have:

Select Case sensor
Case 1 :LOOKUP hexbyte,[$28,$1E,$F5,$24,$03,$00,$00,$B8], col

Lets say the array sensor1\8 is $28,$1E,$F5,$24,$03,$00,$00,$B8
Hope this makes sense.

AvionicsMaster1
- 7th March 2014, 13:24
Should lookup have a : before it?