⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crc.c

📁 基于无线单片机24e1的组网程序。在无线传感器之类的应用中的成功案例。
💻 C
字号:
#include "def.h"
extern void SendCh(U8);
unsigned int code crc_ta[16]=
{ /* CRC余式表 */
         0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
         0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
};

union
{
	U16 iWord;
	struct
	{
		U8 cHigh;
		U8 cLow;
	}iData;
}CrcTemp;

U8 count;


BOOL RxdCrc( U8* p );
void TxdCrc( U8* p );
void Crc( U8* p );

void Crc(U8 *ptr)
{
        unsigned char da;
        U8 i;
        CrcTemp.iWord = 0xffff;
        for( i=0; i<count; i++ )
        {
                da=CrcTemp.iData.cHigh >> 4; /* 暂存CRC的高四位 */
                
                CrcTemp.iWord <<= 4; /* CRC右移4位,相当于取CRC的低12位)*/
                
                CrcTemp.iWord ^= crc_ta[da^(*ptr/16)]; /* CRC的高4位和本字节的前半字节相加后查表计算CRC,
                
                然后加上上一次CRC的余数 */
                da = CrcTemp.iData.cHigh >>4 ; /* 暂存CRC的高4位 */
                CrcTemp.iWord <<= 4; /* CRC右移4位, 相当于CRC的低12位) */
                CrcTemp.iWord ^= crc_ta[da^(*ptr&0x0f)]; /* CRC的高4位和本字节的后半字节相加后查表计算CRC,
                然后再加上上一次CRC的余数 */
                ptr++;
        }
        CrcTemp.iWord ^= 0xffff;
		SendCh(CrcTemp.iData.cHigh);
		SendCh(CrcTemp.iData.cLow);
}

BOOL RxdCrc( U8* p )
{
	count = p[0];
        Crc( p );
	if( CrcTemp.iWord == 0xe2f0 )
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

void TxdCrc( U8* p )
{
	count = p[0]-2;

        Crc( p );
        
	p[count+0] = CrcTemp.iData.cHigh;
	p[count+1] = CrcTemp.iData.cLow;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -