CRC8与CRC3的程序实现(C语言)

  • Post author:
  • Post category:其他


下面两部分的程序可以在Dev-C++上新建Console Application项目后运行。

1、CRC-8

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//CRC(X) = X^8 + X^4 + X^3 + X^2 + 1  ----0x1D;
//CRC(X) = X^8 + X^5 + X^3 + X^2 + X^1 + 1  ----0x2F.

unsigned char u1g_crc8;

unsigned char input_data(unsigned char *arr)
{
	unsigned char i = 0;
	char ch;
		
    printf("输入16进制数据,用空格分隔\n");
    do
    {
        scanf("%X", &arr[i++]);	
	}while((ch = getchar()) != 10); // 这里用来判断是否输入了回车 
	
	//printf("i = %d\n", i); //调试用	
	return i;		
}

void crc_calculate(void)
{
	unsigned char u1t_i;
    unsigned char u1t_j;
    unsigned char u1t_crc8;
    unsigned char u1t_poly;
    unsigned char arr_temp[8] = {0};
    unsigned char len;
           
    /* put your own code here */
    //u1g_len=sizeof(u1g_data);

    printf("++++++++++++\n");
	printf("CRC-8计算\n");
	
	printf("\n");
    
    printf("输入16进制poly值(1D/2F): ") ;
	scanf("%X", &u1t_poly);
	
//	while((u1t_poly != 0x1D) && (u1t_poly != 0x2F))
//	{
//		printf("请输入正确的poly值(1D/2F): ") ;
//	    scanf("%X", &u1t_poly);	      
//	}		
	
	printf("你输入的poly = 0x%X\n", u1t_poly);
	printf("\n");
    
    len = input_data(arr_temp); //获取到输入的数据 

	u1t_crc8 = 0xFF;

    for(u1t_i=0;u1t_i<len;u1t_i++)
    {
        u1t_crc8^=arr_temp[u1t_i];
        
        for(u1t_j=0;u1t_j<8;u1t_j++)
        {
            if(u1t_crc8&0x80)
            {
                u1t_crc8=(u1t_crc8<<1)^u1t_poly;
            }
            else
            {
                u1t_crc8<<=1;
            
            }
        }
    }
    u1t_crc8^=(unsigned char)0xFF;
    u1g_crc8 =  u1t_crc8;
    
    printf("\n");
	printf("CRC计算结果为:CRC = 0x\%X\n", u1g_crc8);
	printf("--------------------------------------\n");
	
}
	
int main (void) 
{
	unsigned char cmd_temp;
	
	for(;;)
	{
		crc_calculate();
	    printf("是否继续计算(y/n): ");
	    scanf("%s", &cmd_temp);	
	    printf("\n\n");
	    if(cmd_temp != 'y')
	    {
	        break;	
		}
	}
	
	return 0;
}


2、CRC-3

#include <stdio.h>
#include <stdlib.h>
#include "stdbool.h"

unsigned char u1g_crc8;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//CRC(X) = X^3 + X^1 + X^0 ----0x03

unsigned char input_data(unsigned char *arr)
{
	unsigned char i = 0;
	char ch;
		
    printf("输入16进制数据,用空格分隔\n");
    do
    {
        scanf("%X", &arr[i++]);	
	}while((ch = getchar()) != 10); // 这里用来判断是否输入了回车 
	
	//printf("i = %d\n", i); //调试用	
	return i;		
}

void crc_calculate(void)
{
	unsigned char u1t_i;
    unsigned char u1t_j;
    unsigned char u1t_crc8;
    unsigned char u1t_poly;
    unsigned char arr_temp[8] = {0};
    unsigned char len;
           

    printf("++++++++++++\n");
	printf("CRC计算\n");
	
	printf("\n");
    
//    printf("输入16进制poly值(1D/2F): ") ;
//	scanf("%X", &u1t_poly);
	
//	while((u1t_poly != 0x1D) && (u1t_poly != 0x2F))
//	{
//		printf("请输入正确的poly值(1D/2F): ") ;
//	    scanf("%X", &u1t_poly);	      
//	}		
	
//	printf("你输入的poly = 0x%X\n", u1t_poly);
//	printf("\n");
    
    u1t_poly = 0x03;
    u1t_poly = u1t_poly << 5;
    
    len = input_data(arr_temp); //获取到输入的数据 

//	u1t_crc8 = 0xFF;
    u1t_crc8 = 0x00;

    for(u1t_i=0;u1t_i<len;u1t_i++)
    {
        u1t_crc8^=arr_temp[u1t_i];
        
        for(u1t_j=0;u1t_j<8;u1t_j++)
        {
            if(u1t_crc8&0x80)
            {
                u1t_crc8=(u1t_crc8<<1)^u1t_poly;
            }
            else
            {
                u1t_crc8<<=1;
            
            }
        }
    }
//    u1t_crc8^=(unsigned char)0xFF;
    u1g_crc8 =  u1t_crc8 >> 5; //poly左移了5位,此时计算出的crc在高5位,要右移恢复 
    
    printf("\n");
	printf("CRC计算结果为:CRC = 0x\%X\n", u1g_crc8);
	printf("--------------------------------------\n");
		
}

int main(int argc, char *argv[]) {
	
	unsigned char u1t_crcResult;
	unsigned char u1t_memoryAddress;
	unsigned int  u2t_data;
	unsigned char cmd_temp;
			
	for(;;)
	{
		crc_calculate();
	    printf("是否继续计算(y/n): ");
	    scanf("%s", &cmd_temp);	
	    printf("\n\n");
	    if(cmd_temp != 'y')
	    {
	        break;	
		}
	}
			
	return 0;
}



版权声明:本文为u013381608原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。