Mercurial > templog
comparison crc8.c @ 315:7d409dded901
Import ds18x20 code
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 18 May 2012 23:57:08 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
314:7aebb1452422 | 315:7d409dded901 |
---|---|
1 /* please read copyright-notice at EOF */ | |
2 | |
3 #include <stdint.h> | |
4 | |
5 #define CRC8INIT 0x00 | |
6 #define CRC8POLY 0x18 //0X18 = X^8+X^5+X^4+X^0 | |
7 | |
8 uint8_t crc8( uint8_t *data, uint16_t number_of_bytes_in_data ) | |
9 { | |
10 uint8_t crc; | |
11 uint16_t loop_count; | |
12 uint8_t bit_counter; | |
13 uint8_t b; | |
14 uint8_t feedback_bit; | |
15 | |
16 crc = CRC8INIT; | |
17 | |
18 for (loop_count = 0; loop_count != number_of_bytes_in_data; loop_count++) | |
19 { | |
20 b = data[loop_count]; | |
21 | |
22 bit_counter = 8; | |
23 do { | |
24 feedback_bit = (crc ^ b) & 0x01; | |
25 | |
26 if ( feedback_bit == 0x01 ) { | |
27 crc = crc ^ CRC8POLY; | |
28 } | |
29 crc = (crc >> 1) & 0x7F; | |
30 if ( feedback_bit == 0x01 ) { | |
31 crc = crc | 0x80; | |
32 } | |
33 | |
34 b = b >> 1; | |
35 bit_counter--; | |
36 | |
37 } while (bit_counter > 0); | |
38 } | |
39 | |
40 return crc; | |
41 } | |
42 | |
43 /* | |
44 This code is from Colin O'Flynn - Copyright (c) 2002 | |
45 only minor changes by M.Thomas 9/2004 | |
46 | |
47 Permission is hereby granted, free of charge, to any person obtaining a copy of | |
48 this software and associated documentation files (the "Software"), to deal in | |
49 the Software without restriction, including without limitation the rights to | |
50 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
51 the Software, and to permit persons to whom the Software is furnished to do so, | |
52 subject to the following conditions: | |
53 | |
54 The above copyright notice and this permission notice shall be included in all | |
55 copies or substantial portions of the Software. | |
56 | |
57 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
58 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
59 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
60 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
61 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
62 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
63 */ |