我想实现CRC-16/X-25。我有一个用于CRC-16/CCITT_FALSE的C代码,但是我不知道为了实现CRC-16/X-25,我需要对该代码进行哪些更改?
下面给出了我用于CRC-16/CCITT-FALSE的代码:
#include <msp430.h>
#define CRC_POLYNOMIAL 0x1021
#define USHORT_TOPBIT 0x8000
short crc_ret;
// function initialisation
unsigned short crc16(const char *buf, unsigned int buf_len)
{
unsigned short ret = 0xFFFF; // Initial Value
int bit = 8;
do {
if (!buf) {
break;
}
unsigned int i = 0;
for (i=0; i<buf_len; ++i) {
ret = (unsigned short)(ret ^ ((unsigned short)buf[i] >> 8));
for (bit = 8; bit > 0; --bit)
{
if (ret & USHORT_TOPBIT)
ret = (unsigned short)((ret >> 1) ^ CRC_POLYNOMIAL);
else
ret = (unsigned short)(ret >> 1);
}
}
} while (0);
return ret;
}
int main(void)
{
PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
char buf[16];
buf[0] = 0x5A; //buf[0], buf[1] and buf[2] are data bytes
buf[1] = 0xCF;
buf[2] = 0x00;
crc_ret = crc16(buf, 3); // Calling CRC function
}
发布于 2019-11-09 00:34:16
根据下面的网站,CRC16/CCITT_FALSE与您的代码不匹配。网站指出它是一个左移(非反射) CRC,而你的代码是右移。
CRC16/X25是右移(反射) CRC,返回~CRC (CRC xor 0xFFFF)。我不知道这是否与CRC16/CCITT_X25相同。
http://www.sunshine2k.de/coding/javascript/crc/crc_js.html
您可以使用该网站将您的结果与其结果进行比较。
我从一个简单的示例开始创建两个crc16函数:
uint16_t crc16_ccitt_false(char* pData, int length)
{
int i;
uint16_t wCrc = 0xffff;
while (length--) {
wCrc ^= *(unsigned char *)pData++ << 8;
for (i=0; i < 8; i++)
wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ 0x1021 : wCrc << 1;
}
return wCrc;
}
uint16_t crc16_x25(char* pData, int length)
{
int i;
uint16_t wCrc = 0xffff;
while (length--) {
wCrc ^= *(unsigned char *)pData++ << 0;
for (i=0; i < 8; i++)
wCrc = wCrc & 0x0001 ? (wCrc >> 1) ^ 0x8408 : wCrc >> 1;
}
return wCrc ^ 0xffff;
}
https://stackoverflow.com/questions/58768018
复制相似问题