DS18B20 ID saving To and Reading From EEPROM


Closed Thread
Results 1 to 15 of 15
  1. #1
    Join Date
    Dec 2011
    Posts
    14

    Default DS18B20 ID saving To and Reading From EEPROM

    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:
    Code:
    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:
    Code:
            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

    Code:
            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...
    Code:
     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
    Code:
    ;*** 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.
    Last edited by snuff28; - 26th November 2013 at 23:52.

  2. #2
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  3. #3
    Join Date
    Dec 2011
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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.

  4. #4
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    snuff28,

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

    Code:
    '=============================================================
    '           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.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  5. #5
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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
    Attached Images Attached Images

  7. #7
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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....
    Attached Files Attached Files
    Dave Purola,
    N8NTA
    EN82fn

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,115


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    Hi Dave,

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

    Ioannis

  9. #9
    Join Date
    Dec 2011
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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.

  10. #10
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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?

  11. #11
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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.

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

    Robert

  12. #12
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    Quote Originally Posted by Charlie View Post
    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 !!!

    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.

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

    Alain
    Last edited by Acetronics2; - 8th December 2013 at 13:37.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  13. #13
    Join Date
    Dec 2010
    Posts
    409


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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)

  14. #14
    Join Date
    Dec 2011
    Posts
    14


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    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:
    Code:
    SELECT CASE sensor
        CASE 1 :LOOKUP hexbyte,[STR sensor1\8], col
    but that does not work

    In my orginal code I have:
    Code:
    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.

  15. #15
    Join Date
    Sep 2010
    Location
    Las Vegas, NV
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: DS18B20 ID saving To and Reading From EEPROM

    Should lookup have a : before it?

Similar Threads

  1. Reading temperature using multi DS18B20
    By KVLV in forum Code Examples
    Replies: 16
    Last Post: - 3rd November 2017, 19:48
  2. Reading DS18S20 and DS18B20 In Same Program
    By Larryd in forum General
    Replies: 4
    Last Post: - 27th January 2013, 05:57
  3. ds18b20 not reading on 16f676
    By revelator in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd April 2009, 00:20
  4. DS18B20 error reading
    By Gaetano in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th August 2007, 16:21
  5. Reading 5 DS18B20 Problem
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 30th August 2005, 20:51

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts