Advertisement
Guest User

Untitled

a guest
Jan 24th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdint.h>
  4. #define POLY 0x1021 //0x1021 CRC16-CCITT polynomial
  5. #define CRC16STARTBIT 0xffff //value to start comparing from (zero bits must be compared as well)
  6. // This uses CRC16-CCITT with a mod so that the init value can be
  7. // specified
  8. unsigned char mess[12] ={
  9.   0x15, 0x47, 0xC3, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xFF
  10. };
  11.  
  12. unsigned short int crc_xmodem_update(uint16_t crc, uint8_t data)
  13. {
  14.   int i;
  15.  
  16.   crc = crc ^ ((uint16_t)data << 8);
  17.   for (i=0; i<8; i++)
  18.   {
  19.     if (crc & 0x8000)
  20.       crc = (crc << 1) ^ POLY; //(polynomial = 0x1021)
  21.     else
  22.       crc <<= 1;
  23.   }
  24.   return crc;
  25. }
  26.  
  27. uint16_t calc_crc(unsigned char *msg,int n,uint16_t init)
  28. {
  29.   uint16_t x = init;
  30.  
  31.   while(n--)
  32.   {
  33.     x=crc_xmodem_update(x, (uint16_t)*msg++);
  34.   }
  35.  
  36.   return(x);
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement