CRC Calculation - Need a little help


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Steve43's Avatar
    Steve43 Guest


    Did you find this post helpful? Yes | No

    Default

    The result is already cumulative. If you compare the results to the assembly version in your link, you'll see the last version I posted outputs the same values. crc_hi & crc_lo are only cleared at the start - so they are cumulative until cleared.

    Yes, that's what I thought by looking at it, but I guess I was thrown by the fact that the results weren't as expected. However, some more experimentation shows that the results aren't as expected even when calculating the CRC for a single byte, so it would indeed appear that the algorithm in the assembly routine above and the one used by the PC-based program are not identical (although they both are supposed to be CCITT-16 reversed (0x8408)).

    I have the C source code for the algorithm used in the PC program. I will try to compare this against the PIC assembly routine and see where the difference might be, although unfortunately I'm no expert in either.

    Thanks again for your assistance. This forum is an incredible resource thanks to you and all of the people who contribute.

  2. #2
    Steve43's Avatar
    Steve43 Guest


    Did you find this post helpful? Yes | No

    Default

    Well, I'm just not able to get anywhere with this. I have attached C source code for a CCITT-16 Reversed (0x8408) algorithm that gives me what I need, but unfortunately the PIC assembly routine I found doesn't provide the same output. I don't know if anyone is kind enough to take this on but I could really use the assistance. I'd be willing to buy a PIC CRC library to do this but I haven't been able to find such a thing.

    Would someone like to take a stab at implementing this in PBP? It doesn't really have to be in assembly because speed isn't much of an issue in my application.

    Thanks (much) again.

    (Note: the attached file is in .RTF format to preserve the source code formatting but has an extension of .txt so I could attach it. Best to open it with Wordpad or other .RTF-aware application.)
    Attached Files Attached Files

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Here ya go..!
    Code:
    Poly    con $8408   ' CRC polynomial
    I       VAR BYTE
    pData   VAR BYTE
    X       VAR BYTE
    CRC     VAR WORD
    C_Data  VAR WORD
    VAL     VAR BYTE[3] ' Holds 3 values passed to CRC routiine
    
    VAL[0] = $AA
    VAL[1] = $05
    VAL[2] = $01
    
    Begin:
        CRC = $FFFF
        for X = 0 TO 2
          pData = VAL[X]
          gosub Calc_CRC
        next X
        CRC = ~CRC
        hserout [IHEX CRC.LowByte,IHEX CRC.HighByte,13,10]
        pause 1000
        goto Begin
        
    Calc_CRC:
        C_Data = $00FF & pData
        CRC = CRC ^ C_Data
        for I = 0 to 7
            if CRC.Bit0 = 1 then
             CRC = (CRC >> 1) ^ Poly
            else
             CRC = CRC >> 1
            endif
        next I
        return
    I'll let you figure out how to send your original data with the final result, but there's your C-to-BASIC
    conversion. Much easier than the assembly routine, but still easier in C....;o]
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Steve43's Avatar
    Steve43 Guest


    Did you find this post helpful? Yes | No

    Default

    Works perfectly! Thanks Bruce, I once again find myself in your debt. Your solution is simple and elegant and is actually a better option for me than what I started out with (although I did learn something by playing with the assembly integration examples.)

    Again, this forum is a great resource and thanks to you and the others who help the rest of us over the rough spots...

  5. #5
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    You're very welcome. Glad I could help...;o]

    I learn a lot here myself. Great resource.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

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. Dallas 1-Wire CRC Calculation
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 15th August 2005, 09:43

Members who have read this thread : 0

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