原创 单片机通信中的CRC算法

2009-8-13 18:56 2393 6 6 分类: MCU/ 嵌入式

 :为了提高单片机之间的通信速度,设计了一种循环冗余码校验CRC(Cyclic Redundancy Check)算法。CRC算法能在通信接口上很好地校验传输的每一个字节。电子园51单片机学习网 | |:D8oN1~Z'W1g
关键词:CRC算法;软件监控;通信接口;驱动电路;单播;广播ABSTRACT:In 

Tw;s@,gJh44606 电子园51单片机学习网5O,TKPHZv!^

1 引 言
_ y7zp2o-e$IE4[44606   
在单片机的近、远程通信中,为了确保传输报文(数据和信息)的正确性和加快报文的传输速度,采用CRC算法。在信道上传输的每一个字节,通过CRC算法校验,从参数表中获得其校验值,使报文无差错地快速传输。电子园51单片机学习网REJ-`r&K
  单片机之间有/无线载波电路进行单播(点对点)通信,或通过专用程控交换机连接单片机组成的有/无线局域网进行单播或广播(多点对多点)通信,都要实现报文的快速交换。一个最关键的问题就是要解决传输报文的误码问题。常用的方法是设计有效的硬件驱动电路和编制相应的监控软件。CRC算法不需要设计专门的硬件驱动电路,而是直接依靠设计监控软件的办法,对报文进行快速的校验来提高报文的传输速度,并保证报文的可靠传输。
?x'k m!X+KQ'Fv44606  CRC算法能进行单个报文和分组报文传输的校验;在分组报文的传输中,对每一个分组报文进行校验,如果正确接收,则将此组报文存入缓冲区;否则记下此分组报文的组号,等所有的分组报文传输完后,再统计接收失败的组数,将失败的分组报文的组号传输到发送端的单片机,请求重发失败的分组报文。报文全部接收正确后,在缓冲区中合并所有的分组,形成一个完整的报文,传输给接收端的单片机,这样就能减少传输的时间,提高系统的可靠性。电子园51单片机学习网K4az!H} M }(B%J?
电子园51单片机学习网5hzwCg8{8X|
2 CRC算法的原理

z*xNW0J3Q44606
R-{)ic-c F5N wQ44606  国际电报电话咨询委员会推荐的CRC—CCITT生成多项式(G(x))有多种,这里选取约定的生成多项式G(x)=x16+x12+x5+1 来制造CRC校验的参数表。CRC约定的校验规则是:让需要校验的报文代码(M(x))能为G(x)除尽。如果除得尽,表明代码正确;如果除不尽,余数(R(x))将指明出错位所在的位置。在单片机的通信中,一般要在内存中开辟缓冲区存放报文代码,涉及的单片机发送端/接收端硬件电路框图见图1。电子园51单片机学习网9lg3{ D,B*K-Hx


6136_2009040619464712CFP.jpg电子园51单片机学习网ehe+M wR`b

电子园51单片机学习网$m*]bp/^G#Y

  在发送端的单片机中,通过对传输报文的字节进行CRC编码,得到一个16进制的编码值,并将该值存放于传输报文的最后,一并传输给接收端的单片机。电子园51单片机学习网 a/f5kZs
  接收端单片机收到报文后,采用CRC校验,也得到一个16进制校验值,将该校验值和传输来的编码值进行比较,如果相等,证明传输无误,向发送端发送一个接收正确的应答信号;否则接收到的报文有误码,并向发送端发送请求重发的应答信号,也就是ARQ方式。重发的次数由程序设计者来定,但一般最好定为3次重发。重发的次数太多,则此信道长期被占用,影响别的单位和自身的通信;重发次数太少,在信道干扰太大及不稳定的情况下不能正确的接收。电子园51单片机学习网4d(v ~!mTF
电子园51单片机学习网 m6tn}k%Y
3 CRC算法程序设计
电子园51单片机学习网$T`.`Z sgk#Y4e'P

电子园51单片机学习网6|/y/KCHw

3.1 CRC算法描述电子园51单片机学习网i,~.ojrE|
  校验一个报文,令不同的变量存放校验值和中间结果,依次从缓冲区中取报文的每一个字节,经过运算,就可以从参数表中获得相应的校验值,直到此报文被校验完。电子园51单片机学习网n)bP5x.s M

VK#w%rW [!{44606

6136_2009040619464729Bv3.jpg  

o2w{`1`0a44606 电子园51单片机学习网2shS` G lP+q

3.2 CRC算法C语言程序清单电子园51单片机学习网 D\wr\ Yd


|~3U\'\2`44606    CRC参数表如下:
@0i^w4laCt j44606 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,

d+e]| ]LI"o_44606 电子园51单片机学习网X-w*xa ?|YWF9i

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};
"Iy m-A$R3d44606#define BUFLEN 512  /*--缓冲区大小———*/

Y r j4Ww44606 电子园51单片机学习网W%qT#Drp*h

#define ETB (byte)0x20 /*——报文结束符——*/

6B,V(xBb0?44606 电子园51单片机学习网;DW$c(iIm

byte crc0=0,crc1=0;  /*———字节变量———*/电子园51单片机学习网w5]0Bd BY-U


#||$nl V3`3D1|44606void main(void)电子园51单片机学习网]pcv#l}


8k){L9J9J-c44606{ /*———根据要求编写主程序,调用crcvalue()子程序———*/ }电子园51单片机学习网9Q Pn z#PL D9I
word crcvalue(byte*crcbuf) /*计算报文的CRC值,crcbuf是缓冲区*/电子园51单片机学习网s@ J7F)w
{ word count;
IW5hMTO G EB'G}44606for(crc0=crc1=0,count=1;crcbuf[count]!=ETB&&count

wT\tA _j)t2N o'i44606 电子园51单片机学习网!Bv e9t%boK

    <BUFLEN;count++)  电子园51单片机学习网X'uq/Dcm7M

电子园51单片机学习网Pa {j(v(m

    crccount(crcbuf[count]);电子园51单片机学习网,ZC's_cJdlL
    if(count+4>=BUFLEN)return 0;  crccount(crcbuf[count]);

)fJ.W&c f1h4p*m44606 电子园51单片机学习网8l3N3m%NQu-S

    crcbuf(++count)=crc0;crcbuf(++count)=crc1;crcbuf(++

Oy3Xi*zXo&g/y^ z5S44606 电子园51单片机学习网DL5pD7x&g

    count)=crc0;
gc6N `u'E u D44606    crcbuf(++count)=crc1;crcbuf(++count)=crc0;crcbuf(++电子园51单片机学习网)G8T2~ ~&c#t/d]xm


YO${ o"K~QD!MWc)o44606    count)=crc1;
!t6iZ,p!X Xk+E44606    return  ++count;  }电子园51单片机学习网%qfY.f-|?
viod crccount(byte crc100) /*计算一个字节的CRC值*/

hk%@P&}7LP,z4z44606

T5F!@!p6i%]6]n44606    {  byte  crc10,crc11,crc20,crc21;  word crclen;
q,y*Yue)M44606    crclen=(word)crc100; crclen=(crclen&0xff)<<1; crc10=crcclar     电子园51单片机学习网$Wy3]K+V{Mo


0gDs@X:[bi44606    [crclen++];
Xe2P^ Y0I2a)Sl44606    crc11=crcclar[crclen++]; crc11=crc100^crc0^crc11;

t%u|3l'cC!K8Q-X44606 电子园51单片机学习网NavH5@:_

    crc10=crc1  ^crc10;电子园51单片机学习网_ cU%L%^ J
    crclen=(word)crc0; crclen=(crclen&0xff)<<1; crc20=电子园51单片机学习网'l+B9n9Eus`

电子园51单片机学习网&flKj8r#}C ]#f

    crcclar[crclen++];
%?4^&P@In sB[44606    crc21=crcclar[crclen++]; crc0=drd^crc20; crc1=crc11^

f,B6}khm:`w44606

5t,m-` X$r3_p44606    crc21;  }电子园51单片机学习网2c+RrsN K:K
电子园51单片机学习网+}?;VjBaR ]:F
4 CRC算法的优缺点

l`%eiVg!z/sU&]44606
K QrvW&{44606  用软件实现的CRC算法,其主要优点是突出了一个“快”字,为了提高校验速度,把参数表和应用程序一起写入单片机的EPROM内,当进行CRC校验时,对需要发送的每一字节,按上述的方法进行计算,就可得到该字节的校验值。从而提高了速度,较好地克服了异步传输中校验和发送时间很不匹配的矛盾。电子园51单片机学习网I*K5PS_N8g*m{
  CRC算法的缺点是由于要存储512字节的参数表,需要更多的存储空间,但是在单片机的应用中,这种以空间换取时间的方法是值得提倡的。 电子园51单片机学习网Q%_O%Zts)h&c!g



5 结束语
/skE'~.x&B44606
电子园51单片机学习网*TW&Bp~1LW]#m

  CRC算法能很好地解决传输报文过程中的校验问题,在8031,80C196,80188等CPU控制的单片机中,进行了大量的有/无线传输报文实验,在300BPS,600BPS,1200BPS,2400BPS,4800BPS的波特率下,CRC算法都能很好地进行校验,提高了传输速度。
iBz2xI-|-|44606 电子园51单片机学习网k-ou8a'?(W w;c

 电子园51单片机学习网9ml+I5l(x4L QvU


  [参考文献]
B bz_%X }44606
[1] 白驹珩,等.单片机及其应用[M].电子科技大学出版社,1994.5. 电子园51单片机学习网g-yN ^!fi

[2] 刘乐善,等.微型计算机接口技术及应用[M].华中理工大学出版社,1994.3.电子园51单片机学习网'J`%^zw I5p
[3] 俸远祯,等.计算机组成原理[M].电子工业出版社,1996.8.电子园51单片机学习网7mS2T&o,l,V%K }
[4] Herbert Schildt.最新C语言大全[M].中科院希望高级电脑技术公司,1991.5.

$u6l4Z(rYz44606
PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
6
关闭 站长推荐上一条 /3 下一条