单片机通信中的CRC算法 点击:172 | 回复:0



xilinxue

    
发表于:2010-05-03 20:26:30
楼主
摘 要:为了提高单片机之间的通信速度,设计了一种循环冗余码校验CRC(Cyclic Redundancy Check)算法。CRC算法能在通信接口上很好地校验传输的每一个字节。
关键词:CRC算法;软件监控;通信接口;驱动电路;单播;广播ABSTRACT:In

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,0x


楼主最近还看过

SSI ļʱ