CRC-16 / CCITT-FALSE和CRC-16 / X-25有什么区别?

问题描述 投票:0回答:1

我想实现CRC-16 / X-25。我有一个用C编写的CRC-16 / CCITT_FALSE的代码,但我不知道我需要在该代码中对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

}

smartcard crc nxp-microcontroller
1个回答
0
投票

根据下面的网站,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;
}
© www.soinside.com 2019 - 2024. All rights reserved.