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