ds18b20 and CRC check


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326

    Default ds18b20 and CRC check

    Good day to all of you in the forum.
    I wrote a picbasic code that allows me to read the temperature from about nine DS18B20 sensors. I just read the temperature from the 18b20 but I do not read the crc and I do not check it for data integrity.
    The programs works well and I get valid data from all the connected sensors.
    I would like to implement CRC check in my program and I need some help.
    > What data should I read from the sensor and how could I compute the CRC and compare it for integrity data check ?
    Thanks in advance for any help.
    Regards,
    Ambrogio

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 and CRC check

    Have a look at post 17 on this thread http://www.picbasic.co.uk/forum/showthread.php?t=7533

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


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 and CRC check

    OK, thanks.
    Is the code attached to post #18 the final one ?
    Thanks
    Ambrogio

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


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 and CRC check

    Hi,
    I just used the crc part of the code indicated by you: it is working well.
    Thanks
    regards,
    Ambrogio

  5. #5
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 and CRC check

    I've tried to incorporate the code (modified to suit my variables) in post #17 so that it will display "NC" on an LCD if the sensor is faulty or not present... but it doesn't work.

    This is the CRC check subs - the only change is the TempT variable insted of DQ

    Code:
    GetCRC:    
    for x = 0 to 7                  ; Get CRC for each of the 8 bytes
        DataByte = TempT[x]                ; Assign array pointer using value of x
        gosub CalcCRC                   ; Get CRC value
        next x                          ; Repeat until all bytes are done
        if TempT[8] <> CRCCalc then        ; Do the CRC values match?
            CRCError = 1                ; Set flag indicating a problem with this sensor
              else                        
            CRCError = 0                ; Set flag indicating no problem with this sensor
        endif                           
        CRCCalc = 0                     ; Reset CRC calculation variable
        return
    
    ;--------------------- CRC Bit Calcuation Method -------------------------------
    
    CalcCRC:
        for i = 0 to 7                      ; Do for all 8 bits in DataByte
        DataBit = CRCCalc.0 ^ DataByte.0    ; XOR bit0 of DataByte and CRC
        DataByte = DataByte >> 1            ; Position DataByte for next bit test
        if DataBit = 0 then Shift           ; If test bit not set, just shift CRCCalc
        CRCCalc = CRCCalc ^ $18             ; If set, account for EXOR feedback
    
    shift:
        CRCCalc = CRCCalc >> 1              ; Shift right the CRCCalc byte
        CRCCalc.7 = DataBit                 ; CRC bit 0 to bit bucket
        next i                              ; Data bit rotates into CRC bit 7
        return
    I then have this as the main part of my program loop

    Code:
    gosub GetCRC                    ; Calculate the CRC for comparison
    
      OWOUT TEMPIN,1,[$CC,$44]
        HIGH TEMPIN
        PAUSE 1000
        OWOUT TEMPIN,1,[$CC,$BE]   
        OWIN TEMPIN,0,[TempT.LowByte,TEMPT.HighByte]
        TempC = (TempT*/1600)
        if CRCError = 1 then 
            LCDout $FE,$80+5,"N/C     "
        endif
        if CRCError = 0 then
        LCDout $FE,$80,"Temp ",DEC (TempC/100),".",#TempC dig 1,"C" 
        endif
    With teh DS18B20 connected I get a true temperature displayed on the screen, but If I remove the DS18B20 from the inpt pin the LCD displays 163.7c on the display (presumably it's the maximum value for a word variable and the the formulat applied to convert it to a temperature) rather than "NC"

    Any suggestions ?

    EDIT:

    OK I thought about it a bit more and realised I needed to read the sensor first and then do the check and then apply the if then condition.... but now all I get is "NC" on the screen even when the sensor is connected !
    Last edited by Scampy; - 27th November 2015 at 23:27.

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: ds18b20 and CRC check

    OWIN TEMPIN,0,[TempT.LowByte,TEMPT.HighByte]
    what about the other 5 data bytes that you need for the crc calc , and the crc actually byte sent by the ds18b20 to check against ?

    hint , if you feed the final crc byte sent into the crc routine then if there are no errors the result is always zero
    Last edited by richard; - 28th November 2015 at 00:47. Reason: spelling

Similar Threads

  1. Crc-16
    By Electrosolve in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 18th June 2013, 17:11
  2. Crc
    By grich in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 7th May 2008, 09:51
  3. Replies: 0
    Last Post: - 26th March 2006, 00:13
  4. CRC Check for Dellas DS1973 needed
    By NL2TTL in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th May 2005, 23:24
  5. CRC Calculation - Need a little help
    By Steve43 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 13th March 2005, 01:23

Members who have read this thread : 2

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