Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DS18B20Sensor DS18B201
crc8.cpp@3:9fd95d590149, 2013-03-03 (annotated)
- Committer:
- jsteve
- Date:
- Sun Mar 03 02:22:27 2013 +0000
- Revision:
- 3:9fd95d590149
- Parent:
- 0:1449f126b241
Updated documentation.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jsteve | 0:1449f126b241 | 1 | #include <inttypes.h> |
| jsteve | 0:1449f126b241 | 2 | |
| jsteve | 0:1449f126b241 | 3 | #define CRC8INIT 0x00 |
| jsteve | 0:1449f126b241 | 4 | #define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0 |
| jsteve | 0:1449f126b241 | 5 | |
| jsteve | 0:1449f126b241 | 6 | uint8_t crc8 ( uint8_t *data_in, uint16_t number_of_bytes_to_read ) |
| jsteve | 0:1449f126b241 | 7 | { |
| jsteve | 0:1449f126b241 | 8 | uint8_t crc; |
| jsteve | 0:1449f126b241 | 9 | uint16_t loop_count; |
| jsteve | 0:1449f126b241 | 10 | uint8_t bit_counter; |
| jsteve | 0:1449f126b241 | 11 | uint8_t data; |
| jsteve | 0:1449f126b241 | 12 | uint8_t feedback_bit; |
| jsteve | 0:1449f126b241 | 13 | |
| jsteve | 0:1449f126b241 | 14 | crc = CRC8INIT; |
| jsteve | 0:1449f126b241 | 15 | |
| jsteve | 0:1449f126b241 | 16 | for (loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++) |
| jsteve | 0:1449f126b241 | 17 | { |
| jsteve | 0:1449f126b241 | 18 | data = data_in[loop_count]; |
| jsteve | 0:1449f126b241 | 19 | |
| jsteve | 0:1449f126b241 | 20 | bit_counter = 8; |
| jsteve | 0:1449f126b241 | 21 | do { |
| jsteve | 0:1449f126b241 | 22 | feedback_bit = (crc ^ data) & 0x01; |
| jsteve | 0:1449f126b241 | 23 | |
| jsteve | 0:1449f126b241 | 24 | if ( feedback_bit == 0x01 ) { |
| jsteve | 0:1449f126b241 | 25 | crc = crc ^ CRC8POLY; |
| jsteve | 0:1449f126b241 | 26 | } |
| jsteve | 0:1449f126b241 | 27 | crc = (crc >> 1) & 0x7F; |
| jsteve | 0:1449f126b241 | 28 | if ( feedback_bit == 0x01 ) { |
| jsteve | 0:1449f126b241 | 29 | crc = crc | 0x80; |
| jsteve | 0:1449f126b241 | 30 | } |
| jsteve | 0:1449f126b241 | 31 | |
| jsteve | 0:1449f126b241 | 32 | data = data >> 1; |
| jsteve | 0:1449f126b241 | 33 | bit_counter--; |
| jsteve | 0:1449f126b241 | 34 | |
| jsteve | 0:1449f126b241 | 35 | } while (bit_counter > 0); |
| jsteve | 0:1449f126b241 | 36 | } |
| jsteve | 0:1449f126b241 | 37 | |
| jsteve | 0:1449f126b241 | 38 | return crc; |
| jsteve | 0:1449f126b241 | 39 | } |