Problems with CRC8 Calc in 1Wire


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2007
    Posts
    4

    Default Problems with CRC8 Calc in 1Wire

    I thought I had this all cracked a while ago but on returning to it now I don't seem to be the right answer.

    I am using the routine posted by Tom Estes http://www.picbasic.co.uk/forum/show...ighlight=1Wire
    but when I test it against what I understand to be valid Device IDs I get a CRC failure.

    This is my code: Can anyone suggest what I am doing wrong?

    I Var Byte
    CRC Var Byte
    CRCData Var Byte
    TestBit Var Bit
    OW_Byte_Ptr Var Byte

    '' PBP Version not using lookup table
    '' Expects CRC starting value and CRCData
    '' CRC will be cumulative unless reset externally
    '' Gives ending CRC value and destroys CRCData
    '' Uses I-Byte, TestBit-Bit, CRC-Byte, CRCData-Byte

    CRC8:
    For I = 0 To 7 ' Do for all 8 bits in data byte
    TestBit = CRC.0 ^ CRCData.0 ' XOR bit0 of data byte and crc
    CRCData = CRCData >> 1 ' Position data byte for next bit test
    If TestBit = 0 Then Shift ' If test bit not set, just shift CRC
    CRC = CRC ^ $18 ' If set, account for EXOR feedback
    Shift: ' Shift right the CRC byte
    CRC = CRC >> 1 ' CRC bit 0 to bit bucket
    CRC.7 = TestBit ' Test bit rotates into CRC bit 7
    Next I
    Return

    'TestCode------------------------------------------------

    TestData VAR Byte[8]

    ' Load up Data array

    TestData[0] = $05 '$28 'Family Code
    TestData[1] = $C5 '$B1 'Serial Number
    TestData[2] = $C3 '$FE
    TestData[3] = $08 '$22
    TestData[4] = $00 '$00
    TestData[5] = $00 '$00
    TestData[6] = $00 '$00
    TestData[7] = $CD '$5D 'CRC

    Main:
    For OW_Byte_Ptr = 0 To 7
    CRCData = TestData[OW_Byte_Ptr]
    GoSub CRC8
    HSerOut [HEX2 CRC, 10, 13]
    Next

    If CRC = 0 Then
    HSerOut ["CRC OK",10,13]
    Else
    HSerOut ["CRC Error ", HEX2 CRC,10,13]
    EndIf

    Stop

    What I am actually getting is
    35
    FF
    4C
    7E
    D8
    3E
    8D
    B1
    CRC Error B1

    Where I should be getting 0

    Am I missing something?

    JohnB

  2. #2
    Join Date
    Jan 2004
    Location
    Grand Lake O' Cherokees USA
    Posts
    15


    Did you find this post helpful? Yes | No

    Default

    JohnB,

    I'm away from my hardware so I can't test your code. However it appears you have not initialized CRC before you test the first byte. Add CRC=0 right after your Main: label before you start to submit bytes to the routine. If that doesn't fix it let me know and when I get home I'll test your software.

    Tom Estes
    Tom

  3. #3
    Join Date
    Jan 2007
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    Tried that - this is the result:

    A8
    13
    06
    46
    A4
    65
    76
    E5
    CRC Error E5


    JohnB

  4. #4
    Join Date
    Jan 2004
    Location
    Grand Lake O' Cherokees USA
    Posts
    15


    Did you find this post helpful? Yes | No

    Default

    You are jumping around the subroutine upon start up right? If your software is verbatum to what you posted then you need to add Goto Start just before the CRC8: label and place a Start: label before your testcode comment. I'm not sure what PBP will do with variables declared after coding starts but I would move the TestData VAR Byte(8) up to before the subroutine where the other declarations are.

    Again, I'm away from home for a couple of days so I can't test the code.....
    Tom

  5. #5
    Join Date
    Jan 2007
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    Can't believe how stupid I have been, indeed it was an extract from a much larger program which I was having a problem with. In the extract I was skipping over loading the array so it was never getting to operate on correct data.

    I will now have to go back to my original code and see where it is going wrong as I still have a problem with the real code, it just not with the CRC calc as I h ad originally thought.

    Sorry to have bothered you.

    JohnB

  6. #6
    Join Date
    Oct 2004
    Location
    Hangover, Germany
    Posts
    289


    Did you find this post helpful? Yes | No

    Cool

    Yesterday I have implemented the CRC-8-calculating from 1-wire with a short routine without tables from:

    http://www.dattalo.com/technical/software/pic/crc.php
    PBP 2.50C, MCS+ 3.0.0.5, MPLAB 8, MPASM 5.14, ASIX Presto, PoScope, mE mikroBasic V7.2, PICKIT2

Similar Threads

  1. Dallas CRC8 Routines
    By Tom Estes in forum Code Examples
    Replies: 23
    Last Post: - 8th May 2018, 19:07
  2. 2 PWM in 16F690 problems
    By ciendavila in forum mel PIC BASIC
    Replies: 9
    Last Post: - 27th April 2008, 10:03
  3. bootloader Freqout problems
    By handgio in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th December 2007, 14:38
  4. Problems with 16F876 on interrupts an WRITE / READ
    By BigWumpus in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th December 2005, 15:38
  5. USART problems
    By egberttheone in forum mel PIC BASIC Pro
    Replies: 47
    Last Post: - 6th March 2005, 22:45

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