您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 电子设计/PCB > 计算CRC-16循环冗余校验码的程序开发
ঊᇒḕⴻ᮷ㄐ䇑㇇ᗚ⧟߇։ṑ傼⸱Ⲵ〻ᒿᔰਁ䇑㇇CRC-16ᗚ⧟߇։ṑ傼⸱Ⲵ〻ᒿᔰਁǏᇩ᪈㾱ǐCRC-16ᱟỰ⍻ᮠᦞ൘ਁ䘱䗷〻ѝਁ⭏䭉䈟Ⲵᑨ⭘ṑ傼ᯩ⌅ˈᵜ᮷䙊䗷Ӿᐕ〻ᓄ⭘Ⲵ䀂ᓖˈ䇢䘠ྲօᇎ⧠CRC-16Ⲵ〻ᒿᔰਁˈᒦ㔉ࠪҶVisualBasic઼VisualC++〻ᒿԓ⸱ˈ㔉Ӿһᐕъ᧗ࡦⲴӪઈ൘߉䙊ؑ〻ᒿⲴᰦىᨀаӋᴹԧ٬Ⲵ৲㘳DŽаǃࡽ䀰CRCⲴ䇑㇇ᯩᔿᱟሶⅢՐ䗃Ⲵᮠᦞඇ㿶Ѫаึ䘎㔝սᡰᶴᡀⲴᮤњᮠ٬ˈሶ↔ᮠ٬䲔ԕањ⢩ᇊⲴ䲔ᮠˈ䙊ᑨԕҼ䘋ࡦ㺘⽪ˈ↔䲔ᮠ〠Ѫ⭏ᡀཊ亩ᔿ˄ANSICRC-16Ⲵ⭏ᡀཊ亩ᔿѪ˖x16+x15+x2+1˅DŽⴞࡽ䖳ᑨ⭘ⲴCRCսᮠⴞᴹ8઼16ԕ৺32ˈ൘䘉䟼ਚ䇢䘠ྲօ߉CRCսᮠⴞѪ16Ⲵ〻ᒿDŽCRCսᮠⴞ䎺བྷˈᮠᦞⲴ䭉䈟Ự⍻⦷ࡉ䎺儈ˈնᗵ享ཊ㣡аӋᰦ䰤䘋㹼ᮠᦞⲴ䇑㇇DŽҼǃCRC-16䇑㇇↕僔1ǃLoada16-bitregisterwithFFFFhex.CallthistheCRCregister.2ǃExclusiveORthefirst8-bitbyteofthemessagewiththelow-orderbyteofthe16-bitCRC,puttingtheresultintheCRCregister.3ǃShifttheCRCregisteronebittotheright(towardtheLSB),zero-fillingtheMSB.ExtractandExamine.4ǃIftheLSBwas1:ExclusiveORtheCRCregisterwithpolynomialvalueA001hex.IftheLSBwas0:Repeatstep3(anothershift).5ǃRepeatstep3and4until8shiftshavebeenperformed.Whenthisisdone,acomplete8-bitbytewillhavebeenprocessed.6ǃRepeatstep2through5forthenext8-bitbyteofthemmessage.7ǃThefinalcontentoftheCRCregisteristheCRCvalue.ѝ᮷䀓䟺ྲл˖1ǃᇊѹањࡍ٬ѪFFFFⲴ16սⲴਈ䟿ˈ䈕ਈ䟿〠ѪCRCᇴᆈಘ.2ǃᢺⅢਁ䘱ᡆ᧕᭦⎸Ⲵ儈8ս઼CRCᇴᆈಘⲴᓅ8սᔲᡆ䘀㇇ˈᒦᢺ㔃᷌൘䍻ࡠCRCᇴᆈಘDŽ3ǃCRCᇴᆈಘਣ〫1ս˄ᵍᴰվս˅ˈ਼ᰦᴰ儈ս䴦DŽਆࠪᒦỰḕᴰվսᱟѪ1DŽ4ǃྲ᷌Ѫ1ˈࡉCRCᇴᆈಘоཊ亩ᔿA001ᔲᡆ˗ྲ᷌Ѫ0ˈࡉ䟽༽ㅜ3↕Ⲵࣘ5ǃ䟽༽3઼4ⴤࡠᆼᡀҶ8⅑〫սDŽ䘉ṧᆼᮤⲴ8սᆇ㢲ሶᆼᡀ༴⨶ҶDŽ6ǃሩҾлањ8սᆇ㢲Ⲵ༴⨶ቡᱟ䟽༽ㅜ2↕ࡠㅜ5↕Ҷ7ǃᢺᡰᴹⲴⅢਁ䘱ᡆ᧕᭦⎸䘉ṧ༴⨶ਾˈCRCᇴᆈಘ䟼Ⲵ٬ቡᱟᡁԜᴰ㓸䴰㾱ᗇࡠⲴCRCṑ傼⸱DŽݸⴻањֻᆀ:䇑㇇00HⲴṑ傼⸱:䴰㾱Ր䘱(᧕ਇ)Ⲵᮠᦞ00000000(Ҽ䘋ࡦ)ᇴᆈಘ1111111111111111ਆՐ䘱ᮠᦞⲴ儈ޛսоᇴᆈಘⲴվޛսᔲᡆ000000001111111111111111ᆈޕᇴᆈಘᇴᆈಘ1111111111111111аǃᇴᆈಘਣ〫аսᇴᆈಘ0111111111111111〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010000000000001˅ᔲᡆ011111111111111110100000000000011101111111111110Ҽǃᇴᆈಘਣ〫аս0110111111111111〫ࠪⲴᱟ0йǃᇴᆈಘਣ〫аս0011011111111111〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010000000000001˅ᔲᡆ001101111111111110100000000000011001011111111110ഋǃᇴᆈಘਣ〫аս0100101111111111〫ࠪⲴᱟ0ӄǃᇴᆈಘਣ〫аս0010010111111111〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010000000000001˅ᔲᡆ001001011111111110100000000000011000010111111110ޝǃᇴᆈಘਣ〫аս0100001011111111〫ࠪⲴᱟ0гǃᇴᆈಘਣ〫аս0010000101111111〫ࠪⲴᱟ1ˈCRCᇴᆈಘоA001˄1010000000000001˅ᔲᡆ001000010111111110100000000000011000000101111110ޛǃᇴᆈಘਣ〫аս0100000010111111ᡰԕˈ00H˄Ҽ䘋ࡦⲴ00000000˅Ⲵṑ傼⸱Ѫ40BF〻ᒿ⭼䶒〻ᒿԓ⸱˖PrivateSubcmdGenerate_Click()DimOutByte(7)AsByte,CRC16()AsByte,CRC16LOAsByte,CRC16HIAsByte,TempHIAsByte,TempLOAsByteDimiAsInteger,jAsInteger'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''OutByte(0)=Val(&H&txtOutByte(0).Text)OutByte(1)=Val(&H&txtOutByte(1).Text)OutByte(2)=Val(&H&txtOutByte(2).Text)OutByte(3)=Val(&H&txtOutByte(3).Text)OutByte(4)=Val(&H&txtOutByte(4).Text)OutByte(5)=Val(&H&txtOutByte(5).Text)CRC16LO=&HFF'(1)Loada16-bitregisterwithFFFFhex.callthistheCRCregister.CRC16HI=&HFFFori=0ToUBound(OutByte)-2CRC16LO=CRC16LOXorOutByte(i)'(2)ExclusiveORthefirst8-bitbyteofthemessage'withthelow-orderbyteofthe16-bitCRC,'puttingtheresultintheCRCregisterForj=0To7TempHI=CRC16HITempLO=CRC16LOCRC16HI=CRC16HI\2'(3)ShifttheCRCregisteronebittotheright(towardtheLSB),zero-fillingCRC16LO=CRC16LO\2'theMSB.ExtractandExamineIf((TempHIAnd&H1)=&H1)ThenCRC16LO=CRC16LOOr&H80'〫սᰦˈྲ᷌儈վսᱟ1ˈࡉվ儈ս࣐1EndIfIf((TempLOAnd&H1)=&H1)Then'(4)iftheLSBwas1:CRC16HI=CRC16HIXor&HA0'ExclusiveORtheCRCregisterwithpolynomialvalueA001hex.CRC16LO=CRC16LOXor&H1EndIf'iftheLSBwas0:Repeatstep3(anothershift)Nextj'(5)Repeatstep3and4until8shiftshavebeenperformed.'whenthisisdone,acomplete8-bitbytewillhavebeenprocessed.Nexti'(6)Repeatstep2through5forthenext8-bitbyteofthemmessage.OutByte(6)=CRC16LO'(7)ThefinalcontentsoftheCRCregisteristheCRCvaluetxtOutByte(6)=Hex(OutByte(6))OutByte(7)=CRC16HItxtOutByte(7)=Hex(OutByte(7))EndSubഋǃVisualC++〻ᒿᇎ⧠〻ᒿ⭼䶒ѫ㾱ԓ⸱˖#defineCHECKVALUE(pt,nl)if((btData==NULL)||(nLength=0)){AfxMessageBox(ᰐ᭸ᮠ㓴ʽ);return-1;}BOOLCCRC_16Dlg::OnInitDialog(){ĂĂm_strCRC=0104005C0004;UpdateData(FALSE);returnTRUE;//returnTRUEunlessyousetthefocustoacontrol}voidCCRC_16Dlg::OnOK(){UpdateData(TRUE);CStringstrTemp;strTemp=m_strCRC;WORDCRC;BYTEbtData[4096];intnLength=StrToHex(strTemp,btData);CRC=ANSI_CRC_16(btData,nLength);//ANSI-16CStringstr;str.Format(%02X%02X,(BYTE)(CRC8),(BYTE)CRC);AfxMessageBox(str);CDialog::OnOK();}WORDCCRC_16Dlg::ANSI_CRC_16(BYTE*btData,intnLength)//ANSICRC-16,x16+x15+x2+1{CHECKVALUE(btData,nLength);WORDCRC=0xFFFF;BYTEj,Tmp=0;inti;for(i=0;inLength;i++){CRC^=btData[i];for(j=0;j8;j++){Tmp=CRC&0x0001;CRC=CRC1;if(Tmp)CRC=(CRC^0xA001);}}return((CRC8)+(CRC8));/*ᓄ⭘ᰦ儈൘ݸ*/}intCCRC_16Dlg::StrToHex(CStringstr,BYTE*btData){intnLength=str.GetLength();if(nLength2)return0;for(inti=0;inLength;i+=3)_stscanf(str.Mid(i,2),%02X,&btData[i/3]);return(i+1)/3;}ᴰ䘁䈫㘵˖㖁৻䇴䇪˖ਁ㺘䇴䇪˖ᇩ˖
本文标题:计算CRC-16循环冗余校验码的程序开发
链接地址:https://www.777doc.com/doc-6411943 .html