CRC Calculator Routine


Closed Thread
Results 1 to 19 of 19

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: CRC-8 with DT's routine ?

    Confirmation denied!

    The 8-bit polynomial for x^8 + x^5 + x^4 + 1 would be $31.
    And the routine for 8-bit CRC's is different.

    Code:
    CRC      VAR BYTE
    CRC_IDX  VAR BYTE
    CRC_IN   VAR BYTE
    CRC_Poly CON $31
    
    CRC8:
        FOR CRC_IDX = 0 to 7
            IF (CRC.7 ^ CRC_IN.7) THEN
                CRC = (CRC << 1) ^ CRC_Poly
            ELSE
                CRC = CRC << 1
            ENDIF
            CRC_IN = CRC_IN << 1
        NEXT CRC_IDX
    RETURN
    This test code ...
    Code:
    X      VAR BYTE
    
    CRC = 0
    FOR X = 0 TO 8
        LOOKUP X,[$AD, $42, $CB, $16, $05, $06, $00, $6C, $0E], CRC_IN
        HSEROUT [HEX2 CRC_IN," "]
        GOSUB CRC8
    NEXT X    
    HSEROUT ["= ",HEX2 CRC,13,10,13,10]
    
    CRC = 0
    FOR X = 0 TO 8
        LOOKUP X,[$AD, $42, $CC, $16, $05, $08, $00, $6C, $0C], CRC_IN 
        HSEROUT [HEX2 CRC_IN," "]
        GOSUB CRC8
    NEXT X    
    HSEROUT ["= ",HEX2 CRC,13,10,13,10]
    Returns these results ...
    Code:
    AD 42 CB 16 05 06 00 6C 0E = 87
    
    AD 42 CC 16 05 08 00 6C 0C = 0D
    DT

  2. #2
    Join Date
    Nov 2008
    Posts
    96


    Did you find this post helpful? Yes | No

    Default Re: CRC Calculator Routine

    denied

    Oh no not again.

    I took my best shot at it based on looking at some online CRC calculators and the CRC Wiki and thought it might work for what I wanted.
    Now that I've been corrected I'll test the CRC routine when I get back to my coding shortly.

    Thanks Darrel,
    Martin

  3. #3
    Join Date
    Nov 2008
    Posts
    96


    Did you find this post helpful? Yes | No

    Default Re: CRC Calculator Routine

    Hey Darrel,

    If you have time, could you show me how you went from a polynomial to the constant used in the code ?
    I was using this web based calculator to try to understand it, but I'm missing something, as I can't match your $31.
    http://ghsi.de/CRC/index.php?Polynom...06006C0E%0D%0A

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


    Did you find this post helpful? Yes | No

    Default Re: CRC Calculator Routine

    The highest bit of a polynomial is assumed to be 1, and is not included in the value used in the formula.
    It's presence in the polynomial indicates the length of the final CRC result.

    With x^8 + x^5 + x^4 + 1 ...
    The x^8 indicates that it is an 8-bit CRC, and it's bit position is not used.

    From there, you just stick the bits in a value to be used in the formula.
    Code:
        x^8 + x^5 + x^4 + 1
    
       7  6  5  4  3  2  1  0
      ------------------------  
       0  0  1  1  0  0  0  1
     ------------------------- 
     $          3           1
    The polynomial for plain CRC16 is x^16 + x^15 + x^2 + x^0
    So it's a 16-bit CRC, the x^16 is not used, and the poly value is $8005.
    Note that + x^0 is the same as + 1.

    The calculator you pointed to forces you to put the highest bit in the constant so it knows the length of the CRC.
    Otherwise, it's the same value ... $31.

    HTH
    DT

  5. #5
    Join Date
    Nov 2008
    Posts
    96


    Did you find this post helpful? Yes | No

    Default Re: CRC Calculator Routine

    Hi Darrel,

    I can now also confirm your CRC calculation code works well. Finally got back to my weather station receiver coding and added the CRC routines. No problems at all, works as advertised.

    Part of the delay was obtaining a bigger PIC to expand my code. Eventually got some PIC16F1509's.

    Regards,
    Martin

    PS. I saw your 'lost youth' sig message above. I think I did see your youth recently, but it was moving farther away from you at the time, and doesn't look like it's going to be coming back any time soon :P

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


    Did you find this post helpful? Yes | No

    Default Re: CRC Calculator Routine

    Great!
    I'm glad it you got it working.

    And that's also great you saw my Youth. That means it's still around somewhere.
    Maybe I should offer a reward?


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 : 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