Rainer Raul
/
OneWireDrv
Test 1-wire , working wtih parasite power and few sensors with mixed power supply.
crc8.cpp@1:f8aa0ff8d04a, 2022-05-18 (annotated)
- Committer:
- rainerraul
- Date:
- Wed May 18 10:09:56 2022 +0000
- Revision:
- 1:f8aa0ff8d04a
- Parent:
- 0:1197076b78f4
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
macraj | 0:1197076b78f4 | 1 | #include <inttypes.h> |
macraj | 0:1197076b78f4 | 2 | |
macraj | 0:1197076b78f4 | 3 | #define CRC8INIT 0x00 |
macraj | 0:1197076b78f4 | 4 | #define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0 |
macraj | 0:1197076b78f4 | 5 | |
macraj | 0:1197076b78f4 | 6 | uint8_t crc8 ( uint8_t *data_in, uint16_t number_of_bytes_to_read ) |
macraj | 0:1197076b78f4 | 7 | { |
macraj | 0:1197076b78f4 | 8 | uint8_t crc; |
macraj | 0:1197076b78f4 | 9 | uint16_t loop_count; |
macraj | 0:1197076b78f4 | 10 | uint8_t bit_counter; |
macraj | 0:1197076b78f4 | 11 | uint8_t data; |
macraj | 0:1197076b78f4 | 12 | uint8_t feedback_bit; |
macraj | 0:1197076b78f4 | 13 | |
macraj | 0:1197076b78f4 | 14 | crc = CRC8INIT; |
macraj | 0:1197076b78f4 | 15 | |
macraj | 0:1197076b78f4 | 16 | for (loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++) |
macraj | 0:1197076b78f4 | 17 | { |
macraj | 0:1197076b78f4 | 18 | data = data_in[loop_count]; |
macraj | 0:1197076b78f4 | 19 | |
macraj | 0:1197076b78f4 | 20 | bit_counter = 8; |
macraj | 0:1197076b78f4 | 21 | do { |
macraj | 0:1197076b78f4 | 22 | feedback_bit = (crc ^ data) & 0x01; |
macraj | 0:1197076b78f4 | 23 | |
macraj | 0:1197076b78f4 | 24 | if ( feedback_bit == 0x01 ) { |
macraj | 0:1197076b78f4 | 25 | crc = crc ^ CRC8POLY; |
macraj | 0:1197076b78f4 | 26 | } |
macraj | 0:1197076b78f4 | 27 | crc = (crc >> 1) & 0x7F; |
macraj | 0:1197076b78f4 | 28 | if ( feedback_bit == 0x01 ) { |
macraj | 0:1197076b78f4 | 29 | crc = crc | 0x80; |
macraj | 0:1197076b78f4 | 30 | } |
macraj | 0:1197076b78f4 | 31 | |
macraj | 0:1197076b78f4 | 32 | data = data >> 1; |
macraj | 0:1197076b78f4 | 33 | bit_counter--; |
macraj | 0:1197076b78f4 | 34 | |
macraj | 0:1197076b78f4 | 35 | } while (bit_counter > 0); |
macraj | 0:1197076b78f4 | 36 | } |
macraj | 0:1197076b78f4 | 37 | |
macraj | 0:1197076b78f4 | 38 | return crc; |
macraj | 0:1197076b78f4 | 39 | } |