CRC16 Sample Code

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

//For Gen2 Tag CRC-16

//Polynomial: x^16 + x^12 + x^5 + 1 (0x1021)<br>


//Initial value: 0xFFFF
//Residue: 0x1D0F

/************************************************************************/
/* for MCU */
/************************************************************************/
uint16_t crc16(uint8_t *add, uint16_t length16){
uint16_t crc;
uint16_t i;
crc = 0xFFFF;
for(i=0; i<length16; i++) {
if((i%8) == 0) crc ^= ((uint16_t)(*add++) << 8);
if(crc & 0x8000) crc = (crc<<1) ^ 0x1021;
else crc <<= 1;
}
return crc;
}

////Verify CRC-16 example


//int verify_crc16(uint8_t *pcEpcCrc, uint16_t bitLength)
//{
// uint16_t crc;
//
// crc = crc16(pcEpcCrc,bitLength);
// if(crc == 0x1D0F)
// return SUCCESS;
// else
// return -1;
//}

/************************************************************************/
/* for C++ MFC */
/************************************************************************/
UINT CRC16(CByteArray &_data)
{
UINT crc16 = 0xFFFF;
UINT x = 0, y = 0;
if (_data.IsEmpty()) return 0;
if (_data[0] == 0)
{
x = 8; y = 1;
}
for (int i = x, j = y; i < _data.GetSize() * 8; i++)
{
if ((i % 8) == 0)
crc16 ^= (_data[j++] << 8) & 0xFF00;
if ((crc16 & 0x8000) != 0)
crc16 = (((crc16 << 1) & 0xFFFE) ^ 0x1021);
else
{
crc16 <<= 1;
crc16 &= 0xFFFE;
}
}
crc16 &= 0xFFFF;
return crc16;
}

/************************************************************************/
/* for C# */
/************************************************************************/
static int CRC16(byte[] pBytes)
{
int crc16 = 0xFFFF;
int x = 0, y = 0;
if (pBytes == null) return 0;
if (pBytes[0] == 0)
{
x = 8; y = 1;
}
for (int i = x, j = y; i < pBytes.Length * 8; i++)
{
if ((i % 8) == 0) crc16 ^= (pBytes[j++] << 8) & 0xFF00;
if ((crc16 & 0x8000) != 0) crc16 = (((crc16 << 1) & 0xFFFE) ^ 0x1021);
else
{
crc16 <<= 1;
crc16 &= 0xFFFE;
}
}
crc16 &= 0xFFFF;
return crc16;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy