高级会员
- 积分
- 561
- 金钱
- 561
- 注册时间
- 2014-10-15
- 在线时间
- 132 小时
|
1金钱
我有一个STM32L4R5板子,有四个CRC例程,我搞好了三个。
还有一个CRC7。
代码如下:
- #include "main.h"
- /** @addtogroup STM32L4xx_HAL_Examples
- * @{
- */
- /** @addtogroup CRC_Bytes_Stream_7bit_CRC
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define BUFFER_SIZE_5 5 /* CRC7_DATA8_TEST5[] is 5-byte long */
- #define BUFFER_SIZE_17 17 /* CRC7_DATA8_TEST17[] is 17-byte long */
- #define BUFFER_SIZE_1 1 /* CRC7_DATA8_TEST1[] is 1-byte long */
- #define BUFFER_SIZE_2 2 /* CRC7_DATA8_TEST2[] is 2-byte long */
- /* User-defined polynomial */
- //#define CRC_POLYNOMIAL_7B 0x65 /* X^7 + X^6 + X^5 + X^2 + 1,
- // used in Train Communication Network, IEC 60870-5[17] */
- #define CRC_POLYNOMIAL_7B 0x65 /* X^7 + X^6 + X^5 + X^2 + 1,*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* CRC handler declaration */
- CRC_HandleTypeDef CrcHandle;
- /* Used for storing CRC Value */
- __IO uint32_t uwCRCValue = 0;
- /* Bytes buffers that will consecutively yield CRCs */
- static const uint8_t CRC7_DATA8_TEST5[5] = {0x12,0x34,0xBA,0x71,0xAD};
- static const uint8_t CRC7_DATA8_TEST17[17] = {0x12,0x34,0xBA,0x71,0xAD,
- 0x11,0x56,0xDC,0x88,0x1B,
- 0xEE,0x4D,0x82, 0x93,0xA6,
- 0x7F,0xC3};
- static const uint8_t CRC7_DATA8_TEST1[1] = {0x19};
- static const uint8_t CRC7_DATA8_TEST2[2] = {0xAB,0xCD};
-
- /* Expected CRC Values */
- /* The 7 LSB bits are the 7-bit long CRC */
- uint32_t uwExpectedCRCValue_1 = 0x00000057; /* First byte stream CRC */
- uint32_t uwExpectedCRCValue_2 = 0x0000006E; /* Second byte stream CRC */
- uint32_t uwExpectedCRCValue_3 = 0x0000004B; /* Third byte stream CRC */
- uint32_t uwExpectedCRCValue_4 = 0x00000026; /* Fourth byte stream CRC */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void Error_Handler(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program
- * [url=home.php?mod=space&uid=271674]@param[/url] None
- * @retval None
- */
- int main(void)
- {
- /* STM32L4xx HAL library initialization:
- - Configure the Flash prefetch
- - Systick timer is configured by default as source of time base, but user
- can eventually implement his proper time base source (a general purpose
- timer for example or other time source), keeping in mind that Time base
- duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
- handled in milliseconds basis.
- - Set NVIC Group Priority to 4
- - Low Level Initialization
- */
- HAL_Init();
-
- /* Configure the system clock to 120 MHz */
- SystemClock_Config();
- /* Configure LED1 and LED3 */
- BSP_LED_Init(LED1);
- BSP_LED_Init(LED3);
- /****************************************************************************/
- /* */
- /* CRC peripheral initialization */
- /* */
- /****************************************************************************/
-
- CrcHandle.Instance = CRC;
- /* The default polynomial is not used. The one to be used must be defined
- in CrcHandle.Init.GeneratingPolynomial */
- CrcHandle.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
-
- /* Set the value of the generating polynomial.
- The one used in that example is the 7-bit long CRC generating
- polynomial X^7 + X^6 + X^5 + X^2 + 1 */
- CrcHandle.Init.GeneratingPolynomial = CRC_POLYNOMIAL_7B;
-
- /* The user-defined generating polynomial yields a 7-bit long CRC */
- CrcHandle.Init.CRCLength = CRC_POLYLENGTH_7B;
- /* The default init value is used */
- CrcHandle.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLE;
- /* The input data are not inverted */
- CrcHandle.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
- /* The output data are not inverted */
- CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
- /* The input data are bytes (8-bit long data) */
- CrcHandle.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
- /* De-initialize the CRC peripheral */
- if (HAL_CRC_DeInit(&CrcHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- /* Then, initialize the CRC handle */
- if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- /****************************************************************************/
- /* */
- /* CRC computation of a first bytes stream */
- /* */
- /****************************************************************************/
- /* The 7-bit long CRC of a 5-byte buffer is computed. After peripheral initialization,
- the CRC calculator is initialized with the default value that is 0x7F for
- a 7-bit CRC.
-
- The computed CRC is stored in uint32_t uwCRCValue. The 7-bit long CRC is made of
- uwCRCValue 7 LSB bits. */
- uwCRCValue = HAL_CRC_Accumulate(&CrcHandle, (uint32_t *)&CRC7_DATA8_TEST5, BUFFER_SIZE_5);
- /* Compare the CRC value to the expected one */
- if (uwCRCValue != uwExpectedCRCValue_1)
- {
- /* Wrong CRC value: Turn LED3 on */
- Error_Handler();
- }
-
- /****************************************************************************/
- /* */
- /* CRC computation of a second bytes stream */
- /* */
- /****************************************************************************/
- /* The 7-bit long CRC of a 17-byte buffer is computed. The CRC calculator
- is not re-initialized, instead the previously computed CRC is used
- as initial value. */
- uwCRCValue = HAL_CRC_Accumulate(&CrcHandle, (uint32_t *)&CRC7_DATA8_TEST17, BUFFER_SIZE_17);
- /* Compare the CRC value to the expected one */
- if (uwCRCValue != uwExpectedCRCValue_2)
- {
- /* Wrong CRC value: Turn LED3 on */
- Error_Handler();
- }
- /****************************************************************************/
- /* */
- /* CRC computation of a single byte */
- /* */
- /****************************************************************************/
- /* The 7-bit long CRC of a 1-byte buffer is computed. The CRC calculator
- is not re-initialized, instead the previously computed CRC is used
- as initial value. */
- uwCRCValue = HAL_CRC_Accumulate(&CrcHandle, (uint32_t *)&CRC7_DATA8_TEST1, BUFFER_SIZE_1);
- /* Compare the CRC value to the expected one */
- if (uwCRCValue != uwExpectedCRCValue_3)
- {
- /* Wrong CRC value: Turn LED3 on */
- Error_Handler();
- }
- /****************************************************************************/
- /* */
- /* CRC computation of the last bytes stream */
- /* */
- /****************************************************************************/
- /* The 7-bit long CRC of a 2-byte buffer is computed. The CRC calculator
- is re-initialized with the default value that is 0x7F for a 7-bit CRC.
- This is done with a call to HAL_CRC_Calculate() instead of
- HAL_CRC_Accumulate(). */
- uwCRCValue = HAL_CRC_Calculate(&CrcHandle, (uint32_t *)&CRC7_DATA8_TEST2, BUFFER_SIZE_2);
- /* Compare the CRC value to the expected one */
- if (uwCRCValue != uwExpectedCRCValue_4)
- {
- /* Wrong CRC value: Turn LED3 on */
- Error_Handler();
- }
- else
- {
- /* Right CRC value: Turn LED1 on */
- BSP_LED_On(LED1);
- }
- /* Infinite loop */
- while (1)
- {
- }
- }
复制代码 我找到一个例子,代码如下:
- # include <stdio.h>
- # include <string.h>
- #define uint8_t unsigned char
- #define uint16_t unsigned int
- #define uint32_t unsigned long int
- #define TAB_LEN 256
- #define ALPHA 0x65
- #define BUFFER_SIZE_5 5 /* CRC7_DATA8_TEST5[] is 5-byte long */
- #define BUFFER_SIZE_17 17 /* CRC7_DATA8_TEST17[] is 17-byte long */
- #define BUFFER_SIZE_1 1 /* CRC7_DATA8_TEST1[] is 1-byte long */
- #define BUFFER_SIZE_2 2 /* CRC7_DATA8_TEST2[] is 2-byte long */
- const uint8_t CRC7_DATA8_TEST5[5] = {0x12,0x34,0xBA,0x71,0xAD};
- const uint8_t CRC7_DATA8_TEST17[17] = {0x12,0x34,0xBA,0x71,0xAD,
- 0x11,0x56,0xDC,0x88,0x1B,
- 0xEE,0x4D,0x82, 0x93,0xA6,
- 0x7F,0xC3};
- const uint8_t CRC7_DATA8_TEST1[1] = {0x19};
- const uint8_t CRC7_DATA8_TEST2[2] = {0xAB,0xCD};
- const unsigned char testdat[10] = "0123456789";
-
- /* Expected CRC Values */
- /* The 7 LSB bits are the 7-bit long CRC */
- uint32_t uwExpectedCRCValue_1 = 0x00000057; /* First byte stream CRC */
- uint32_t uwExpectedCRCValue_2 = 0x0000006E; /* Second byte stream CRC */
- uint32_t uwExpectedCRCValue_3 = 0x0000004B; /* Third byte stream CRC */
- uint32_t uwExpectedCRCValue_4 = 0x00000026; /* Fourth byte stream CRC */
- unsigned char result;
- int table_gen8(unsigned char *buf){
- unsigned int alpha = ALPHA; //x^7+x^3+1
- int i,j;
- unsigned char tmp;
- for(i=0;i<TAB_LEN;i++){
- tmp = i;
- for(j=0;j<8;j++){
- if(tmp & 0x80)
- tmp ^= alpha;
- tmp <<= 1;
- }
- buf[i] = tmp>>1; /*余数为7bit,计算中用了8bit,结尾多一位0要去掉*/
- }
- return 0;
- }
- uint8_t checkCRC7(uint8_t *data, uint32_t len){
- uint8_t crc = 0x7F;
- uint8_t crcP = 0x65;
- uint8_t i, j;
-
- for ( i = 0; i < len; i++){
- crc ^= data[i];
- for (j = 0; j < 8; j++){
- if ( crc &0x80){
- crc ^= 0x65;
- }
- crc = crc <<1;
- }
- }
-
- crc = crc >> 1;
- return crc;
- }
- unsigned char get_crc7(unsigned char start, const unsigned char *buff, int len, unsigned char *table){
- unsigned char accu = start;
- //unsigned char accu = (start<<1);
- unsigned int i= 0;
- for (i=0; i < len; i++) {
- accu = table[(accu << 1) ^ buff[i]];
- //accu = table[accu^ buff[i]];
- }
- return (accu);
- }
- int main(void){
- unsigned char data[TAB_LEN] = {0};
- int i,j;
- printf("CRC7 table:\n");
- table_gen8(data);
- /* for(i=0;i<TAB_LEN/8;i++){
- for(j=0;j<8;j++)
- printf("0x%02x ",data[i*8+j]);
- printf("\n");
- }
- printf("\n");*/
- /*Test*/
-
- //result = get_crc7(0x7f, CRC7_DATA8_TEST5, 5, data);
- //result = get_crc7(0x7f, CRC7_DATA8_TEST17, 17, data);
- //result = get_crc7(0x7f, CRC7_DATA8_TEST1, 1, data);
- //result = get_crc7(0x7f, CRC7_DATA8_TEST2, 2, data);
- printf("get_crc7:0x%02x\n",result);
- return 0;
- }
复制代码 一共四组数据,第一组和第四组值是对的。而第二组和第三组值不对。
本来应该第二组是0x5E,第三组是0x4b.实测值为0x26和0x2f。
请问高手,谁知道软件如何计算出正确的值?或者我写的这个程序哪里有问题?谢谢!
|
|