本帖最后由 hotpower 于 2012-9-22 13:19 编辑
#include "uni_int.h"
#include <stdlib.h>
uint8 crc8_create(uint8 crc_in,const uint8 *data,uint16 size);
uint8 crc8_update_nibble(uint8 crc_in,uint8 data);
uint8 crc8_update_byte(uint8 crc_in,uint8 data);
uint8 crc8_update_word(uint8 crc_in,uint16 data);
uint8 crc8_dis_update_nibble(uint8 crc_out,uint8 data);
uint8 crc8_dis_update_byte(uint8 crc_out,uint8 data);
uint8 crc8_dis_update_word(uint8 crc_in,uint16 data);
volatile uint8 A;
volatile uint8 crc_data[10];
volatile uint8 crc_out;
int main()
{
A=0x55;
A=crc8_update_byte(A,0x21);
A=crc8_dis_update_byte(A,0x21); // 恢复初值
A=0x78;
A=crc8_update_nibble(A,0x1);
A=crc8_update_nibble(A,0x2);
//相当于crc8_dis_update_byte(A,0x21);
A=crc8_dis_update_nibble(A,0x2);
A=crc8_dis_update_nibble(A,0x1);
//相当于A=crc8_dis_update_byte(A,0x21); // 恢复初值
A=0x69;
A=crc8_update_word(A,0x4321);
A=crc8_dis_update_word(A,0x4321); //恢复初值
A=0x69;
A=crc8_update_byte(A,0x21);
A=crc8_update_byte(A,0x43);
A=crc8_dis_update_byte(A,0x43);
A=crc8_dis_update_byte(A,0x21); //恢复初值
A=0x69;
A=crc8_update_nibble(A,0x1);
A=crc8_update_nibble(A,0x2);
A=crc8_update_nibble(A,0x3);
A=crc8_update_nibble(A,0x4);
A=crc8_dis_update_nibble(A,0x4);
A=crc8_dis_update_nibble(A,0x3);
A=crc8_dis_update_nibble(A,0x2);
A=crc8_dis_update_nibble(A,0x1); //恢复初值
//已知CRC8值为0x96,生成10个字节的数,使之CRC校验(初值为0)为0x96
uint8 index=9;
crc_out=0x96;
while(index!=0)
{
crc_data[index]=rand()%256; //随机一个字节
crc_out=crc8_dis_update_byte(crc_out,crc_data[index]); //逆运算,一个字节
index--;
}
//最后前面的一个字节:
crc_out=crc8_dis_update_byte(crc_out,0); //逆运算,一个字节
crc_data[index]=crc_out;
//验证:
crc_out=crc8_create(0,(void *)crc_data,10); //为0x96
asm("nop");
while(1);
} |