單片機通信中的CRC算法原理及程序設計
1 引 言
本文引用地址:http://m.butianyuan.cn/article/172509.htm在單片機的近、遠程通信中,為了確保傳輸報文(數據和信息)的正確性和加快報文的傳輸速度,采用CRC算法。在信道上傳輸的每一個字節(jié),通過CRC算法校驗,從參數表中獲得其校驗值,使報文無差錯地快速傳輸。
單片機之間有/無線載波電路進行單播(點對點)通信,或通過專用程控交換機連接單片機組成的有/無線局域網進行單播或廣播(多點對多點)通信,都要實現(xiàn)報文的快速交換。一個最關鍵的問題就是要解決傳輸報文的誤碼問題。常用的方法是設計有效的硬件驅動電路和編制相應的監(jiān)控軟件。CRC算法不需要設計專門的硬件驅動電路,而是直接依靠設計監(jiān)控軟件的辦法,對報文進行快速的校驗來提高報文的傳輸速度,并保證報文的可靠傳輸。
CRC算法能進行單個報文和分組報文傳輸的校驗;在分組報文的傳輸中,對每一個分組報文進行校驗,如果正確接收,則將此組報文存入緩沖區(qū);否則記下此分組報文的組號,等所有的分組報文傳輸完后,再統(tǒng)計接收失敗的組數,將失敗的分組報文的組號傳輸到發(fā)送端的單片機,請求重發(fā)失敗的分組報文。報文全部接收正確后,在緩沖區(qū)中合并所有的分組,形成一個完整的報文,傳輸給接收端的單片機,這樣就能減少傳輸的時間,提高系統(tǒng)的可靠性。
2 CRC算法的原理
國際電報電話咨詢委員會推薦的CRC—CCITT生成多項式(G(x))有多種,這里選取約定的生成多項式G(x)=x16+x12+x5+1 來制造CRC校驗的參數表。CRC約定的校驗規(guī)則是:讓需要校驗的報文代碼(M(x))能為G(x)除盡。如果除得盡,表明代碼正確;如果除不盡,余數(R(x))將指明出錯位所在的位置。在單片機的通信中,一般要在內存中開辟緩沖區(qū)存放報文代碼,涉及的單片機發(fā)送端/接收端硬件電路框圖見圖1。
在發(fā)送端的單片機中,通過對傳輸報文的字節(jié)進行CRC編碼,得到一個16進制的編碼值,并將該值存放于傳輸報文的最后,一并傳輸給接收端的單片機。
接收端單片機收到報文后,采用CRC校驗,也得到一個16進制校驗值,將該校驗值和傳輸來的編碼值進行比較,如果相等,證明傳輸無誤,向發(fā)送端發(fā)送一個接收正確的應答信號;否則接收到的報文有誤碼,并向發(fā)送端發(fā)送請求重發(fā)的應答信號,也就是ARQ方式。重發(fā)的次數由程序設計者來定,但一般最好定為3次重發(fā)。重發(fā)的次數太多,則此信道長期被占用,影響別的單位和自身的通信;重發(fā)次數太少,在信道干擾太大及不穩(wěn)定的情況下不能正確的接收。
3 CRC算法程序設計
3.1 CRC算法描述
校驗一個報文,令不同的變量存放校驗值和中間結果,依次從緩沖區(qū)中取報文的每一個字節(jié),經過運算,就可以從參數表中獲得相應的校驗值,直到此報文被校驗完。
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};
?。efine BUFLEN 512 /*--緩沖區(qū)大小———*/
?。efine ETB (byte)0x20?。?mdash;—報文結束符——*/
byte crc0=0,crc1=0; ?。?mdash;——字節(jié)變量———*/
void main(void)
?。。?mdash;——根據要求編寫主程序,調用crcvalue()子程序———*/?。?/p>
word crcvalue(byte*crcbuf) /*計算報文的CRC值,crcbuf是緩沖區(qū)*/
{ word count;
for(crc0=crc1=0,count=1;crcbuf[count]!=ETB&&count
?。糂UFLEN;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 ?。玞ount; ?。?/p>
viod crccount(byte crc100) /*計算一個字節(jié)的CRC值*/
?。 yte crc10,crc11,crc20,crc21; word crclen;
crclen=(word)crc100; crclen=(crclen&0xff)<<1; crc10=crcclar
?。踓rclen++];
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; ?。?/p>
4 CRC算法的優(yōu)缺點
用軟件實現(xiàn)的CRC算法,其主要優(yōu)點是突出了一個“快”字,為了提高校驗速度,把參數表和應用程序一起寫入單片機的EPROM內,當進行CRC校驗時,對需要發(fā)送的每一字節(jié),按上述的方法進行計算,就可得到該字節(jié)的校驗值。從而提高了速度,較好地克服了異步傳輸中校驗和發(fā)送時間很不匹配的矛盾。
CRC算法的缺點是由于要存儲512字節(jié)的參數表,需要更多的存儲空間,但是在單片機的應用中,這種以空間換取時間的方法是值得提倡的。
5 結束語
CRC算法能很好地解決傳輸報文過程中的校驗問題,在8031,80C196,80188等CPU控制的單片機中,進行了大量的有/無線傳輸報文實驗,在300BPS,600BPS,1200BPS,2400BPS,4800BPS的波特率下,CRC算法都能很好地進行校驗,提高了傳輸速度。
?。蹍⒖嘉墨I]
[1] 白駒珩,等.單片機及其應用[M].電子科技大學出版社,1994.5.
?。?] 劉樂善,等.微型計算機接口技術及應用[M].華中理工大學出版社,1994.3.
?。?] 俸遠禎,等.計算機組成原理[M].電子工業(yè)出版社,1996.8.
[4] Herbert Schildt.最新C語言大全[M].中科院希望高級電腦技術公司,1991.5.
c語言相關文章:c語言教程
通信相關文章:通信原理
評論