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 crc16.cpp Source File

crc16.cpp

00001 #include <inttypes.h>
00002 #include "mbed.h"
00003 #include "onewire.h"
00004 
00005 #define CRC16INIT    0x0000
00006 //#define CRC16POLY    0x8005; // Polynome =x^16 + x^15 + x^2 + x^0 = 0x18005
00007 #define CRC16POLY    0xA001;
00008 
00009 uint16_t crc16(uint8_t* octets, uint16_t nboctets)
00010 {
00011 uint16_t crc = CRC16INIT;
00012 int i, done = 0;
00013 uint8_t todo;
00014     if (nboctets != 0) {
00015         do {
00016             todo = octets[done];
00017             crc ^= todo;
00018             for (i = 0; i < 8; i++) {
00019                 if (crc % 2 != 0) {
00020                     crc = (crc >> 1) ^ CRC16POLY;
00021                 } else {
00022                     crc = crc >> 1;
00023                 }
00024             }
00025             done++;
00026         } while (done < nboctets);
00027         
00028         
00029     }
00030 
00031 return crc;
00032 }
00033 //CRC16 byte, always two bytes, bit inverted, LSByte first 
00034 uint8_t ctrl_crc16(uint8_t* octets, uint16_t nboctets)
00035 {
00036     uint16_t crc;
00037     uint8_t *ptr;
00038 #ifdef DEBUG
00039     printf( "\nCRC16 : " );
00040     for ( uint8_t i=0 ; i< nboctets; i++ )
00041         printf(":%2.2X",octets[i]);
00042     printf( "\n" );
00043 #endif
00044     crc =~crc16(octets, nboctets-2);
00045     ptr=(uint8_t*)&crc;
00046 #ifdef DEBUG
00047     printf( "\n" );
00048     printf("CRC16:%X",crc);
00049     printf( "\n" );
00050 #endif
00051     if(*ptr==octets[nboctets-2])
00052         if(*++ptr==octets[nboctets-1])
00053             return 0;
00054 
00055     return 1;
00056  }