#define POLYLEN 0x000F // Length of polynomial-1"
#define POLY 0x1021 // Generator Polynomial
// *****************************************************************************
// *****************************************************************************
// Section: File Scope or Global Constants
// *****************************************************************************
// *****************************************************************************
uint8_t messageString[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
uint16_t crcResult=0;
/******************************************************************************
* Function: int main(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Main function
*****************************************************************************/
#ifdef TEST_MODE
int ce_main(void)
#else
int main(void)
#endif
{
uint16_t prev_CRC = 0x0000;
CRC_Init();
Nop();
Nop();
crcResult = CRC_ChecksumByte( messageString, 8, prev_CRC );
#ifdef TEST_MODE
if(crcResult == EXPECTED_RESULT )
return 0;
else
return 1;
#else
while( 1 );
#endif
}
/******************************************************************************
* Function: void CRC_Init(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: This function configures CRC module
*****************************************************************************/
void CRC_Init( void )
{
CRCCON1bits.CRCEN = 0;
CRCXORL = 0x0000;
Nop();
Nop();
CRCWDATL = 0x0000;
CRCWDATH = 0x0000; // NOTE:Non-direct Initial value fed to CRCWDAT
CRCXORL = POLY; // Generator Polynomial // X^12 + X^5
CRCXORH = 0x0000;
CRCCON1bits.CRCISEL = 0;
CRCCON2bits.PLEN = POLYLEN; // Length of polynomial-1"
CRCCON2bits.DWIDTH = 7;
CRCCON1bits.LENDIAN = 0x0;
CRCCON1bits.CRCEN = 1;
}
/******************************************************************************
* Function: uint16_t CRC_ChecksumByte(uint8_t* data, uint8_t Number_of_bytes, uint16_t prev_CRC)
*
* PreCondition: None
*
* Input: data - Pointer to the first data byte for which CRC needs to be
* calculated.
* Number_of_bytess - Total number of bytes for which CRC needs to be
* calculated.
* prev_CRC - previous CRC result.
*
* Output: CRCWDATL Register content that is the two byte checksum
*
* Side Effects: None
*
* Overview: Calculates the checksum and returns the value
*****************************************************************************/
uint16_t CRC_ChecksumByte( uint8_t *data, uint8_t Number_of_bytes, uint16_t prev_CRC )
{
uint8_t volatile *dest = ( uint8_t * ) &CRCDATL;
CRCWDATL = prev_CRC;
IFS4bits.CRCIF=0; //CRC status Flag is Cleared
do
{
while( (0 == CRCCON1bits.CRCFUL) && (0 < Number_of_bytes) )
{
*dest = *data;
data++;
Number_of_bytes--;
}
} while( 0 < Number_of_bytes );
CRCCON1bits.CRCGO = 1;
CRCDATL = 0x0000; /* Do this to shift the last word out of the CRC shift register */
while( CRCCON1bits.CRCFUL == 1 );
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
while(CRCCON1bits.CRCMPT!=1);
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
while(IFS4bits.CRCIF!=1);
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
CRCCON1bits.CRCGO=0;
Nop();
Nop();
Nop();
Nop();
Nop();
return ( CRCWDATL );
}
CRCCON1bits.CRCGO = 1;
CRCDATL = 0x0000; /* Do this to shift the last word out of the CRC shift register */
这个 CRCDATL = 0x0000很重要。需要写。当时没写。算出来的CRC值不对。