打印

CRC-16,有谁做过crc-16,基于多项式x16+x12+x2+1的C语言实现

[复制链接]
8240|18
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
guanshan237|  楼主 | 2012-10-22 16:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如题,CRC-16,有谁做过crc-16,基于多项式x16+x12+x2+1的,有没有C语言实现过的?
CRC好多多项式,有没有哪位大侠做过基于多项式X16+X12+X2+1的CRC校验??

相关帖子

沙发
阿南| | 2012-10-22 16:50 | 只看该作者
没试过,帮顶

使用特权

评论回复
板凳
john_lee| | 2012-10-23 00:10 | 只看该作者
楼主这个多项式还真是没见过,wikipedia 上关于crc16的只有下面几种多项式:
8005,X16+X15+X2+1
1021,X16+X12+X5+1
8BB7,X16+X15+X11+X9+X8+X7+X5+X4+X2+X1+1
3D65,X16+X13+X12+X11+X10+X8+X6+X5+X2+1
0589,X16+X10+X8+X7+X3+1
A02B,X16+X15+X13+X5+X3+X1+1
而没有楼主的多项式1005,X16+X12+X2+1,楼主莫不是写错了?

使用特权

评论回复
地板
guanshan237|  楼主 | 2012-10-23 09:14 | 只看该作者
没有啊,就这样的多项式X16+X12+X2+1,理论上讲,应该把多项式值比如由A001变成8005,或者1005,即可完成吧,但串口打印出来值确不对啊!!! 3# john_lee

使用特权

评论回复
5
jlass| | 2012-10-23 09:20 | 只看该作者
CRC-16直接用查表法,简单又好用,我一直用它
算法我用汇编(DSP 5416的)做过,网上应该有参考。
CRC-16的算法确实有很多种,所以还是建议用查表法

使用特权

评论回复
6
guanshan237|  楼主 | 2012-10-23 09:57 | 只看该作者
你都做过哪些多项式的? 5# jlass

使用特权

评论回复
7
jlass| | 2012-10-24 09:29 | 只看该作者
HDLC的CRC-16校检,具体是哪种我也没去查过

使用特权

评论回复
8
john_lee| | 2012-10-25 09:17 | 只看该作者
HDLC用的CRC16生成多项式是1021的,就是所谓的CRC16-CCITT,这个多项式用得非常广泛:X.25, V.41, HDLC, XMODEM, Bluetooth, PACTOR, SD等等。

使用特权

评论回复
9
谈的元| | 2012-12-11 22:54 | 只看该作者
记号

使用特权

评论回复
10
xudaofu| | 2012-12-12 11:07 | 只看该作者
if(frame_LC%2!=0)
        {
          for(i=0;i<(frame_LC+1)/2;i++)
          {
            crc^=(Crc_DATA[i]<<16);
            for(j=0;j<16;j++)
            {
              if(crc&0x80000000)
              {
                crc<<=1;
                crc^=0x10210000;
              }
              else
               crc<<=1;
            }
          }
          crc>>=16;
        }
        else
        {
          for(i=0;i<frame_LC/2;i++)
          {
            crc^=(Crc_DATA[i]<<16);
            for(j=0;j<16;j++)
            {
              if(crc&0x80000000)
              {
                crc<<=1;
                crc^=0x10210000;
              }
              else
               crc<<=1;
            }
          }
          crc>>=16;
        }
其中frame_LC=数据字节数+SAD字节数+CMD字节数+CRC字节数,CRC_DATA为参与CRC计算的数据,算法没问题,一直在用。

使用特权

评论回复
11
spillar| | 2012-12-13 16:49 | 只看该作者
#define  CRC_QUANTIC 0xA001
uint16 Calculate_CRC(uint8 *Buff,uint8 len)       
{
    uint16 Crc = 0xFFFF;
        uint8  i   = 0;
        uint8  j   = 0;
        for(i=0;i<len;i++)
        {
           Crc = Crc^(*Buff);
           Buff++ ;
           for(j=0;j<8;j++)
           {
                     if((Crc&0x0001)== 1)
              {
                         Crc = Crc>>1;
                      Crc = Crc^CRC_QUANTIC;
              }
              else
              {
                         Crc = Crc >> 1;
              }
           }          
        }
    return(Crc);
}

这个是直接计算的,比较占用CPU时间,还有查表方式的。

使用特权

评论回复
12
spillar| | 2012-12-13 16:50 | 只看该作者
uint16 Calculate_CRC16(uint8 *Buff,uint16 Len)
{   
    uint16 CRC,k;
    uint8 CRC_High,CRC_Low;
    uint8 j;
    uint8 New_CRC_High,New_CRC_Low;
    uint8 CRC_index;
        uint16 i;
        CRC_High=0xFF;
        CRC_Low=0xFF;
        k=Len;
        for(i=0;i<k;i++)
        {
          j=*Buff++;
          CRC_index=CRC_Low^(j);
          New_CRC_High=CRC16_HIGH[CRC_index];
          New_CRC_Low=CRC16_LOW[CRC_index]^CRC_High;
          CRC_High=New_CRC_High;
          CRC_Low=New_CRC_Low;
    }
    CRC=(((uint16)CRC_High)<<8)|((uint16)CRC_Low);
    return(CRC);
}

查表方式的,占用ROM空间,但速度较快。

使用特权

评论回复
13
spillar| | 2012-12-13 16:51 | 只看该作者
const uint8 CRC16_LOW[256]= {0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,
    0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,
    0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,
    0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,
    0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,
    0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,
    0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,
    0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,
    0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,
    0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,
    0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,
    0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,
    0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40,0x00,0xc1,0x81,0x40,0x01,0xc0,0x80,0x41,0x00,0xc1,
    0x81,0x40,0x01,0xc0,0x80,0x41,0x01,0xc0,0x80,0x41,0x00,0xc1,0x81,0x40};
   
   
const uint8 CRC16_HIGH[256]={0x00,0xc0,0xc1,0x01,0xc3,0x03,0x02,0xc2,0xc6,0x06,0x07,0xc7,0x05,0xc5,
    0xc4,0x04,0xcc,0x0c,0x0d,0xcd,0x0f,0xcf,0xce,0x0e,0x0a,0xca,0xcb,0x0b,0xc9,0x09,0x08,0xc8,0xd8,
    0x18,0x19,0xd9,0x1b,0xdb,0xda,0x1a,0x1e,0xde,0xdf,0x1f,0xdd,0x1d,0x1c,0xdc,0x14,0xd4,0xd5,0x15,
    0xd7,0x17,0x16,0xd6,0xd2,0x12,0x13,0xd3,0x11,0xd1,0xd0,0x10,0xf0,0x30,0x31,0xf1,0x33,0xf3,0xf2,
    0x32,0x36,0xf6,0xf7,0x37,0xf5,0x35,0x34,0xf4,0x3c,0xfc,0xfd,0x3d,0xff,0x3f,0x3e,0xfe,0xfa,0x3a,
    0x3b,0xfb,0x39,0xf9,0xf8,0x38,0x28,0xe8,0xe9,0x29,0xeb,0x2b,0x2a,0xea,0xee,0x2e,0x2f,0xef,0x2d,
    0xed,0xec,0x2c,0xe4,0x24,0x25,0xe5,0x27,0xe7,0xe6,0x26,0x22,0xe2,0xe3,0x23,0xe1,0x21,0x20,0xe0,
        0xa0,0x60,0x61,0xa1,0x63,0xa3,0xa2,0x62,0x66,0xa6,0xa7,0x67,0xa5,0x65,0x64,0xa4,0x6c,0xac,0xad,
        0x6d,0xaf,0x6f,0x6e,0xae,0xaa,0x6a,0x6b,0xab,0x69,0xa9,0xa8,0x68,0x78,0xb8,0xb9,0x79,0xbb,0x7b,
        0x7a,0xba,0xbe,0x7e,0x7f,0xbf,0x7d,0xbd,0xbc,0x7c,0xb4,0x74,0x75,0xb5,0x77,0xb7,0xb6,0x76,0x72,
        0xb2,0xb3,0x73,0xb1,0x71,0x70,0xb0,0x50,0x90,0x91,0x51,0x93,0x53,0x52,0x92,0x96,0x56,0x57,0x97,
        0x55,0x95,0x94,0x54,0x9c,0x5c,0x5d,0x9d,0x5f,0x9f,0x9e,0x5e,0x5a,0x9a,0x9b,0x5b,0x99,0x59,0x58,
        0x98,0x88,0x48,0x49,0x89,0x4b,0x8b,0x8a,0x4a,0x4e,0x8e,0x8f,0x4f,0x8d,0x4d,0x4c,0x8c,0x44,0x84,
        0x85,0x45,0x87,0x47,0x46,0x86,0x82,0x42,0x43,0x83,0x41,0x81,0x80,0x40};
CRC16数据表,多项式 A001

使用特权

评论回复
14
hotpower| | 2015-9-26 16:52 | 只看该作者
http://www.21ic.com/tools/HotWC3_V1.11.html写入表达式,选取表格和C语言,即可自动生成

使用特权

评论回复
15
hotpower| | 2015-9-27 11:28 | 只看该作者
我用手机做一个

使用特权

评论回复
16
hotpower| | 2015-9-27 11:31 | 只看该作者
多项式的简写式为CRCL16_1005_FFFF_0000

使用特权

评论回复
17
hotpower| | 2015-9-27 11:33 | 只看该作者
//CRC位域8表(大端): const unsigned int CRCL16_1005_Table[256] = {   0x0000,0x1005,0x200A,0x300F,0x4014,0x5011,0x601E,0x701B,0x8028,0x902D,0xA022,0xB027,0xC03C,0xD039,0xE036,0xF033,   0x1055,0x0050,0x305F,0x205A,0x5041,0x4044,0x704B,0x604E,0x907D,0x8078,0xB077,0xA072,0xD069,0xC06C,0xF063,0xE066,   0x20AA,0x30AF,0x00A0,0x10A5,0x60BE,0x70BB,0x40B4,0x50B1,0xA082,0xB087,0x8088,0x908D,0xE096,0xF093,0xC09C,0xD099,   0x30FF,0x20FA,0x10F5,0x00F0,0x70EB,0x60EE,0x50E1,0x40E4,0xB0D7,0xA0D2,0x90DD,0x80D8,0xF0C3,0xE0C6,0xD0C9,0xC0CC,   0x4154,0x5151,0x615E,0x715B,0x0140,0x1145,0x214A,0x314F,0xC17C,0xD179,0xE176,0xF173,0x8168,0x916D,0xA162,0xB167,   0x5101,0x4104,0x710B,0x610E,0x1115,0x0110,0x311F,0x211A,0xD129,0xC12C,0xF123,0xE126,0x913D,0x8138,0xB137,0xA132,   0x61FE,0x71FB,0x41F4,0x51F1,0x21EA,0x31EF,0x01E0,0x11E5,0xE1D6,0xF1D3,0xC1DC,0xD1D9,0xA1C2,0xB1C7,0x81C8,0x91CD,   0x71AB,0x61AE,0x51A1,0x41A4,0x31BF,0x21BA,0x11B5,0x01B0,0xF183,0xE186,0xD189,0xC18C,0xB197,0xA192,0x919D,0x8198,   0x82A8,0x92AD,0xA2A2,0xB2A7,0xC2BC,0xD2B9,0xE2B6,0xF2B3,0x0280,0x1285,0x228A,0x328F,0x4294,0x5291,0x629E,0x729B,   0x92FD,0x82F8,0xB2F7,0xA2F2,0xD2E9,0xC2EC,0xF2E3,0xE2E6,0x12D5,0x02D0,0x32DF,0x22DA,0x52C1,0x42C4,0x72CB,0x62CE,   0xA202,0xB207,0x8208,0x920D,0xE216,0xF213,0xC21C,0xD219,0x222A,0x322F,0x0220,0x1225,0x623E,0x723B,0x4234,0x5231,   0xB257,0xA252,0x925D,0x8258,0xF243,0xE246,0xD249,0xC24C,0x327F,0x227A,0x1275,0x0270,0x726B,0x626E,0x5261,0x4264,   0xC3FC,0xD3F9,0xE3F6,0xF3F3,0x83E8,0x93ED,0xA3E2,0xB3E7,0x43D4,0x53D1,0x63DE,0x73DB,0x03C0,0x13C5,0x23CA,0x33CF,   0xD3A9,0xC3AC,0xF3A3,0xE3A6,0x93BD,0x83B8,0xB3B7,0xA3B2,0x5381,0x4384,0x738B,0x638E,0x1395,0x0390,0x339F,0x239A,   0xE356,0xF353,0xC35C,0xD359,0xA342,0xB347,0x8348,0x934D,0x637E,0x737B,0x4374,0x5371,0x236A,0x336F,0x0360,0x1365,   0xF303,0xE306,0xD309,0xC30C,0xB317,0xA312,0x931D,0x8318,0x732B,0x632E,0x5321,0x4324,0x333F,0x233A,0x1335,0x0330 }; //CRC位域8查表算法(大端): unsigned int GetCRCL16_1005_FFFF_0000_Table(unsigned char *crcBuff, unsigned char crcLen) { unsigned char i; unsigned int crc = 0xFFFF;   for(i = 0; i < crcLen; i ++) {     crc = (crc << 8) ^ CRCL16_1005_Table[(crc >> 8) ^ crcBuff[i]];   }   return crc; } void main() { const unsigned char dataTable[] = "\x01\x23\x45\x67\x89\xAB\xCD\xEF"; unsigned int crc;   crc = GetCRCL16_1005_FFFF_0000_Table((unsigned char *)dataTable, sizeof(dataTable) - 1);   while(1); }

使用特权

评论回复
18
hotpower| | 2015-9-27 11:36 | 只看该作者
简写式为CRCR16_A008_FFFF_0000

使用特权

评论回复
19
hotpower| | 2015-9-27 11:37 | 只看该作者
//CRC位域8表(小端): const unsigned int CRCR16_A008_Table[256] = {   0x0000,0x1541,0x2A82,0x3FC3,0x5504,0x4045,0x7F86,0x6AC7,0xAA08,0xBF49,0x808A,0x95CB,0xFF0C,0xEA4D,0xD58E,0xC0CF,   0x1401,0x0140,0x3E83,0x2BC2,0x4105,0x5444,0x6B87,0x7EC6,0xBE09,0xAB48,0x948B,0x81CA,0xEB0D,0xFE4C,0xC18F,0xD4CE,   0x2802,0x3D43,0x0280,0x17C1,0x7D06,0x6847,0x5784,0x42C5,0x820A,0x974B,0xA888,0xBDC9,0xD70E,0xC24F,0xFD8C,0xE8CD,   0x3C03,0x2942,0x1681,0x03C0,0x6907,0x7C46,0x4385,0x56C4,0x960B,0x834A,0xBC89,0xA9C8,0xC30F,0xD64E,0xE98D,0xFCCC,   0x5004,0x4545,0x7A86,0x6FC7,0x0500,0x1041,0x2F82,0x3AC3,0xFA0C,0xEF4D,0xD08E,0xC5CF,0xAF08,0xBA49,0x858A,0x90CB,   0x4405,0x5144,0x6E87,0x7BC6,0x1101,0x0440,0x3B83,0x2EC2,0xEE0D,0xFB4C,0xC48F,0xD1CE,0xBB09,0xAE48,0x918B,0x84CA,   0x7806,0x6D47,0x5284,0x47C5,0x2D02,0x3843,0x0780,0x12C1,0xD20E,0xC74F,0xF88C,0xEDCD,0x870A,0x924B,0xAD88,0xB8C9,   0x6C07,0x7946,0x4685,0x53C4,0x3903,0x2C42,0x1381,0x06C0,0xC60F,0xD34E,0xEC8D,0xF9CC,0x930B,0x864A,0xB989,0xACC8,   0xA008,0xB549,0x8A8A,0x9FCB,0xF50C,0xE04D,0xDF8E,0xCACF,0x0A00,0x1F41,0x2082,0x35C3,0x5F04,0x4A45,0x7586,0x60C7,   0xB409,0xA148,0x9E8B,0x8BCA,0xE10D,0xF44C,0xCB8F,0xDECE,0x1E01,0x0B40,0x3483,0x21C2,0x4B05,0x5E44,0x6187,0x74C6,   0x880A,0x9D4B,0xA288,0xB7C9,0xDD0E,0xC84F,0xF78C,0xE2CD,0x2202,0x3743,0x0880,0x1DC1,0x7706,0x6247,0x5D84,0x48C5,   0x9C0B,0x894A,0xB689,0xA3C8,0xC90F,0xDC4E,0xE38D,0xF6CC,0x3603,0x2342,0x1C81,0x09C0,0x6307,0x7646,0x4985,0x5CC4,   0xF00C,0xE54D,0xDA8E,0xCFCF,0xA508,0xB049,0x8F8A,0x9ACB,0x5A04,0x4F45,0x7086,0x65C7,0x0F00,0x1A41,0x2582,0x30C3,   0xE40D,0xF14C,0xCE8F,0xDBCE,0xB109,0xA448,0x9B8B,0x8ECA,0x4E05,0x5B44,0x6487,0x71C6,0x1B01,0x0E40,0x3183,0x24C2,   0xD80E,0xCD4F,0xF28C,0xE7CD,0x8D0A,0x984B,0xA788,0xB2C9,0x7206,0x6747,0x5884,0x4DC5,0x2702,0x3243,0x0D80,0x18C1,   0xCC0F,0xD94E,0xE68D,0xF3CC,0x990B,0x8C4A,0xB389,0xA6C8,0x6607,0x7346,0x4C85,0x59C4,0x3303,0x2642,0x1981,0x0CC0 }; //CRC位域8查表算法(小端): unsigned int GetCRCR16_A008_FFFF_0000_Table(unsigned char *crcBuff, unsigned char crcLen) { unsigned char i; unsigned int crc = 0xFFFF;   for(i = 0; i < crcLen; i ++) {     crc = (crc >> 8) ^ CRCR16_A008_Table[(crc & 0xFF) ^ crcBuff[i]];   }   return crc; } void main() { const unsigned char dataTable[] = "\x01\x23\x45\x67\x89\xAB\xCD\xEF"; unsigned int crc;   crc = GetCRCR16_A008_FFFF_0000_Table((unsigned char *)dataTable, sizeof(dataTable) - 1);   while(1); }

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

38

帖子

1

粉丝