dsPIC33EP的硬件CRC校验

#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值不对。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值