Crc-16


Closed Thread
Results 1 to 3 of 3

Thread: Crc-16

  1. #1
    Join Date
    Jun 2009
    Location
    Cambridge, England
    Posts
    9

    Default Crc-16

    Hello

    I'm using Darrel's CRC routine to calculate a CRC but the results are not as expected.

    Here is Darrel's routine -
    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
    The eight bytes I'm using are "F2 AA 4C D4 60 F6 00 80"

    The result I get is B2D5 rather than E23E as it should be.

    Using an online calculator I get the same result, but if I tick the box "bit reflected", I get the correct result.

    I'm sure I'm missing something really simple but I have tried many things without success.

    Any Ideas?
    Last edited by Darrel Taylor; - 18th June 2013 at 18:32. Reason: added [code][/code] tags

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


    Did you find this post helpful? Yes | No

    Default Re: Crc-16

    There are many variations of CRC16.
    Along with the different polynomial ... Some need a starting value in the CRC variable, some need a final XOR of the CRC result with another value, some reverse the bit order of the data, and some use combinations of those methods.

    The variation you are looking for is called CRC16_CCITT_TRUE or CRC16_KERMIT, which reverses the bit order.

    For this type, the CRC16: code is the same.
    But when you are feeding it data, reverse the order of each byte.
    Then reverse the order of the final result.
    Code:
        ARRAYWRITE MyArray,[$F2,$AA,$4C,$D4,$60,$F6,$00,$80] ; Your Data
    
    
        CRC = 0
        FOR X = 0 TO 7
            CRC_IN = MyArray(X) REV 8
            GOSUB CRC16
        NEXT X
        CRC = CRC REV 16
    The above gives the result ... $E23E
    Last edited by Darrel Taylor; - 18th June 2013 at 17:51.
    DT

  3. #3
    Join Date
    Jun 2009
    Location
    Cambridge, England
    Posts
    9


    Did you find this post helpful? Yes | No

    Default Re: Crc-16

    Hello Darryl

    Thank you for that. It now looks fine.

    I did try reversing the bytes but didn't think about reversing the result, so I guess I was about halfway to solving it.

    You have saved me a lot of time - thanks again.

    BR

    Reg Smith

Similar Threads

  1. Help with CRC checksums
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th February 2013, 00:25
  2. how to specific crc ?
    By phoenix_1 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 17th March 2010, 11:31
  3. CRC Calculations
    By timmers in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th June 2009, 18:10
  4. Crc
    By grich in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 7th May 2008, 10:51
  5. CRC Calculation - Need a little help
    By Steve43 in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 13th March 2005, 02:23

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