老版本GCU
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

164 lines
7.2 KiB

  1. /************************************************************************
  2. * 模块名称:CRC.c
  3. * 程序功能�
  4. * 编 写 者:TNT
  5. * 编写日期:2014-01-12
  6. * 修 改 者:
  7. * 修改日期:
  8. * 编译环境:KEIL4 MDK ARM V4.71.2.0
  9. * 硬件平台:STM32F103RBT6
  10. ************************************************************************/
  11. /************************************************************************/
  12. /* */
  13. /* */
  14. /* INCLUDES */
  15. /* */
  16. /* */
  17. /************************************************************************/
  18. #include "crc.h"
  19. /************************************************************************/
  20. /* */
  21. /* */
  22. /* DEFINITION */
  23. /* */
  24. /* */
  25. /************************************************************************/
  26. /************************************************************************/
  27. /* */
  28. /* */
  29. /* DECLARATION */
  30. /* */
  31. /* */
  32. /************************************************************************/
  33. /************************************************************************/
  34. /* */
  35. /* */
  36. /* VARIABLE */
  37. /* */
  38. /* */
  39. /************************************************************************/
  40. unsigned char const CRC_TAB[256]=
  41. {
  42. 0x00,0xd5,0x7f,0xaa,0xfe,0x2b,0x81,0x54,0x29,0xfc,0x56,0x83,0xd7,0x02,0xa8,0x7d,
  43. 0x52,0x87,0x2d,0xf8,0xac,0x79,0xd3,0x06,0x7b,0xae,0x04,0xd1,0x85,0x50,0xfa,0x2f,
  44. 0xa4,0x71,0xdb,0x0e,0x5a,0x8f,0x25,0xf0,0x8d,0x58,0xf2,0x27,0x73,0xa6,0x0c,0xd9,
  45. 0xf6,0x23,0x89,0x5c,0x08,0xdd,0x77,0xa2,0xdf,0x0a,0xa0,0x75,0x21,0xf4,0x5e,0x8b,
  46. 0x9d,0x48,0xe2,0x37,0x63,0xb6,0x1c,0xc9,0xb4,0x61,0xcb,0x1e,0x4a,0x9f,0x35,0xe0,
  47. 0xcf,0x1a,0xb0,0x65,0x31,0xe4,0x4e,0x9b,0xe6,0x33,0x99,0x4c,0x18,0xcd,0x67,0xb2,
  48. 0x39,0xec,0x46,0x93,0xc7,0x12,0xb8,0x6d,0x10,0xc5,0x6f,0xba,0xee,0x3b,0x91,0x44,
  49. 0x6b,0xbe,0x14,0xc1,0x95,0x40,0xea,0x3f,0x42,0x97,0x3d,0xe8,0xbc,0x69,0xc3,0x16,
  50. 0xef,0x3a,0x90,0x45,0x11,0xc4,0x6e,0xbb,0xc6,0x13,0xb9,0x6c,0x38,0xed,0x47,0x92,
  51. 0xbd,0x68,0xc2,0x17,0x43,0x96,0x3c,0xe9,0x94,0x41,0xeb,0x3e,0x6a,0xbf,0x15,0xc0,
  52. 0x4b,0x9e,0x34,0xe1,0xb5,0x60,0xca,0x1f,0x62,0xb7,0x1d,0xc8,0x9c,0x49,0xe3,0x36,
  53. 0x19,0xcc,0x66,0xb3,0xe7,0x32,0x98,0x4d,0x30,0xe5,0x4f,0x9a,0xce,0x1b,0xb1,0x64,
  54. 0x72,0xa7,0x0d,0xd8,0x8c,0x59,0xf3,0x26,0x5b,0x8e,0x24,0xf1,0xa5,0x70,0xda,0x0f,
  55. 0x20,0xf5,0x5f,0x8a,0xde,0x0b,0xa1,0x74,0x09,0xdc,0x76,0xa3,0xf7,0x22,0x88,0x5d,
  56. 0xd6,0x03,0xa9,0x7c,0x28,0xfd,0x57,0x82,0xff,0x2a,0x80,0x55,0x01,0xd4,0x7e,0xab,
  57. 0x84,0x51,0xfb,0x2e,0x7a,0xaf,0x05,0xd0,0xad,0x78,0xd2,0x07,0x53,0x86,0x2c,0xf9
  58. };
  59. /************************************************************************/
  60. /* */
  61. /* */
  62. /* IMPLEMENTATION */
  63. /* */
  64. /* */
  65. /************************************************************************/
  66. /*********************************************************************
  67. ; 发送的CRC校验过程:
  68. ; (1) CRC校验初值为0xFF,放入某个变量(如CRC_temp)中,
  69. ; (2) 对每一个发送字节TX[i],i=0,...,Send_num,
  70. ; { 将CRC_temp与TX[i]异或,结果设为CRCTab_Adresss
  71. ; 根据CRCTab_Adresss查CRC表,得到的值放回CRC_temp中
  72. ; }
  73. ; (3) 全部发送结束,则发送CRC_temp作为CRC校验码
  74. ;
  75. ; 接收的CRC校验过程:
  76. ; (1) CRC校验初值为0xFF,放入某个变量(如CRC_temp)中,
  77. ; (2) 对每一个接收到的字节RX[i],i=0,...,RCV_num,CRC校验码
  78. ; { 将CRC_temp与TX[i]异或,结果设为CRCTab_Adresss
  79. ; 根据CRCTab_Adresss查CRC表,得到的值放回CRC_temp中
  80. ; }
  81. ; (3) 全部接收结束,则CRC_temp中的结果如果是0,则CRC校验成功,否则失败
  82. *********************************************************************/
  83. /***********************************************************************
  84. * 函 数 名: CheckCrc()
  85. * 功能描述:
  86. * 函数说明:
  87. * 调用函数: 无
  88. * 全局变量: 无
  89. * 输 入: 无
  90. * 返 回:
  91. * 设 计 者:XX 日期:2003-10-15
  92. * 修 改 者:XX 日期:
  93. * 版 本:0.01
  94. ***********************************************************************/
  95. unsigned char CheckCrc(unsigned char *ptr,unsigned char leng)
  96. {
  97. unsigned char i;
  98. unsigned char bytetemp=0xff; //seed value
  99. for(i=0;i<leng;i++)
  100. bytetemp=CRC_TAB[bytetemp^ptr[i]];
  101. return(bytetemp);
  102. }
  103. /***********************************************************************
  104. * 函 数 名: CalcCrc()
  105. * 功能描述:
  106. * 函数说明:
  107. * 调用函数: 无
  108. * 全局变量: 无
  109. * 输 入: 无
  110. * 返 回:
  111. * 设 计 者:XX 日期:2003-10-15
  112. * 修 改 者:XX 日期:
  113. * 版 本:0.01
  114. ***********************************************************************/
  115. unsigned char CalcCrc(unsigned char runingcrc,unsigned char addtocrc)
  116. {
  117. return(CRC_TAB[runingcrc^addtocrc]);
  118. }
  119. /*********************************************************************
  120. ; 发送的ccTalk CheckSum 校验过程:
  121. ; (1) CheckSum is what makes the 8 less important bits of the
  122. ; sum of all the bytes of the message, including the actual
  123. ; checksum , give '0' as the result.
  124. ;
  125. ; (2) example:
  126. ; the message 01 00 02 00 will be followed by checksum 253 because:
  127. ; 01 00 02 00 FD
  128. ;
  129. ; 1 + 0 + 2 + 0 + 253 = 256 = 0
  130. *********************************************************************/
  131. /***********************************************************************
  132. * 函 数 名: unsigned char CheckSum(unsigned char *ptr,unsigned char leng)
  133. * 功能描述:
  134. * 函数说明:
  135. * 调用函数: 无
  136. * 全局变量: 无
  137. * 输 入: 无
  138. * 返 回:
  139. * 设 计 者:XXX 日期:2003-10-15
  140. * 修 改 者:XXX 日期:
  141. * 版 本:1.00
  142. ***********************************************************************/
  143. unsigned char CheckSum(unsigned char *ptr,unsigned char leng)
  144. {
  145. unsigned char i;
  146. unsigned char bytetemp=0;
  147. for(i=0;i<leng;i++)
  148. {
  149. bytetemp-=ptr[i];
  150. }
  151. return(bytetemp);
  152. }
  153. /******************* (C) COPYRIGHT 2017 LECOOAI *****END OF FILE****/