I am currently working with a Melexix temperature measurement device that uses SMBus protocol with transactions protected with a Packet Error Code (PEC) byte in CRC-8 format. While Tom Estes has posted some code at http://www.picbasic.co.uk/forum/showthread.php?t=1672 that looks like it works for this same format (based on a Dallas Semi app note), I did not find the example code to work for my application. So I filed off the serial numbers from his code and re-worked it.

Code was tested by putting a device read into a loop and comparing the returned PEC from one I calculated from this code. While I did not do an exhaustive test of all combinations this is a fairly strong check. Of course, YMMV.


Hi, we are pleased to see that you are using our forum.

We'd like you to become a member of our community. Membership is FREE, please REGISTER and view our forums without these annoying reminders!

Once registered you may post on the forums, download from the file areas and use the WIKI without interruption.






Code:
 ' ********** Subroutine CRC8LU **********
' Uses CRCData for input, CRC for output
' CRC must be initialized to 0 for each new data set
' CRCData is used to pass each byte in turn to the check code
' no other variables used or changed in this code
CRC8LU:
    CRC = CRCData ^ CRC
    ' PBP only handles 255 constants in the list (256 for 18Cxxx). Since table is
    ' indexed starting at zero, special case for $FF (256th element).
    If CRC = $FF Then
        CRC = $F3
    ELSE
        Lookup CRC, [$0,  $7,  $E,  $9,  $1C, $1B, $12, $15, $38, $3F, $36, $31,_
                     $24, $23, $2A, $2D, $70, $77, $7E, $79, $6C, $6B, $62, $65,_
                     $48, $4F, $46, $41, $54, $53, $5A, $5D, $E0, $E7, $EE, $E9,_
                     $FC, $FB, $F2, $F5, $D8, $DF, $D6, $D1, $C4, $C3, $CA, $CD,_
                     $90, $97, $9E, $99, $8C, $8B, $82, $85, $A8, $AF, $A6, $A1,_
                     $B4, $B3, $BA, $BD, $C7, $C0, $C9, $CE, $DB, $DC, $D5, $D2,_
                     $FF, $F8, $F1, $F6, $E3, $E4, $ED, $EA, $B7, $B0, $B9, $BE,_
                     $AB, $AC, $A5, $A2, $8F, $88, $81, $86, $93, $94, $9D, $9A,_
                     $27, $20, $29, $2E, $3B, $3C, $35, $32, $1F, $18, $11, $16,_
                     $3,  $4,  $D,  $A,  $57, $50, $59, $5E, $4B, $4C, $45, $42,_
                     $6F, $68, $61, $66, $73, $74, $7D, $7A, $89, $8E, $87, $80,_
                     $95, $92, $9B, $9C, $B1, $B6, $BF, $B8, $AD, $AA, $A3, $A4,_
                     $F9, $FE, $F7, $F0, $E5, $E2, $EB, $EC, $C1, $C6, $CF, $C8,_
                     $DD, $DA, $D3, $D4, $69, $6E, $67, $60, $75, $72, $7B, $7C,_
                     $51, $56, $5F, $58, $4D, $4A, $43, $44, $19, $1E, $17, $10,_
                     $5,  $2,  $B,  $C,  $21, $26, $2F, $28, $3D, $3A, $33, $34,_
                     $4E, $49, $40, $47, $52, $55, $5C, $5B, $76, $71, $78, $7F,_
                     $6A, $6D, $64, $63, $3E, $39, $30, $37, $22, $25, $2C, $2B,_
                     $6,  $1,  $8,  $F,  $1A, $1D, $14, $13, $AE, $A9, $A0, $A7,_
                     $B2, $B5, $BC, $BB, $96, $91, $98, $9F, $8A, $8D, $84, $83,_
                     $DE, $D9, $D0, $D7, $C2, $C5, $CC, $CB, $E6, $E1, $E8, $EF,_
                     $FA, $FD, $F4], CRC
    EndIF
Return