MAX31850, DS18B20, DS2450, thermocouple
crc16.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 | #include "mbed.h" |
fblanc | 0:5d39f2521173 | 3 | #include "onewire.h" |
fblanc | 0:5d39f2521173 | 4 | |
fblanc | 0:5d39f2521173 | 5 | #define CRC16INIT 0x0000 |
fblanc | 0:5d39f2521173 | 6 | //#define CRC16POLY 0x8005; // Polynome =x^16 + x^15 + x^2 + x^0 = 0x18005 |
fblanc | 0:5d39f2521173 | 7 | #define CRC16POLY 0xA001; |
fblanc | 0:5d39f2521173 | 8 | |
fblanc | 0:5d39f2521173 | 9 | uint16_t crc16(uint8_t* octets, uint16_t nboctets) |
fblanc | 0:5d39f2521173 | 10 | { |
fblanc | 0:5d39f2521173 | 11 | uint16_t crc = CRC16INIT; |
fblanc | 0:5d39f2521173 | 12 | int i, done = 0; |
fblanc | 0:5d39f2521173 | 13 | uint8_t todo; |
fblanc | 0:5d39f2521173 | 14 | if (nboctets != 0) { |
fblanc | 0:5d39f2521173 | 15 | do { |
fblanc | 0:5d39f2521173 | 16 | todo = octets[done]; |
fblanc | 0:5d39f2521173 | 17 | crc ^= todo; |
fblanc | 0:5d39f2521173 | 18 | for (i = 0; i < 8; i++) { |
fblanc | 0:5d39f2521173 | 19 | if (crc % 2 != 0) { |
fblanc | 0:5d39f2521173 | 20 | crc = (crc >> 1) ^ CRC16POLY; |
fblanc | 0:5d39f2521173 | 21 | } else { |
fblanc | 0:5d39f2521173 | 22 | crc = crc >> 1; |
fblanc | 0:5d39f2521173 | 23 | } |
fblanc | 0:5d39f2521173 | 24 | } |
fblanc | 0:5d39f2521173 | 25 | done++; |
fblanc | 0:5d39f2521173 | 26 | } while (done < nboctets); |
fblanc | 0:5d39f2521173 | 27 | |
fblanc | 0:5d39f2521173 | 28 | |
fblanc | 0:5d39f2521173 | 29 | } |
fblanc | 0:5d39f2521173 | 30 | |
fblanc | 0:5d39f2521173 | 31 | return crc; |
fblanc | 0:5d39f2521173 | 32 | } |
fblanc | 0:5d39f2521173 | 33 | //CRC16 byte, always two bytes, bit inverted, LSByte first |
fblanc | 0:5d39f2521173 | 34 | uint8_t ctrl_crc16(uint8_t* octets, uint16_t nboctets) |
fblanc | 0:5d39f2521173 | 35 | { |
fblanc | 0:5d39f2521173 | 36 | uint16_t crc; |
fblanc | 0:5d39f2521173 | 37 | uint8_t *ptr; |
fblanc | 0:5d39f2521173 | 38 | #ifdef DEBUG |
fblanc | 0:5d39f2521173 | 39 | printf( "\nCRC16 : " ); |
fblanc | 0:5d39f2521173 | 40 | for ( uint8_t i=0 ; i< nboctets; i++ ) |
fblanc | 0:5d39f2521173 | 41 | printf(":%2.2X",octets[i]); |
fblanc | 0:5d39f2521173 | 42 | printf( "\n" ); |
fblanc | 0:5d39f2521173 | 43 | #endif |
fblanc | 0:5d39f2521173 | 44 | crc =~crc16(octets, nboctets-2); |
fblanc | 0:5d39f2521173 | 45 | ptr=(uint8_t*)&crc; |
fblanc | 0:5d39f2521173 | 46 | #ifdef DEBUG |
fblanc | 0:5d39f2521173 | 47 | printf( "\n" ); |
fblanc | 0:5d39f2521173 | 48 | printf("CRC16:%X",crc); |
fblanc | 0:5d39f2521173 | 49 | printf( "\n" ); |
fblanc | 0:5d39f2521173 | 50 | #endif |
fblanc | 0:5d39f2521173 | 51 | if(*ptr==octets[nboctets-2]) |
fblanc | 0:5d39f2521173 | 52 | if(*++ptr==octets[nboctets-1]) |
fblanc | 0:5d39f2521173 | 53 | return 0; |
fblanc | 0:5d39f2521173 | 54 | |
fblanc | 0:5d39f2521173 | 55 | return 1; |
fblanc | 0:5d39f2521173 | 56 | } |