Simple cpp wrapper of a ds18b20, onewire 'c' library. Supports multiple sensors.

Dependencies:   mbed

Dependents:   LPC11U68_DS18B20Sensor

Fork of DS18B20Sensor by Steve Spence

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers crc8.cpp Source File

crc8.cpp

00001 #include <inttypes.h>
00002 
00003 #define CRC8INIT    0x00
00004 #define CRC8POLY    0x18              //0X18 = X^8+X^5+X^4+X^0
00005 
00006 uint8_t    crc8 ( uint8_t *data_in, uint16_t number_of_bytes_to_read )
00007 {
00008     uint8_t     crc;
00009     uint16_t loop_count;
00010     uint8_t  bit_counter;
00011     uint8_t  data;
00012     uint8_t  feedback_bit;
00013     
00014     crc = CRC8INIT;
00015 
00016     for (loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++)
00017     {
00018         data = data_in[loop_count];
00019         
00020         bit_counter = 8;
00021         do {
00022             feedback_bit = (crc ^ data) & 0x01;
00023     
00024             if ( feedback_bit == 0x01 ) {
00025                 crc = crc ^ CRC8POLY;
00026             }
00027             crc = (crc >> 1) & 0x7F;
00028             if ( feedback_bit == 0x01 ) {
00029                 crc = crc | 0x80;
00030             }
00031         
00032             data = data >> 1;
00033             bit_counter--;
00034         
00035         } while (bit_counter > 0);
00036     }
00037     
00038     return crc;
00039 }