
MSR300 Safety System Manual
Rockwell Automation
MSR300 Manual.doc
Pg 81 of 85
Example 1:
The following example calculates the CRC using the method described earlier.
Note:
This function performs the swapping of the high/low CRC bytes internally.
Therefore the CRC value returned from the function can be directly placed into the
message for transmission.
The function returns the CRC as a type UINT16, and takes two arguments:
• UINT8 *pabMessage;
A pointer to the message buffer containing binary data to be used for generating the
CRC.
• UINT16 iLength;
The quantity of bytes in the message buffer.
Typedefs
UINT8 = Unsigned 8 bit (e.g. unsigned char)
UINT16 = Unsigned 16 bit (e.g. unsigned short)
Source Code
UINT16 GenerateCrc( UINT8* pabMessage, UINT16 iLength )
{
UINT16 iCRCReg = 0xFFFF;
UINT8
bByteCount;
UINT8
bBitCount,
bCarryFlag;
for( bByteCount = 0 ; bByteCount < iLength ; bBy+ )
{
*((UINT8*)&i 1) = pabMessage[ bByteCount ] ^ ( iCRCReg &
0x00FF
);
for( bBitCount = 0; bBitCount < 8 ; bB+ )
{
bCarryFlag
=
iCRCReg
&
0x0001;
iCRCReg = iCRCReg >> 1;
if(
bCarryFlag
!=
0
)
{
iCRCReg
^=
0xA001;
}
}
}
return( iCRCReg << 8 | iCRCReg >> 8 );
}/* end GenerateCrc */