DIY编程器网

标题: 单片机通信中的CRC算法原理及程序设计 [打印本页]

作者: liyf    时间: 2012-1-16 16:41
标题: 单片机通信中的CRC算法原理及程序设计
1 引 言
  在单片机的近、远程通信中,为了确保传输报文(数据和信息)的正确性和加快报文的传输速度,采用CRC算法。在信道上传输的每一个字节,通过CRC算法校验,从参数表中获得其校验值,使报文无差错地快速传输。
  单片机之间有/无线载波电路进行单播(点对点)通信,或通过专用程控交换机连接单片机组成的有/无线局域网进行单播或广播(多点对多点)通信,都要实现报文的快速交换。一个最关键的问题就是要解决传输报文的误码问题。常用的方法是设计有效的硬件驱动电路和编制相应的监控软件。CRC算法不需要设计专门的硬件驱动电路,而是直接依靠设计监控软件的办法,对报文进行快速的校验来提高报文的传输速度,并保证报文的可靠传输。
  CRC算法能进行单个报文和分组报文传输的校验;在分组报文的传输中,对每一个分组报文进行校验,如果正确接收,则将此组报文存入缓冲区;否则记下此分组报文的组号,等所有的分组报文传输完后,再统计接收失败的组数,将失败的分组报文的组号传输到发送端的单片机,请求重发失败的分组报文。报文全部接收正确后,在缓冲区中合并所有的分组,形成一个完整的报文,传输给接收端的单片机,这样就能减少传输的时间,提高系统的可靠性。
  2 CRC算法的原理
  国际电报电话咨询委员会推荐的CRC—CCITT生成多项式(G(x))有多种,这里选取约定的生成多项式G(x)=x16+x12+x5+1 来制造CRC校验的参数表。CRC约定的校验规则是:让需要校验的报文代码(M(x))能为G(x)除尽。如果除得尽,表明代码正确;如果除不尽,余数(R(x))将指明出错位所在的位置。在单片机的通信中,一般要在内存中开辟缓冲区存放报文代码,涉及的单片机发送端/接收端硬件电路框图见图1。
?
  


?
  在发送端的单片机中,通过对传输报文的字节进行CRC编码,得到一个16进制的编码值,并将该值存放于传输报文的最后,一并传输给接收端的单片机。
  接收端单片机收到报文后,采用CRC校验,也得到一个16进制校验值,将该校验值和传输来的编码值进行比较,如果相等,证明传输无误,向发送端发送一个接收正确的应答信号;否则接收到的报文有误码,并向发送端发送请求重发的应答信号,也就是ARQ方式。重发的次数由程序设计者来定,但一般最好定为3次重发。重发的次数太多,则此信道长期被占用,影响别的单位和自身的通信;重发次数太少,在信道干扰太大及不稳定的情况下不能正确的接收。
  3 CRC算法程序设计
  3.1 CRC算法描述
  校验一个报文,令不同的变量存放校验值和中间结果,依次从缓冲区中取报文的每一个字节,经过运算,就可以从参数表中获得相应的校验值,直到此报文被校验完。
?
  


?
  3.2 CRC算法C语言程序清单
  CRC参数表如下:
  Const byte crcclar[512]={0x00,0x00,0x89,0x10,0x12,0x21,0x9b,0x31,0x24,0x42,0xad, 0x52,0x36,0x63,0xbf,0x73,0x48,0x84,0xc1,0x94,0x5a,0xa5,0xd3,0xb5,0x6c, 0xc6,0xe5,0xd6,0x7e,0xe7,0xf7,0xf7,0x81,0x00,0x08,0x10,0x93,0x21,0x1a, 0x31,0xa5,0x42,0x2c,0x52,0xb7,0x63,0x3e,0x73,0xc9,0x84,0x40,0x94,0xdb, 0xa5,0x52,0xb5,0xed,0xc6,0x64,0xd6,0xff,0xe7,0x76,0xf7,0x02,0x01,0x8b, 0x11,0x10,0x20,0x99,0x30,0x26,0x43,0xaf,0x53,0x34,0x62,0xbd,0 x72,0x4a,0x85,0xc3,0x95,0x58,0xa4,0xd1,0xb4,0x6e,0xc7,0xe7,0xd7,0x7c, 0xe6,0xf5,0xf6,0x83,0x01,0x0a,0x11,0x91,0x20,0x18,0x30,0 xa7,0x43,0x2e,0x53,0xb5,0x62,0x3c,0x72,0xcb,0x85,0x42,0 x95,0xd9,0xa4,0x50,0xb4,0xef,0xc7,0x66,0xd7,0 xfd,0xe6,0x74,0xf6,0x04,0x02,0x8d,0x12,0x16,0x23,0x9f,0x33,0x20,0x40, 0xa9,0x50,0x32,0x61,0xbb,0x71,0x4c,0x86,0 xc5,0x96,0x5e,0xa7,0xd7,0xb7,0x68,0xc4,0xe1,0xd4,0x7a,0xe5,0xf3,0xf5, 0x85,0x02,0x0c,0x12,0x97,0x23,0x1e,0x33,0xa1,0x40,0 x28,0x50,0xb3,0x61,0x3a,0x71,0xcd,
  0 x86,0 x44,0x96,0xdf,0xa7,0x56,0xb7,0xe9,0xc4,0x60,0xd4,0xfb,0xe5,0x72,0xf5, 0x06,0 x03,0x8f,0x13,0x14,0x22,0x9d,0x32,0x22,0x41,0xab,0x51,0x30,0x60,0xb9,0 x70,0x4e,0x87,0xc7,0x97,0x5c,0xa6,0 xd5,0xb6,0x6a,0xc5,0xe3,0xd5,0x78,  0xe4,0xf1,0xf4,0x87,0x03,0x0e,0x13, 0x95,0x22,0x1c,0x32,0xa3,0x41,0x2a,0 x51,0xb1,0x60,0x38,0x70,0xcf,0x87,0x46,0x97,0xdd,0xa6,0x54,0xb6,0xeb, 0xc5,0x62,0xd5,0xf9,0xe4,0x70,0xf4,0x08,0x04,0x81,0x14,0x1a,0x25,0 x93,0x35,0x2c,0x46,0xa5,0x56,0x3e,0x67,0xb7,0x77,0x40,0x80,0xc9,0x90, 0x52,0xa1,0xdb,0xb1,0x64,0xc2,0xed,0xd2,0x76,0xe3,0xff,0xf3,0x89,0x04, 0x00,0x14,0x9b,0x25,0x12,0x35,0xad,0x46,0x24,0x56,0xbf,0x67,0x36,0x77, 0xc1,0x80,0x48,0x90,0xd3,0xa1,0x5a,0xb1,0xe5,0xc2,0x6c,0xd2,0xf7,0xe3, 0x7e,0xf3,0x0a,0x05,0x83,0x15,0x18,0x24,0x91,0x34,0x2e,0x47,0 xa7,0x57,0x3c,0x66,0xb5,0x76,0x42,0x81,0xcb,0x91,0x50,0xa0,0 xd9,0xb0,0x66,0xc3,0xef,0xd3,0x74,0xe2,0xfd,0xf2,0x8b,0x05,0x02,0x15, 0x99,0x24,0x10,0 x34,0xaf,0x47,0x26,0x57,0xbd,0x66,0x34,0x76,0xc3,0x81,0x4a,0x91,0xd1, 0xa0,0x58,0xb0,0xe7,0xc3,0x6e,0xd3,0xf5,0xe2,0x7c,0xf2,0x0c,0x06,0x85, 0x16,0x1e,0x27,0 x97,0x37,0x28,0 x44,0xa1,0x54,0x3a,0x65,0xb3,0x75,0x44,0x82,0xcd,0x92,0x56,0xa3,0xdf, 0xb3,0 x60,0 xc0,0xe9,0xd0,0x72,0xe1,0xfb,0xf1,0x8d,0x06,0x04,0x16,0x9f,0x27,0x16, 0x37,0xa9,0x44,0 x20,0x54,0xbb,0x65,0x32,0x75,0xc5,0x82,0x4c,0x92,0xd7,0xa3,0x5e,0xb3, 0xe1,0xc0,0x68,0xd0,0 xf3,0xe1,0x7a,0xf1,0x0e,0x07,0x87,0x17,0x1c,0x26,0x95,0x36,0x2a,0x45, 0xa3,0x55,0x38,0x64,0xb1,0x74,0x46,0x83,0xcf,0x93,0x54,0xa2,0xdd,0xb2, 0x62,0xc1,0xeb,0xd1,0x70,0xe0,0xf9,0xf0,0x8f,0x07,0x06,0x17,0x9d,0x26, 0x14,0x36,0xab,0 x45,0x22,0x55,0xb9,0x64,0x30,0x74,0xc7,0x83,0x4e,0x93,0xd5,0xa2,0x5c, 0xb2,0xe3,0xc1,0x6a,0xd1,0xf1,0xe0,0x78,0xf0};
  #define bUFLEN 512  /*--缓冲区大小———*/
  #define ETb (byte)0x20 /*——报文结束符——*/
  byte crc0=0,crc1=0;  /*———字节变量———*/
  void main(void)
  { /*———根据要求编写主程序,调用crcvalue()子程序———*/ }
  word crcvalue(byte*crcbuf) /*计算报文的CRC值,crcbuf是缓冲区*/
  { word count;
  for(crc0=crc1=0,count=1;crcbuf[count]!=ETb&&count
  <bUFLEN;count++)
  crccount(crcbuf[count]);
  if(count+4>=bUFLEN)return 0;  crccount(crcbuf[count]);
  crcbuf(++count)=crc0;crcbuf(++count)=crc1;crcbuf(++
  count)=crc0;
  crcbuf(++count)=crc1;crcbuf(++count)=crc0;crcbuf(++
  count)=crc1;
  return  ++count;  }
  viod crccount(byte crc100) /*计算一个字节的CRC值*/
  {  byte  crc10,crc11,crc20,crc21;  word crclen;
  crclen=(word)crc100; crclen=(crclen&0xff)<<1; crc10=crcclar
  [crclen++];
  crc11=crcclar[crclen++]; crc11=crc100^crc0^crc11;
  crc10=crc1 ^crc10;
  crclen=(word)crc0; crclen=(crclen&0xff)<<1; crc20=
  crcclar[crclen++];
  crc21=crcclar[crclen++]; crc0=drd^crc20; crc1=crc11^
  crc21;  }
?
  4 CRC算法的优缺点
  用软件实现的CRC算法,其主要优点是突出了一个“快”字,为了提高校验速度,把参数表和应用程序一起写入单片机的EPROM内,当进行CRC校验时,对需要发送的每一字节,按上述的方法进行计算,就可得到该字节的校验值。从而提高了速度,较好地克服了异步传输中校验和发送时间很不匹配的矛盾。
  CRC算法的缺点是由于要存储512字节的参数表,需要更多的存储空间,但是在单片机的应用中,这种以空间换取时间的方法是值得提倡的。
  5 结束语
  CRC算法能很好地解决传输报文过程中的校验问题,在8031,80C196,80188等CPU控制的单片机中,进行了大量的有/无线传输报文实验,在300bPS,600bPS,1200bPS,2400bPS,4800bPS的波特率下,CRC算法都能很好地进行校验,提高了传输速度。
  [参考文献]
  [1] 白驹珩,等.单片机及其应用[M].电子科技大学出版社,1994.5.
  [2] 刘乐善,等.微型计算机接口技术及应用[M].华中理工大学出版社,1994.3.
  [3] 俸远祯,等.计算机组成原理[M].电子工业出版社,1996.8.
  [4] Herbert Schildt.最新C语言大全[M].中科院希望高级电脑技术公司,1991.5.
                          
                       
                          
                               




欢迎光临 DIY编程器网 (http://diybcq.com/) Powered by Discuz! X3.2