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

📄 rf_manchester.c

📁 51单片机读EM卡的程序,EM卡输出是曼彻斯特吗有.很好用的.
💻 C
📖 第 1 页 / 共 2 页
字号:
				case 5:pBuf5=0;	break;
				case 6:pBuf6=0;	break;
				case 7:pBuf7=0;	break;
			}
		} 
		i=DataHeadbuf.intHigh+DataHeadbuf.intLow-1;
		i=i/8;
		RecvBuf[i]=pBuf;
	}

	i=DataHeadbuf.intHigh+DataHeadbuf.intLow; 
	if(i==55)							//数据读取完毕,发送
	{
		for(i=0;i<7;i++)
		{
			Send_Data(RecvBuf[i]);
		}
		i=Calibration();
		if(i)
			for(i=0;i<7;i++)
			{
				Send_Data(RecvBuf[i]);
				RecvBuf[i]=0x55;
			}
		
		i=P2;
		P2=~i;
		OS_Status=Start_Find;			//发送完毕,置位从新检测
		DataHeadbuf.intHigh=0;
		DataHeadbuf.intLow=0;
		mcsbuf.Head=mcsHeadNull;
	}
}


/*--------------------------------------------------------------------------------
函数:
功能:
引用值:
返回值:
--------------------------------------------------------------------------------*/
uchar Calibration(void)
{
	temp1=RecvBuf[0] & 0x1F;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败1

	RecvBuf[0]=(RecvBuf[0] & 0xF0) | temp1;

	temp1=RecvBuf[0]>>5 | (RecvBuf[1] & 0x03 )<<3;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败2

	RecvBuf[0]=(RecvBuf[0] & 0x0F) | temp1<<4;

	temp1=(RecvBuf[1]>>2) & 0x1F;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败3

	RecvBuf[1]=(RecvBuf[1] & 0xF0) | temp1;

	temp1=RecvBuf[1]>>7 | (RecvBuf[2] & 0x0F)<<1;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败4

	RecvBuf[1]=(RecvBuf[1] & 0x0F) | temp1<<4;

	temp1=(RecvBuf[2]>>4 & 0x0F)| (RecvBuf[3] & 0x01)<<4;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败5

	RecvBuf[2]=(RecvBuf[2] & 0xF0) | temp1;

	temp1=RecvBuf[3]>>1 & 0x1F;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败6

	RecvBuf[2]=(RecvBuf[2] & 0x0F) | temp1<<4;

	temp1=((RecvBuf[3]>>6) & 0x03) | ((RecvBuf[4] <<2) & 0x1c);
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败7

	RecvBuf[3]=(RecvBuf[3] & 0xF0) | temp1;

	temp1=RecvBuf[4]>>3;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败8

	RecvBuf[3]=(RecvBuf[3] & 0x0F) | temp1<<4;

	temp1=RecvBuf[5] & 0x1F;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败9

	RecvBuf[4]=(RecvBuf[4] & 0xF0) | temp1;

	temp1=RecvBuf[5]>>5 | (RecvBuf[6] & 0x03 )<<3;
	temp2=(temp1 & 0x10)>>4;
	temp1=temp1& 0x0F;
	if(temp2!=Adj[temp1])
		return(0);						//校验失败2

	RecvBuf[4]=(RecvBuf[4] & 0x0F) | temp1<<4;

	RecvBuf[5]=(RecvBuf[6]>>2) & 0x0F;

	temp1=0; 
	for(i=0;i<5;i++)
	{
		temp1=temp1^(RecvBuf[i] & 0x01)^((RecvBuf[i] & 0x10) >>4);
	} 
	temp2=RecvBuf[5]&0x01;
	if(temp1!=temp2)
		return(0);

	temp1=0; 
	for(i=0;i<5;i++)
	{
		temp1=temp1^((RecvBuf[i] & 0x02)>>1)^((RecvBuf[i] & 0x20) >>5);
	} 
	temp2=(RecvBuf[5]&0x02)>>1;
	if(temp1!=temp2)
		return(0);

	temp1=0; 
	for(i=0;i<5;i++)
	{
		temp1=temp1^((RecvBuf[i] & 0x04)>>2)^((RecvBuf[i] & 0x40) >>6);
	} 
	temp2=(RecvBuf[5]&0x04)>>2;
	if(temp1!=temp2)
		return(0);

	temp1=0; 
	for(i=0;i<5;i++)
	{
		temp1=temp1^((RecvBuf[i] & 0x08)>>3)^((RecvBuf[i] & 0x80) >>7);
	} 
	temp2=(RecvBuf[5]&0x08)>>3;
	if(temp1!=temp2)
		return(0);
	
	return(1);
}


/*--------------------------------------------------------------------------------
函数:	 HeadFindHandle(uchar HFHData)
功能:	 数据起始字头判断(找出9个连续的1)
引用值:uchar HFHData:由Manchster()解码出的数据(0,1,2,3)
返回值:无
--------------------------------------------------------------------------------*/
void HeadFindHandle(uchar HFHData)
{
	if(HFHData==1)
	{
		DataHeadbuf.intHigh++; 			//是高电平就自动加1,找出字头(9个1)
	}
	else if(HFHData==0)
	{					
		DataHeadbuf.intLow++;			//是低电平判断高电平是否有9个连续的1
		if(DataHeadbuf.intHigh!=9)
		{				
			DataHeadbuf.intHigh=0;		//无连续9个1,说明数据头不对,丢弃从新检测
			DataHeadbuf.intLow=0;
		}
		else
		{				
			OS_Status=Start_Check;		//是字头则改变状态到数据接收检测,并清高电平个数从新计数
			DataHeadbuf.intHigh=0;
			Push_Data(0x00);			//数据保存
		}
	}
	else if(HFHData==3)
	{									//检测错误,清0从新检测
		DataHeadbuf.intHigh=0;
		DataHeadbuf.intLow=0;
		mcsbuf.Head=mcsHeadNull;
	}
}


/*--------------------------------------------------------------------------------
函数:	DataRecvHandle(uchar DRHData)
功能:	处理数据接收(9个1之后的55位数据)
引用值:uchar DRHData
返回值:无
--------------------------------------------------------------------------------*/
void DataRecvHandle(uchar DRHData)
{
	if(DRHData==1)
	{
		DataHeadbuf.intHigh++;			//是高电平就自动加1,找出字头(9个1)
		Push_Data(0x01);					//数据保存		
	}
	else if(DRHData==0)
	{					
		DataHeadbuf.intLow++;			//是低电平判断高电平是否有9个连续的1
		Push_Data(0x00);					//数据保存
	}
	else if(DRHData==3)
	{									//检测错误,清0从新检测
		OS_Status=Start_Find;
		DataHeadbuf.intHigh=0;
		DataHeadbuf.intLow=0;
		mcsbuf.Head=mcsHeadNull;
	}
}

/*--------------------------------------------------------------------------------
函数:
功能:
引用值:
返回值:
--------------------------------------------------------------------------------*/

void read_em(void)
{
	uchar	temp;
	InitReg();
	TR0	= 1;
	EA = 1;
	High_Timer = 0;
	Low_Timer = 0;
    EMSHD=0;
	WDT_CONTR=WDT;//喂狗
//	while(1)
//	{
		IRbitPri = IRbitNext;
		IRbitPri = IRbitNext;
		IRbitNext = IRout;
		IRbitNext = IRout;

		WDT_CONTR=WDT;//喂狗
		if(IRbitPri!=IRbitNext)			//有跳变处理
		{	BEEP=0;//
			IRbitNext = IRout;
			IRbitNext = IRout;
			IRbitNext = IRout;
			IRbitNext = IRout;
			IRbitNext = IRout;
			IRbitNext = IRout;
			IRbitNext = IRout;
			IRbitNext = IRout;
			IRbitNext = IRout;

			if(IRbitPri!=IRbitNext)			//有跳变处理
			{
				WDT_CONTR=WDT;//喂狗

				Low_Timer=TH0;
				temp=TL0;
				High_Timer=TH0;				//高电平时间计算
				if(Low_Timer==High_Timer)
				{
					Low_Timer=temp+High_Timer*256; 				
				}
				else
				{
					Low_Timer=TL0+High_Timer*256;
				}
				High_Timer=Low_Timer;
				TL0=0x00;
				TH0=0x00;
			
				if((IRbitPri==0)&&(IRbitNext==1))				//如果前一位为低电平
				{
					temp=Manchster(mcsLowTimer);
				}
				else if((IRbitPri==1)&&(IRbitNext==0))			//如果前一位是高电平
				{
					temp=Manchster(mcsHighTimer);
				}

				if(OS_Status==Start_Find)
				{							//数据头检测
					HeadFindHandle(temp);
				}
				else if(OS_Status==Start_Check)
				{
					DataRecvHandle(temp);
				}
			}
			BEEP=1;
		//} 		
	} 
}

⌨️ 快捷键说明

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