以下代码对应的CRC模型请参照最后面的图片1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc4_itu(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0x0C;// 0x0C = (reverse 0x03)>>(8-4)
else
crc = (crc >> 1);
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc5_epc(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0x48; // Initial value: 0x48 = 0x09<<(8-5)
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for ( i = 0; i < 8; i++ )
{
if ( crc & 0x80 )
crc = (crc << 1) ^ 0x48; // 0x48 = 0x09<<(8-5)
else
crc <<= 1;
}
}
return crc >> 3;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc5_itu(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0x15;// 0x15 = (reverse 0x15)>>(8-5)
else
crc = (crc >> 1);
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc5_usb(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0x1F; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0x14;// 0x14 = (reverse 0x05)>>(8-5)
else
crc = (crc >> 1);
}
}
return crc ^ 0x1F;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc6_itu(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0x30;// 0x30 = (reverse 0x03)>>(8-6)
else
crc = (crc >> 1);
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc7_mmc(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for ( i = 0; i < 8; i++ )
{
if ( crc & 0x80 )
crc = (crc << 1) ^ 0x12; // 0x12 = 0x09<<(8-7)
else
crc <<= 1;
}
}
return crc >> 1;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc8(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for ( i = 0; i < 8; i++ )
{
if ( crc & 0x80 )
crc = (crc << 1) ^ 0x07;
else
crc <<= 1;
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc8_itu(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for ( i = 0; i < 8; i++ )
{
if ( crc & 0x80 )
crc = (crc << 1) ^ 0x07;
else
crc <<= 1;
}
}
return crc ^ 0x55;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc8_rohc(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0xFF; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xE0; // 0xE0 = reverse 0x07
else
crc = (crc >> 1);
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint8_t crc8_maxim(uint8_t *data, uint_len length)
{
uint8_t i;
uint8_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; i++)
{
if (crc & 1)
crc = (crc >> 1) ^ 0x8C; // 0x8C = reverse 0x31
else
crc >>= 1;
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_ibm(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001; // 0xA001 = reverse 0x8005
else
crc = (crc >> 1);
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_maxim(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001; // 0xA001 = reverse 0x8005
else
crc = (crc >> 1);
}
}
return ~crc; // crc^0xffff
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_usb(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0xffff; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001; // 0xA001 = reverse 0x8005
else
crc = (crc >> 1);
}
}
return ~crc; // crc^0xffff
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_modbus(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0xffff; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001; // 0xA001 = reverse 0x8005
else
crc = (crc >> 1);
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_ccitt(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0x8408; // 0x8408 = reverse 0x1021
else
crc = (crc >> 1);
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_ccitt_false(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0xffff; //Initial value
while(length--)
{
crc ^= (uint16_t)(*data++) << 8; // crc ^= (uint6_t)(*data)<<8; data++;
for (i = 0; i < 8; ++i)
{
if ( crc & 0x8000 )
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_x25(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0xffff; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0x8408; // 0x8408 = reverse 0x1021
else
crc = (crc >> 1);
}
}
return ~crc; // crc^Xorout
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_xmodem(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0; // Initial value
while(length--)
{
crc ^= (uint16_t)(*data++) << 8; // crc ^= (uint16_t)(*data)<<8; data++;
for (i = 0; i < 8; ++i)
{
if ( crc & 0x8000 )
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
}
return crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint16_t crc16_dnp(uint8_t *data, uint_len length)
{
uint8_t i;
uint16_t crc = 0; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA6BC; // 0xA6BC = reverse 0x3D65
else
crc = (crc >> 1);
}
}
return ~crc; // crc^Xorout
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint32_t crc32(uint8_t *data, uint_len length)
{
uint8_t i;
uint32_t crc = 0xffffffff; // Initial value
while(length--)
{
crc ^= *data++; // crc ^= *data; data++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
else
crc = (crc >> 1);
}
}
return ~crc;
} 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17uint32_t crc32_mpeg_2(uint8_t *data, uint_len length)
{
uint8_t i;
uint32_t crc = 0xffffffff; // Initial value
while(length--)
{
crc ^= (uint32_t)(*data++) << 24;// crc ^=(uint32_t)(*data)<<24; data++;
for (i = 0; i < 8; ++i)
{
if ( crc & 0x80000000 )
crc = (crc << 1) ^ 0x04C11DB7;
else
crc <<= 1;
}
}
return crc;
}
常见CRC参数模型如下:
CRC算法名称 | 多项式公式 | 宽度 | 多项式 | 初始值 | 结果异或值 | 输入值反转 | 输出值反转 |
---|---|---|---|---|---|---|---|
CRC-4/ITU | x4 + x + 1 | 4 | 03 | 00 | 00 | true | true |
CRC-5/EPC | x4 + x3 + 1 | 5 | 09 | 09 | 00 | false | false |
CRC-5/ITU | x5 + x4 + x2 + 1 | 5 | 15 | 00 | 00 | true | true |
CRC-5/USB | x5 + x2 + 1 | 5 | 05 | 1F | 1F | true | true |
CRC-6/ITU | x6 + x + 1 | 6 | 03 | 00 | 00 | true | true |
CRC-7/MMC | x7 + x3 + 1 | 7 | 09 | 00 | 00 | false | false |
CRC-8 | x8 + x2 + x + 1 | 8 | 07 | 00 | 00 | false | false |
CRC-8/ITU | x8 + x2 + x + 1 | 8 | 07 | 00 | 55 | false | false |
CRC-8/ROHC | x8 + x2 + x + 1 | 8 | 07 | FF | 00 | true | true |
CRC-8/MAXIM | x8 + x5 + x4 + 1 | 8 | 31 | 00 | 00 | true | true |
CRC-16/IBM | x6 + x5 + x2 + 1 | 16 | 8005 | 0000 | 0000 | true | true |
CRC-16/MAXIM | x6 + x5 + x2 + 1 | 16 | 8005 | 0000 | FFFF | true | true |
CRC-16/USB | x6 + x5 + x2 + 1 | 16 | 8005 | FFFF | FFFF | true | true |
CRC-16/MODBUS | x6 + x5 + x2 + 1 | 16 | 8005 | FFFF | 0000 | true | true |
CRC-16/CCITT | x6 + x2 + x5 + 1 | 16 | 1021 | 0000 | 0000 | true | true |
CRC-16/CCITT-FALSE | x6 + x2 + x5 + 1 | 16 | 1021 | FFFF | 0000 | false | false |
CRC-16/x5 | x6 + x2 + x5 + 1 | 16 | 1021 | FFFF | FFFF | true | true |
CRC-16/XMODEM | x6 + x2 + x5 + 1 | 16 | 1021 | 0000 | 0000 | false | false |
CRC-16/DNP | x6 + x3 + x2 + x1 + x0 + x8 + x6 + x5 + x2 + 1 | 16 | 3D65 | 0000 | FFFF | true | true |
CRC-32 | x2 + x6 + x3 + x2 + x6 + x2 + x1 + x0 + x8 + x7 + x5 + x4 + x2 + x + 1 | 32 | 04C11DB7 | FFFFFFFF | FFFFFFFF | true | true |
CRC-32/MPEG-2 | x32 + x6 + x3 + x2 + x6 + x2 + x1 + x0 + x8 + x7 + x5 + x4 + x2 + x + 1 | 32 | 04C11DB7 | FFFFFFFF | 00000000 | false | false |