微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > CRC校验原理与程序设计——(RS485总线系统应用之1)

CRC校验原理与程序设计——(RS485总线系统应用之1)

时间:12-15 来源:互联网 点击:

unction

Function GetCRCHi(Ind As Long) As Byte’CRC高位字节值表

GetCRCHi = Choose(Ind + 1,_

&H00, &HC0, &HC1, &H01, &HC3, &H03, &H02, &HC2, &HC6, &H06,_

&H07, &HC7, &H05, &HC5, &HC4, &H04, &HCC, &H0C, &H0D, &HCD,_

&H0F, &HCF, &HCE, &H0E, &H0A, &HCA, &HCB, &H0B, &HC9, &H09,_

&H08, &HC8, &HD8, &H18, &H19, &HD9, &H1B, &HDB, &HDA, &H1A,_

&H1E, &HDE, &HDF, &H1F, &HDD, &H1D, &H1C, &HDC, &H14, &HD4,_

&HD5, &H15, &HD7, &H17, &H16, &HD6, &HD2, &H12, &H13, &HD3,_

&H11, &HD1, &HD0, &H10, &HF0, &H30, &H31, &HF1, &H33, &HF3,_

&HF2, &H32, &H36, &HF6, &HF7, &H37, &HF5, &H35, &H34, &HF4,_

&H3C, &HFC, &HFD, &H3D, &HFF, &H3F, &H3E, &HFE, &HFA, &H3A,_

&H3B, &HFB, &H39, &HF9, &HF8, &H38, &H28, &HE8, &HE9, &H29,_

&HEB, &H2B, &H2A, &HEA, &HEE, &H2E, &H2F, &HEF, &H2D, &HED,_

&HEC, &H2C, &HE4, &H24, &H25, &HE5, &H27, &HE7, &HE6, &H26,_

&H22, &HE2, &HE3, &H23, &HE1, &H21, &H20, &HE0, &HA0, &H60,_

&H61, &HA1, &H63, &HA3, &HA2, &H62, &H66, &HA6, &HA7, &H67,_

&HA5, &H65, &H64, &HA4, &H6C, &HAC, &HAD, &H6D, &HAF, &H6F,_

&H6E, &HAE, &HAA, &H6A, &H6B, &HAB, &H69, &HA9, &HA8, &H68,_

&H78, &HB8, &HB9, &H79, &HBB, &H7B, &H7A, &HBA, &HBE, &H7E,_

&H7F, &HBF, &H7D, &HBD, &HBC, &H7C, &HB4, &H74, &H75, &HB5,_

&H77, &HB7, &HB6, &H76, &H72, &HB2, &HB3, &H73, &HB1, &H71,_

&H70, &HB0, &H50, &H90, &H91, &H51, &H93, &H53, &H52, &H92,_

&H96, &H56, &H57, &H97, &H55, &H95, &H94, &H54, &H9C, &H5C,_

&H5D, &H9D, &H5F, &H9F, &H9E, &H5E, &H5A, &H9A, &H9B, &H5B,_

&H99, &H59, &H58, &H98, &H88, &H48, &H49, &H89, &H4B, &H8B,_

&H8A, &H4A, &H4E, &H8E, &H8F, &H4F, &H8D, &H4D, &H4C, &H8C,_

&H44, &H84, &H85, &H45, &H87, &H47, &H46, &H86, &H82, &H42,_

&H43, &H83, &H41, &H81, &H80, &H40)

End Function

Function GetCRCLo(Ind As Long) As Byte’CRC低位字节值表

GetCRCLo = Choose(Ind + 1,_

&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_

&H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_

&H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40, &H01, &HC0,_

&H80, &H41, &H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40,_

&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H00, &HC1,_

&H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0, &H80, &H41,_

&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H00, &HC1,_

&H81, &H40, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_

&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_

&H80, &H41, &H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40,_

&H01, &HC0, &H80, &H41, &H01, &HC0, &H80, &H41, &H00, &HC1,_

&H81, &H40, &H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40,_

&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_

&H80, &H41, &H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40,_

&H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0,_

&H80, &H41, &H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40,_

&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_

&H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_

&H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40, &H01, &HC0,_

&H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_

&H01, &HC0, &H80, &H41, &H00, &HC1, &H81, &H40, &H01, &HC0,_

&H80, &H41, &H00, &HC1, &H81, &H40, &H00, &HC1, &H81, &H40,_

&H01, &HC0, &H80, &H41, &H01, &HC0, &H80, &H41, &H00, &HC1,_

&H81, &H40, &H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41,_

&H00, &HC1, &H81, &H40, &H01, &HC0, &H80, &H41, &H01, &HC0,_

&H80, &H41, &H00, &HC1, &H81, &H40)
End Function

3.2用计算法求CRC函数的程序

计算法的优点是程序编码量小,虽然比查表法速度偏慢,但当被校验的数据量不大时并不明显。

3.2.1用VC编写的程序

unsigned short CRC16(unsigned char buf[], int n)

{

unsigned short crc = 0xffff ;

for(i = 0; i < n; i++)

{

crc ^ = buf[i] ;

for(b = 0; b < 8; b++)

{

bool f = crc & 1 ;

crc >> = 1 ;

if(f)

crc ^ = 0xa001 ;

}

}

return(crc) ;

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top