Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 363 | johboh | 1 | #include <p18cxxx.h> |
| 2 | #include <crc16.h> |
||
| 3 | |||
| 4 | // ***************************************************************************** |
||
| 5 | // Update the CRC for transmitted and received data using |
||
| 6 | // the CCITT 16bit algorithm (X^16 + X^12 + X^5 + X^0) |
||
| 7 | // |
||
| 8 | // Adapted by R Andrag from an |
||
| 9 | // asm routine by John C. Wren (google to find the original code) |
||
| 10 | // roland.andrag at gmail.com |
||
| 11 | // |
||
| 12 | // To check your crc implementation use: |
||
| 13 | // http://www.zorc.breitbandkatze.de/crc.html |
||
| 14 | // Web calculator by Sven Reifegerste |
||
| 15 | // CRC order = 16 |
||
| 16 | // CRC polynomial = 1021 hex |
||
| 17 | // Initial value = FFFF hex |
||
| 18 | // direct CRC |
||
| 19 | // Final XOR value = 0 |
||
| 20 | // Don't reverse data bytes or CRC result |
||
| 21 | // calculator allows you to enter hex bytes by prefixing % |
||
| 22 | // append 00 00 to your mesage when you calulate the crc |
||
| 23 | // i.e. crc should work out to zero on: message:00:00:crc_h:crc_l |
||
| 24 | // e.g. %01%02%00%00 gives a crc of 9c14 |
||
| 25 | // %01%02%00%00%9c%14 gives a crc of 0000 |
||
| 26 | // %01%02%9c%14 gives a crc of 9327 hex |
||
| 27 | // ***************************************************************************** |
||
| 28 | void update_crc(uint16 *p_crc, uint8 data) { |
||
| 29 | *p_crc = (*p_crc >> 8) | (*p_crc << 8); |
||
| 30 | *p_crc ^= data; |
||
| 31 | *p_crc ^= (*p_crc & 0xff) >> 4; |
||
| 32 | *p_crc ^= *p_crc << 12; |
||
| 33 | *p_crc ^= (*p_crc & 0xff) << 5; |
||
| 34 | } |
||
| 35 | |||
| 36 | |||
| 37 | uint16 calc_crc(uint8 * p_data, uint16 n_bytes) { |
||
| 38 | uint16 crc = 0xFFFF; |
||
| 39 | |||
| 40 | for (; n_bytes > 0; n_bytes--) { |
||
| 41 | update_crc(&crc, *(p_data++)); |
||
| 42 | } |
||
| 43 | |||
| 44 | return crc; |
||
| 45 | } |