MAX31850, DS18B20, DS2450, thermocouple
crc8.cpp@0:5d39f2521173, 2015-03-09 (annotated)
- Committer:
- fblanc
- Date:
- Mon Mar 09 11:55:54 2015 +0000
- Revision:
- 0:5d39f2521173
MAX31850
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fblanc | 0:5d39f2521173 | 1 | #include <inttypes.h> |
fblanc | 0:5d39f2521173 | 2 | |
fblanc | 0:5d39f2521173 | 3 | #define CRC8INIT 0x00 |
fblanc | 0:5d39f2521173 | 4 | #define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0 |
fblanc | 0:5d39f2521173 | 5 | |
fblanc | 0:5d39f2521173 | 6 | uint8_t crc8 ( uint8_t *data_in, uint16_t number_of_bytes_to_read ) |
fblanc | 0:5d39f2521173 | 7 | { |
fblanc | 0:5d39f2521173 | 8 | uint8_t crc; |
fblanc | 0:5d39f2521173 | 9 | uint16_t loop_count; |
fblanc | 0:5d39f2521173 | 10 | uint8_t bit_counter; |
fblanc | 0:5d39f2521173 | 11 | uint8_t data; |
fblanc | 0:5d39f2521173 | 12 | uint8_t feedback_bit; |
fblanc | 0:5d39f2521173 | 13 | |
fblanc | 0:5d39f2521173 | 14 | crc = CRC8INIT; |
fblanc | 0:5d39f2521173 | 15 | |
fblanc | 0:5d39f2521173 | 16 | for (loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++) |
fblanc | 0:5d39f2521173 | 17 | { |
fblanc | 0:5d39f2521173 | 18 | data = data_in[loop_count]; |
fblanc | 0:5d39f2521173 | 19 | |
fblanc | 0:5d39f2521173 | 20 | bit_counter = 8; |
fblanc | 0:5d39f2521173 | 21 | do { |
fblanc | 0:5d39f2521173 | 22 | feedback_bit = (crc ^ data) & 0x01; |
fblanc | 0:5d39f2521173 | 23 | |
fblanc | 0:5d39f2521173 | 24 | if ( feedback_bit == 0x01 ) { |
fblanc | 0:5d39f2521173 | 25 | crc = crc ^ CRC8POLY; |
fblanc | 0:5d39f2521173 | 26 | } |
fblanc | 0:5d39f2521173 | 27 | crc = (crc >> 1) & 0x7F; |
fblanc | 0:5d39f2521173 | 28 | if ( feedback_bit == 0x01 ) { |
fblanc | 0:5d39f2521173 | 29 | crc = crc | 0x80; |
fblanc | 0:5d39f2521173 | 30 | } |
fblanc | 0:5d39f2521173 | 31 | |
fblanc | 0:5d39f2521173 | 32 | data = data >> 1; |
fblanc | 0:5d39f2521173 | 33 | bit_counter--; |
fblanc | 0:5d39f2521173 | 34 | |
fblanc | 0:5d39f2521173 | 35 | } while (bit_counter > 0); |
fblanc | 0:5d39f2521173 | 36 | } |
fblanc | 0:5d39f2521173 | 37 | |
fblanc | 0:5d39f2521173 | 38 | return crc; |
fblanc | 0:5d39f2521173 | 39 | } |