#define m 6
#define BufSize 40
unsigned int TimeClock,Time;
unsigned char receivestat=0;
unsigned char Receivedat;
unsigned char enCode[m*2];
unsigned char UARTrBuf[BufSize];
unsigned char RBufRW=0;
unsigned char RBufRR=0;
void inRBuf(unsigned char dat)
{
UARTrBuf[RBufRW]=dat;
if(++RBufRW >= BufSize ) RBufRW=0;
}
unsigned char OutRBuf(void)
{
unsigned char dat;
dat=UARTrBuf[RBufRR];
if(++RBufRR >= BufSize ) RBufRR=0;
return (dat);
}
unsigned char RBufempty(void)
{
if( RBufRR == RBufRW) return 1;
else return 0;
}
void UART_inter(void) interruput 4
{
unsigned char dat;
if(RI==1)
{
RI=0;
dat=SBUF;
inRBuf(dat);
Time=TimeClock+20;
if(Time>10000) Time-=10000;
}
}
void T0_inter(void) interruput 1
{
if( ++TimeClock > 10000) TimeClock=0;
th0=-10000>>8
TL0=-10000;
}
//*********************************************send
unsigned short CRCPack( unsigned char len,unsigned char crcBuf[]) //
{
unsigned short crc=0;
unsigned short i, len;
for( i=0; i<len; i++ )
{
unsigned short tmp;
tmp = (crcBuf[i] >> 4) & 0x0f;
tmp ^= (crc >> 12) & 0x0f;
tmp |= (tmp << 12) | (tmp << 5);
crc = ( crc << 4 ) ^ tmp;
tmp = (crcBuf[i] >> 0) & 0x0f;
tmp ^= (crc >> 12) & 0x0f;
tmp |= (tmp << 12) | (tmp << 5);
crc = ( crc << 4 ) ^ tmp;
}
return(crc);
}
unsigned short CarcalCRC( unsigned char len,unsigned char dat[])
{
unsigned short crc=0;
unsigned short i, len;
for( i=0; i<len; i++ )
{
unsigned short tmp;
tmp = ( dat[i] >> 4) & 0x0f;
tmp ^= (crc >> 12) & 0x0f;
tmp |= (tmp << 12) | (tmp << 5);
crc = ( crc << 4 ) ^ tmp;
tmp = ( dat[i] >> 0) & 0x0f;
tmp ^= (crc >> 12) & 0x0f;
tmp |= (tmp << 12) | (tmp << 5);
crc = ( crc << 4 ) ^ tmp;
}
return(crc);
}
void Sigle_not_Zero(unsigned char dat,unsigned char msg[] )
{
msg[0]=0;
msg[1]=0;
for(i=0;i<8;i++)
{
if(dat & 0x80) { msg[i/4]<<=2;msg[i/4] |= 0x02;}
else {msg[i/4]<<=2;msg[i/4] |= 0x01;}
dat<<=1;
}
}
void Pack_Send(unsigned char dat)
{
unsigned char puckDat[m];
unsigned char str[2];
unsigned short crc=0;
unsigned char i,k=0;
unsigned char Tmp_dat[2];
Tmp_dat[0]=dat;
if(Tmp_dat != 0x7e)
{
crc=CarcalCRC(1,Tmp_dat);
puckDat[0]=0x7e;
puckDat[1]=dat;
puckDat[2]=crc>>8;
puckDat[3]=crc;
puckDat[4]=0x7e;
for(i=0;i<5;i++)
{
Sigle_not_Zero(puckDat[i],str);
enCode[k]=str[0];
enCode[k+1]=str[1];
k += 2;
}
}
else
{
Tmp_dat[0]=0x5E;
Tmp_dat[1]=0x7d;
crc=CarcalCRC(2,Tmp_dat);
puckDat[0]=0x7e;
puckDat[1]=0x5e;
puckDat[2]=0x7d;
puckDat[3]=crc>>8;
puckDat[4]=crc;
puckDat[5]=0x7e;
for(i=0;i<6;i++)
{
Sigle_not_Zero(puckDat[i],str);
enCode[k]=str[0];
enCode[k+1]=str[1];
k += 2;
}
}
}
//**********************************************Receive
unsigned char decode(unsigned char msg[])
{
unsigned char i,sts;
unsigned char temp,dat=0;
temp=msg[0];
for(i=0;i<4;i++)
{
if((temp & 0xc0) == 0x80) {dat<<=1; dta |= 0x01;}
if((temp & 0xc0) == 0x40) {dat<<=1; dta |= 0x00;}
temp <<=2;
}
temp=msg[1];
for(i=0;i<4;i++)
{
if((temp & 0xc0) == 0x80) {dat<<=1; dta |= 0x01;}
if((temp & 0xc0) == 0x40) {dat<<=1; dta |= 0x00;}
temp <<=2;
}
}
unsigned char unpack(void)
{
unsigned char msg[2];
unsigned cahr flag=0,ReciveDat[6];
static unsigned char i=0,k=0,sts=0;
if(RBufempty())
{
if((Time==TimeClock)&&(receivestat==1)) { sts=0; k=0;}
return 1;
}
receivestat=1;
msg[i]=OutRBuf();
if(++i < 2) return 1;
i=0;
ReciveDat[k]=decode(msg);
k++;
switch(sts)
{
case 0:
if(ReciveDat[k] != 0x7e) k=0;
else sts=1;
break;
case 1:
if(ReciveDat[k] != 0x7e)
{ if(k>=6) { sts=0; k=0;}}
else
{sts=2;k=0;receivestat=0;}
break;
case 2:
if( ReciveDat[1] ! =0x5e)
{
Receivedat=ReciveDat[1];
ReciveDat[0]=ReciveDat[1];
ReciveDat[1]=ReciveDat[2];
ReciveDat[2]=ReciveDat[3];
flag=3;
}
else
{
Receivedat=0x7e;
ReciveDat[0]=ReciveDat[1];
ReciveDat[1]=ReciveDat[2];
ReciveDat[2]=ReciveDat[3];
ReciveDat[3]=ReciveDat[4];
flag=4;
}
sts=0;
break;
}
if(flag != 0 )
{
if( CRCPack(flag,ReciveDat) == 0) { falg=0;return 0;}
else { falg=0; return 2; }
}
}
crc.rar_crc
版权申诉
177 浏览量
2022-09-24
01:08:18
上传
评论
收藏 1KB RAR 举报
![avatar](https://profile-avatar.csdnimg.cn/9d774e17dae94b1287526d89d7503a61_weixin_42659194.jpg!1)
刘良运
- 粉丝: 71
- 资源: 1万+
评论0