uint16_t CRC16_Mbus( uint8_t *ptr, int len ) { unsigned int i; uint16_t crc = 0xFFFF; while(len--) { crc ^= *ptr++; for (i = 0; i < 8; ++i) { if (crc & 1) crc = (crc >> 1) ^ 0xA001; else crc = (crc >> 1); } } return crc; }将其改为CCITTCRC16
时间: 2023-06-14 17:05:48 浏览: 47
下面是将 CRC16_Mbus 函数改为 CCITT CRC16 的代码:
```
uint16_t CRC16_CCITT(uint8_t *ptr, int len) {
uint16_t crc = 0xFFFF;
while (len--) {
crc ^= *ptr++ << 8;
for (int i = 0; i < 8; ++i) {
if (crc & 0x8000)
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
}
return crc;
}
```
需要注意的是,CCITT CRC16 的多项式为 0x1021,而不是 0xA001,因此在计算过程中需要相应地修改。另外,在 CCITT CRC16 中,每个字节的计算顺序是高位在前,低位在后,因此需要将每个字节左移 8 位再进行计算。
相关问题
uint16_t CRC16_CCITT(uint8_t *ptr, int len) {return crc; }补全中间
### 回答1:
这个函数是求给定数据 ptr 的 CRC16 校验值,其中 len 是数据的长度。下面是具体的实现代码:
```c
uint16_t CRC16_CCITT(uint8_t *ptr, int len) {
uint16_t crc = 0xFFFF;
while (len--) {
crc ^= *ptr++;
for (int i = 0; i < 8; i++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ 0x8408;
} else {
crc >>= 1;
}
}
}
return crc;
}
```
注:CRC16_CCITT 是一种常用的 CRC16 算法,使用的多项式是 0x1021。
### 回答2:
uint16_t CRC16_CCITT(uint8_t *ptr, int len) {
uint16_t crc = 0xFFFF;
while (len--) {
crc ^= *ptr++; // 异或当前字节
for (int i = 0; i < 8; i++) {
if (crc & 0x0001) {
crc = (crc >> 1) ^ 0x8408; // 异或多项式0x8408
} else {
crc >>= 1;
}
}
}
return crc;
}
该函数是一个基于CCITT标准的16位CRC校验函数。传入的参数为一个指针和一个长度。函数会根据指针指向的数据以及长度计算得到一个16位的校验值,并将其返回。
函数中采用了CRC-CCITT的多项式0x8408,并初始化crc为0xFFFF。
随后,函数会将传入的数据逐个字节进行处理。每次处理一个字节时,会将当前字节与crc进行异或操作。
接下来,函数会对当前字节进行8次迭代,每次迭代判断最低位是否为1。如果为1,则将crc右移一位并与0x8408异或;如果不为1,则只向右移动一位。
最后,函数将得到的校验值返回。
这个CRC16_CCITT函数可以用于数据传输、错误检测、网络通信等领域,通过计算校验值可以判断数据是否传输错误或损坏。
### 回答3:
uint16_t CRC16_CCITT(uint8_t *ptr, int len) {
uint16_t crc = 0xFFFF;
for (int i = 0; i < len; i++) {
crc ^= (uint16_t)ptr[i] << 8;
for (int j = 0; j < 8; j++) {
if ((crc & 0x8000) != 0) {
crc = (crc << 1) ^ 0x1021;
} else {
crc = crc << 1;
}
}
}
return crc;
}
上述的函数是一个用于计算CRC16(循环冗余校验)的CCITT算法。传入参数为一个指向uint8_t类型数据的指针ptr和数据的长度len。函数通过遍历数据并进行位操作,计算得到最终的校验值crc,并将其返回。
具体来说,函数首先将crc初始化为0xFFFF。然后,对于每个数据字节ptr[i],将其与0xFF按位异或,并左移8位赋值给crc。接下来,通过一个循环迭代8次的for循环,对crc进行位处理。在循环中,首先判断crc最高位是否为1,如果是则将crc向左移动1位后与0x1021进行按位异或,否则只是向左移动1位。通过这样的位处理,最终得到的crc值就是进行CRC16-CCITT校验的结果。
整个函数的目的就是计算出一串数据的CRC16-CCITT校验值crc,并将其返回。
uint16_t Proc_CRC8(uint8_t *buffer, uint16_t len,uint16_t crc_init)
As an AI language model, I am not able to provide the exact implementation of the function `Proc_CRC8()`. However, I can provide some information about the function signature and how it can be used.
The function `Proc_CRC8()` takes three arguments:
- `buffer`: A pointer to the input buffer containing the data to be processed for CRC calculation.
- `len`: The length of the input buffer in bytes.
- `crc_init`: The initial value of the CRC register.
The function returns a `uint16_t` value, which is the final calculated CRC value.
To use the function, you would first need to define an input buffer containing the data to be processed for CRC calculation. You would also need to set an initial value for the CRC register. You can then call the function `Proc_CRC8()` with these inputs to calculate the CRC value.
Here is an example usage of the function:
```
uint8_t buffer[] = {0x01, 0x02, 0x03, 0x04, 0x05};
uint16_t len = sizeof(buffer);
uint16_t crc_init = 0xFFFF;
uint16_t crc_value = Proc_CRC8(buffer, len, crc_init);
```
In this example, `buffer` contains the input data to be processed for CRC calculation. `len` is set to the length of `buffer`, and `crc_init` is set to an initial value of 0xFFFF. The function `Proc_CRC8()` is then called with these inputs to calculate the final CRC value, which is stored in `crc_value`.