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