void swuart_calcCRC(uint8_t* datagram, uint8_t datagramLength)
{
int i, j;
uint8_t* crc = datagram + (datagramLength - 1); // CRC located in last byte of message
uint8_t currentByte;
*crc = 0;
for (i = 0; i < (datagramLength - 1); i++) { // Execute for all bytes of a message
currentByte = datagram; // Retrieve a byte to be sent from Array
for (j = 0; j < 8; j++) {
if ((*crc >> 7) ^ (currentByte & 0x01)) // update CRC based result of XOR operation
{
*crc = (*crc << 1) ^ 0x07;
}
else
{
*crc = (*crc << 1);
}
currentByte = currentByte >> 1;
} // for CRC bit
} // for message byte
}
void data_dump(uint8_t* buff, uint8_t length)
{
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
for (int i = 0; i < length; i++) {
if (i % 5 == 0) {
printf("\r\n");
}
printf("%02x ", buff);
}
if ( (length-1) % 5 != 0) {
std::cout << std::endl;
}
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\r\n");
}
int main()
{
std::cout << "Hello!, It is a CRC8-ATM demo\n";
std::cout << "data in test_data1 is: \r\n";
swuart_calcCRC(test_data1, 10);
data_dump(test_data1, 10);
std::cout << "data in test_data2 is: \r\n";
swuart_calcCRC(test_data2, 10);
data_dump(test_data2, 10);
```
uint8_t test_data1[10] = { 0x01 ,0x23 ,0x45 ,0x67 ,0x89 ,0x1A ,0xBC ,0xDE ,0xF0, 0x00 };
uint8_t test_data2[10] = { 0xFE ,0xDC ,0xBA ,0x98 ,0x76 ,0x54 ,0x32 ,0x10 ,0xF8, 0x00 };
void swuart_calcCRC(uint8_t* datagram, uint8_t datagramLength)
{
int i, j;
uint8_t* crc = datagram + (datagramLength - 1); // CRC located in last byte of message
uint8_t currentByte;
*crc = 0;
for (i = 0; i < (datagramLength - 1); i++) { // Execute for all bytes of a message
currentByte = datagram; // Retrieve a byte to be sent from Array
for (j = 0; j < 8; j++) {
if ((*crc >> 7) ^ (currentByte & 0x01)) // update CRC based result of XOR operation
{
*crc = (*crc << 1) ^ 0x07;
}
else
{
*crc = (*crc << 1);
}
currentByte = currentByte >> 1;
} // for CRC bit
} // for message byte
}
void data_dump(uint8_t* buff, uint8_t length)
{
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
for (int i = 0; i < length; i++) {
if (i % 5 == 0) {
printf("\r\n");
}
printf("%02x ", buff);
}
if ( (length-1) % 5 != 0) {
std::cout << std::endl;
}
printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\r\n");
}
int main()
{
std::cout << "Hello!, It is a CRC8-ATM demo\n";
std::cout << "data in test_data1 is: \r\n";
swuart_calcCRC(test_data1, 10);
data_dump(test_data1, 10);
std::cout << "data in test_data2 is: \r\n";
swuart_calcCRC(test_data2, 10);
data_dump(test_data2, 10);
}
```
按照这个代码出现的结果如下:
![image.png](data/attachment/forum/202310/28/172354l9921olsr8p2vljp.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
上面的代码时官方的。那怎么配置stm32的参数呐?我们不妨先用在线工具测试一下:
![image.png](data/attachment/forum/202310/28/172516fgadgpev11xgdegn.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
总结如下:输入反转,输出不反转。(反转时LSB在前,否则MSB在前。)初始值时00,多项式是07(X8+X2+X1+X0),结果不异或。
那怎么在stm32里面配置呐?
我猜应该如下:
![image.png](data/attachment/forum/202310/28/175005x7rwdwj7al3wrbdm.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "image.png")
为什么我不验证呐?因为我缺少一块开发板和micro USB线。