[ create a new paste ] login | about

Link: http://codepad.org/jPPW1CQ4    [ raw code | output | fork ]

C++, pasted on Mar 12:
static unsigned char CalculateCRC(unsigned short int input)
{
	bool CRC0 = true;
	bool CRC1 = true;
	bool CRC2 = true;
	bool CRC3 = true;
	int i;
	bool DoInvert;
	uint16_t mask = 0x8000;

	for (i = 0; i < 16; ++i)
	{
		DoInvert = ((input & mask) != 0) ^ CRC3; // XOR required?
		CRC3 = CRC2;
		CRC2 = CRC1;
		CRC1 = CRC0 ^ DoInvert;
		CRC0 = DoInvert;
		mask >>= 1;
	}
	return (CRC3 ? 8U : 0U) + (CRC2 ? 4U : 0U) + (CRC1 ? 2U : 0U) + (CRC0 ? 1U : 0U);
}


int main(void)
{
	unsigned char SPI_CRC;

	unsigned short int SPImessage;

	SPImessage = 0x45A4; // should give CRC = 0xB
	SPI_CRC = CalculateCRC(SPImessage);
	printf("CRC = 0x%X\n", SPI_CRC);

	SPImessage = 0x4B1F;  // should give CRC = 0x1
	SPI_CRC = CalculateCRC(SPImessage);
	printf("CRC = 0x%X\n", SPI_CRC);

	return 0;
}


Output:
1
2
CRC = 0xB
CRC = 0x1


Create a new paste based on this one


Comments: