Please help with code for DS18B20


Closed Thread
Results 1 to 40 of 110

Hybrid View

  1. #1
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default

    Hi, Fratello

    some homework to do for you ...

    take the DS18B20 Datasheet out, and look at :

    1) How to get the device ID
    2) How to address the device with it's ID

    Buuuuuut, I do think ALL the answers you look for already are posted on this Maaarrrrrvellous Forum !!!

    It's called SEARCH Function ...

    Alain

    PS: HELP Function momentarily out of use : New Year update ... lol
    ************************************************** ***********************
    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 " !!!
    *****************************************

  2. #2
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default

    I try to read the serial number of my DS18B20 sensor with this code :

    TRISA= %11110000 ' RA0..3=Outputs RA4=Input
    TRISB= %00000111 ' RB0..RB2=Inputs, RB3..RB7=Outputs
    CMCON=7 ' Disable comparators

    DEFINE LCD_DREG PORTB ' LCD on port B
    DEFINE LCD_DBIT 4 ' Data bits B4..B7
    DEFINE LCD_RSREG PORTA ' RS on PORTA
    DEFINE LCD_RSBIT 1 ' RS on A1
    DEFINE LCD_EREG PORTA ' E on PORTA
    DEFINE LCD_EBIT 0 ' E on A0
    DEFINE LCD_BITS 4 ' LCD 4 bit mode
    DEFINE LCD_LINES 2 ' 2 line LCD display

    DQ Var PORTA.4 ' One-wire data pin
    i Var Byte
    Temp Var Byte
    ReadRom Con $33

    Pause 500
    LCDOUT $FE, 1, $FE, $0C ' Clear display, cursor off
    Pause 250

    Main

    OWOut DQ, 1, [ReadRom]
    for i = 0 to 7
    OWIn DQ, 0, [Temp]
    LcdOut $FE, $80, Hex Temp
    Next
    LcdOut $FE, $14
    LcdOut $FE, $14
    Pause 1000
    GoTo Main


    but on display appears only "A7". What I do wrong ? Thanks !

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Code:
    LcdOut $FE, $80, Hex Temp
    The $80 moves the cursor to Line1 Column1, and it overwrites the previous values on each loop. All you see is the last loop.
    <br>
    DT

  4. #4
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default

    Per aspera ad astra !!!!
    I write another code :

    DQ Var PORTA.4 ' One-wire data pin
    i Var Byte
    Temp Var Byte
    ReadRom Con $33
    ID VAR BYTE[8] ' Array storage variable for 64-bit ROM code


    Begin:
    PAUSE 500 ' Wait .5 second
    LCDOUT $FE, 1, $FE, $0C ' Clear display, cursor off

    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"
    for i = 0 to 7
    LcdOut HEX ID[i]
    next i
    END



    Now I have on display : 28CAC648100A7
    I think now it is correct. Can You confirm ? Thanks !

  5. #5
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink

    Hi, Fratello

    Looks fine

    my DS shows 7A00000190DF64

    read you soon

    Alain
    ************************************************** ***********************
    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 " !!!
    *****************************************

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Almost, but there's not enough digits.

    64-bits (8-bytes), will have 16 hex digits.

    Try it this way ...
    Code:
    LcdOut HEX2 ID[i]
    DT

  7. #7
    Join Date
    Dec 2008
    Location
    Ploiesti, ROMANIA
    Posts
    582


    Did you find this post helpful? Yes | No

    Default

    Thank you all for reply !
    In this book : "PIC Microcontrollers: Know It All" from Lucio Di Jasio, Tim Wilmshurst, Dogan Ibrahim, John Morton, Martin Bates, Jack Smith, D.W. Smith, and Chuck Hellebuyck - witch is my inspiration , I read this :
    "Dallas Semiconductor’s 1-wire specifications define device serial numbers as 8 bytes (64 bits) long.The family code for 18B20 digital thermometer chips is $28. The CRC (cyclic redundancy code) is an error-checking feature, so that, should we desire, we may verify that the 56 bits of the family code and serial number have been correctly received and were not corrupted. We’ll not further consider how CRCs are calculated, as it’s a topic well beyond the level of this introductory book.
    When we run the program, we see the following output:
    28 4C D4 3E 0 0 0 D6
    ...
    The digits 4C D4 3E 0 0 0 D6 are, of course, dependent upon the particular DS18B20 chip.
    I plugged in a second DS18B20 chip and found its serial number:
    28 FE DA 3E 0 0 0 C1
    In the output, $28 is the family number and $D6 (or $C1 in the second example) is the CRC. The center six bytes represent the serial number of the chip. But, here’s a difference between the result and the serial number specification isn’t there? The definition has the CRC sent first and the family code sent last. Yet, Program 22.1 displays the family code first, and the CRC last.The explanation is that 1-wire devices store the least significant byte at the lower address and
    the most significant byte at the higher address. Bytes are transmitted and received from lowest address to highest address. Hence, the net effect is the send/receive byte order is reversed from the data sheet description. This is more confusing to describe than to use; when we wish to address a particular device, we just repeat the byte order we read with Program 22.1"

    So, I think it is enough for me these results.
    Next step is : Reading multiple sensors on the same bus.
    ... Wish me luck !
    I will keep you informed and - hope- I will receive yours advices again.
    All the best !
    Last edited by fratello; - 10th January 2009 at 11:26.

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink

    Hi, Fratello

    From the Datasheet Organigrams ... you'll see the Bytes Order is inverted ...

    as you worked Well ...

    Code:
    '*****************************************************************************
    SensID:' Sensor Identification
    '*****************************************************************************
    
      OWOUT DQ, 1, [ $33 ]								' Read Chip code
      OWIN  DQ, 2, [ FAM,ID[6],ID[5],ID[4],ID[3],ID[2],ID[1],CRC]
      
      IF FAM = $05 THEN
      
       	LCDOUT $FE,$80, "DS 2405     "
       	
      	PAUSE 1000
      	
      	LCDOUT $FE,$80, 				" CRC: ",HEX2 CRC," FAM: ", HEX2 FAM
      	
      ENDIF
      
      IF FAM = $10 THEN
      
      	LCDOUT $FE,$80, "DS 18S20/1820"
      	
      	PAUSE 1000
      	
      	LCDOUT $FE,$80, 				" CRC: ",HEX2 CRC," FAM: ", HEX2 FAM
      	
      ENDIF
      	  	
      IF FAM = $28 THEN
      
       	LCDOUT $FE,$80, 				"DS 18B20     " 
       	
     	OWOUT DQ, 1, [$CC, $4E, 0, 0, DS18B20_11bit]    'Skip ROM search and write N_bits
      	'                                                 resolution to scratch pad
      	PAUSE 1000
      	
      	LCDOUT $FE,$80, 				" CRC: ",HEX2 CRC," FAM: ", HEX2 FAM
      	
      ENDIF
      
      	LCDOUT $FE,$C0," ID:",HEX2 ID[1],HEX2 ID[2],HEX2 ID[3],HEX2 ID[4],HEX2 ID[5],HEX2 ID[6]
    Here Data are shown with their signification ... Hope it will help

    Alain
    ************************************************** ***********************
    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 " !!!
    *****************************************

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

Members who have read this thread : 3

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