ds18b20 code


Closed Thread
Results 1 to 11 of 11

Thread: ds18b20 code

  1. #1
    Join Date
    Jul 2009
    Posts
    5

    Default ds18b20 code

    Hi,
    I wanted to know if the code is correct for 10-bit

    Get_temp:
    OWOUT DQ,1,[$CC, $44] ' Skip ROM search & do temp conversion
    pause 188 ' Conversion time for 10 bit reading
    OWOUT DQ,1,[$CC, $BE] ' Skip ROM search & read scratchpad memory
    OWIN DQ,2,[TempR.Lowbyte,TempR.Highbyte]' Read two bytes / end comms

    Convert_Temp:
    TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
    TempC = (TempR & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
    Float = ((TempR.Lowbyte & $0F) * 25) ' Lower 4-bits of result * 25

    I have some doubts on the quantization:

    for 12 bit 750.0mS 0.0625C 1/16 degree so lower 4-bits of result *625

    for 10 bit 187.5mS 0.25C 1/4 degree the lower 4-bits of result *25?

    Thank you

  2. #2
    Join Date
    Jul 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    no suggestions?

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,806


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    Thefollowing example sets the 18B20 in 10bit mode, makes the conversions in the sub Convert_Temp and displays the current temperature on an LCD. Bruce Reynolds and others on this forum have contributted to make this function.

    You will need to adapt for your LCD probably.

    Code:
    DQ      var portd.7
    count_remain    var byte
    R_Temp      VAR    WORD     ' RAW Temperature readings
    TempC       VAR WORD        ' Temp in deg C
    Float       VAR WORD        ' Holds remainder for + temp C display
    Cold_Bit    VAR R_Temp.Bit11' Sign-Bit for +/- Temp. 1 = Below 0 deg C
    Real_Cold   CON 1           ' Define Real_Cold = 1
    Deg         CON 223         ' Data to display Deg ° symbol
    Sign        VAR BYTE        ' +/- sign for temp display
    Dummy       VAR BYTE        ' Dummy for Div32
    
    lcdout $fe,1
    OWOut DQ, 1, [$CC,$4E, $00,$00,$3F] ;set DS18B20 at 10bit 
    
    main11: OWOut DQ, 1, [$CC, $44]       ' Start temperature conversion
    
    waitloop: OWIn DQ, 4, [count_remain]    ' Check for still busy converting
        IF count_remain = 0 Then waitloop
    
        OWOut DQ, 1, [$CC, $BE]        ' Read the temperature
        OWIn DQ, 2, [R_temp.LOWBYTE, R_temp.HIGHBYTE]', Skip 4, count_remain, count_per_c]
        gosub convert_temp
        
    goto main11
        
    Convert_Temp:                 ' +32.0 to +257 F 
        IF Cold_Bit = Real_Cold THEN Yikes ' If Cold_Bit = 1, it's below "0" deg C
        Sign  = "+"
        Dummy = 625 * R_Temp      ' Multiply to load internal registers with 32-bit value
        TempC = DIV32 10          ' Use Div32 value to calculate precise deg C
        Dummy = 1125 * R_Temp
        TempC  = (R_Temp & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
        Float = ((R_Temp.Lowbyte & $0F) * 625)/100 ' Lower 4-bits of result * 625
        lcdout $fe,$80, " TempC = ",Sign,DEC TempC,".",DEC2 Float,Deg,"C "
        lcdout $fe,$C0, "Raw", IBIN16 R_Temp
        RETURN
    
    Yikes:                      ' Display full range -C to -F conversion
        Sign   = "-"            ' Display - symbol for negative temp
        Dummy  = 625 * ~R_Temp+1' Multiply to load internal registers with 32-bit value
        TempC  = DIV32 10       ' Use Div32 value to calculate precise deg C
        lcdout $fe,$80, " TempC = ",Sign,DEC TempC DIG 4,DEC TempC DIG 3,".",DEC TempC,Deg,"C "
        RETURN
        
    END

    Hope this helps a little
    Ioannis

  4. #4
    Join Date
    Jul 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    thanks for your answer, so the result lower 4-bits of result is always *625
    Good

  5. #5
    Join Date
    Jul 2009
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    Ioannis,
    I thank you for the help you have given me, now my code for DS18B20 works well.
    I was not able to search the forums for the original code temp.bas Bruce, I know you indicate in which post is or do you have the original?
    I want to try to change the code before the expiry time of PBP3 before I buy the full version
    thank you very much

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,806


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    I think you mean this:

    http://www.rentron.com/PicBasic/one-wire3.htm

    Ioannis

  7. #7
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    Good day ,
    I am using three DS18b20 temperature sensors.
    I would like to have them setted for 9 bit resolution to save time .
    How could i do that ?
    Any code available for setting up resolution in multiple sensor enviroment ?
    Thanks for helping
    Regards,
    Ambrogio

  8. #8
    Join Date
    Aug 2006
    Location
    Italy
    Posts
    72


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    It was explained wonderfully.
    Thanks a LOT.

  9. #9
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    I am currently looking for a piece of code for reading upto 10 sensors.
    Any help please ?
    Ambro

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,806


    Did you find this post helpful? Yes | No

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


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 code

    If you look up under the wiki link at the top of the forum and then go to projects you will find this...
    http://www.picbasic.co.uk/forum/cont...r-nine-of-them

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

Similar Threads

  1. Please help with code for DS18B20
    By fratello in forum mel PIC BASIC Pro
    Replies: 109
    Last Post: - 28th April 2013, 21:12
  2. Multi DS18b20 Code help.
    By sccsltd in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th December 2009, 06:13
  3. Ds18b20
    By ahmed_salah in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 14th June 2009, 15:21
  4. Ds18b20..
    By karenhornby in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 18th June 2008, 16:29
  5. using the DS18B20
    By lerameur in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 22nd December 2006, 13:09

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