CRC Calculator Routine


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    timmers's Avatar
    timmers Guest

    Default CRC Calculator Routine

    Having just busted CRC calculations, could we have a neat built in polynomial calculator. With the increasing need for CRC calculations it would save an awful lot of work for someone embarking into data comms or storage projects.

    We have written some code for calculating a 16 bit poly and at 4Mhz it takes 2 milliseconds to execute! I am sure this could be halved with some natty code.

    Tim.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by timmers View Post
    We have written some code for calculating a 16 bit poly and at 4Mhz it takes 2 milliseconds to execute! I am sure this could be halved with some natty code.
    Is that 2 mS per byte?

    Just timed my CRC routines at 4Mhz and it's ~825uS per byte for 16-bit Algorithm's, and ~710uS for 8-bit ones. Of course they all vary a little depending on the polynomial, initial value and whether there's a final XOR or bit reversal or not.

    For the most part ... A little less than half.
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Oh, and I forgot about this ...

    This was my first version.
    It was for CRC-16, but I later found that there are many versions of CRC-16.
    Even if they have the same "Polynomial".

    Might give you some idea's ....
    Code:
    CRC       VAR WORD
    CRC_IN    VAR BYTE
    Idx       VAR BYTE
    CRC_Len   CON 16
    CRC_Poly  CON $1021  ; x16 + x12 + x5 + 1
    
    ;-----CRC-16 -----------------------------------------------------
    CRC16:
      CRC.HighByte = CRC.HighByte ^ CRC_IN
      FOR Idx = 0 to 7
        IF CRC.0(CRC_Len-1) THEN
          CRC = (CRC << 1) ^ CRC_Poly
        ELSE
          CRC = CRC << 1
        ENDIF
      NEXT Idx
    RETURN
    That's actually faster than my previous measurements.
    But the other program can do any polynomial version, which is probably more than you need.
    <br>
    DT

  4. #4
    timmers's Avatar
    timmers Guest


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel,
    Its 2mS for the whole CRC routine. I simply toggle a pin at the start of the subroutine and again at the exit. It is working through 8 or so bytes to calculate the CRC value.

    Out of interest, could you expand upon the line :-
    IF CRC.0(CRC_Len-1) THEN
    I don't understand the purpose or method of the code in parenthesis.

    Thanks,
    Tim.

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


    Did you find this post helpful? Yes | No

    Default

    In the context of that program ...

    IF CRC.0(CRC_Len-1) THEN

    compiles to the exact same thing as ...

    IF CRC.15 THEN

    And at the assembly level, it's 2 instructions ...

    &nbsp; btfss _CRC + 1, 7
    &nbsp; goto TestFailed

    This allows the CRC length to be changed via a constant, with the least amount of code.
    And gets around the desire to use a variable or constant as the bit number.

    IF CRC.CRC_Len THEN ; This notation doesn't work.
    <hr>
    The CRC.0(x) notation tells PBP to treat the variable as a BIT array, and is discussed in these threads.

    Bits, Bytes Words and Arrays (Melanie)
    http://www.picbasic.co.uk/forum/showthread.php?t=544

    Indexing Port Pins (Bruce)
    http://www.picbasic.co.uk/forum/showthread.php?t=3753

    hth,
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Oh shoot, and I just went back and looked at how I timed my CRC routines, and the ~825uS was for a 9-byte array with the chars "123456789".

    That would have sounded so much better in my first post.
    <br>
    DT

Similar Threads

  1. Dallas CRC8 Routines
    By Tom Estes in forum Code Examples
    Replies: 23
    Last Post: - 8th May 2018, 18:07
  2. Calculating CRC for Modbus RTU
    By tekart in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 20th January 2010, 22:42
  3. CRC Calculations
    By timmers in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th June 2009, 17:10
  4. Sensirion CRC
    By Tom Estes in forum Code Examples
    Replies: 3
    Last Post: - 9th November 2007, 15:09
  5. Problems with CRC8 Calc in 1Wire
    By JohnB in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 16th March 2007, 22:01

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