beautypg.com

KROHNE MFC 010 C Converter User Manual

Page 93

background image

MFC010 Interface Manual

91

unsigned short Calculate_Checksum (unsigned char uc_Telegram_Length, unsigned short *Buffer_Pointer )

{

// Declare Local Variables

unsigned char uc_Byte_Loop;

unsigned char uc_Bit_Loop;

unsigned short us_Temporary_Checksum;

// Initialise the checksum value

unsigned short us_Checksum = 0xFFFF;

// Loop through each byte of the telegram Buffer

for (uc_Byte_Loop = 0; uc_Byte_Loop < uc_ Telegram_Length; uc_Byte_Loop++)

{

// XOR the newly indexed buffer location with the current value of the checksum

us_Checksum ^= (*(Buffer_Pointer + uc_Byte_Loop) & 0x00FF);

// Perform an 8 bit rotation and Polynomial addition on the checksum

for(uc_Bit_Loop = 0; uc_Bit_Loop < 8; uc_Bit_Loop++)

{

// Store the current value of the checksum in a temporary local variable

us_Temporary_Checksum = us_Checksum;

// Right shift the Checksum by one bit

us_Checksum >>= 1;

// Was the first bit of he checksum set?

if (us_Temporary_Checksum & 0x0001)

{

// If so, XOR the Checksum with the Polynomial value

us_Checksum ^= 0xA001;

}

}

}

// Return the calculated checksum result

return us_Checksum;

}