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

📄 fm1715.c

📁 复旦FM1702源码,完整资料,已经编译通过.复旦FM1702的资料几乎就是RC500英文的翻译版
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <reg52.h>
#include <absacc.h>
#include <intrins.h>
#include "FM1715_Reg.h"

uchar idata readdata[6];//	_at_ 0x0040;	//Read/write memory
 uchar idata value[4];  // _at_ 0x0050;	//Inc/Dec value
 uchar idata KeySet;							//password type
 uchar idata tagtype[2];//_at_ 0x0096;	//card indentify character

 uchar idata PRO_SendBuf[16];	//_at_ 0x0080;	//Send buffer
uchar idata PRO_RecvBuf[16];	//_at_ 0x0080;	//Receive buffer
 uchar idata buffer[24];	//_at_ 0x0060;	//FM1715 command send/receive buffer
 uchar idata UID[5];	//	_at_ 0x007a;	//User ID
 uchar idata Secnr;	//	_at_ 0x0090;	//sector number
 uchar idata keybuffer[6];					//key save buffer

uchar idata keybuffer[6]={0xff,0xff,0xff,0xff,0xff,0xff};		//定义

//**********************************************************
//Name: Check_UID
//function: check card UID
//input: N/A
//output:	TRUE  UID right
//		FALSE UID error
//**********************************************************'
uchar Check_UID(void)
{
	uchar temp;
	uchar i;
	
	temp=0x00;
	for(i=0;i<5;i++)
	{
		temp=temp^UID[i];
	}
	if(temp==0)
	{
		return TRUE;
	}
	return FALSE;
}

//*******************************************************************
//name:Save_UID
//function: This function sae card's serial number
//input:  row : coflict row
//	  col : coflict line
//	length: the length of received UID data
//output:  N/A
//*******************************************************************
void Save_UID(uchar row,uchar col,uchar length)
{
	uchar i;
	uchar temp;
	uchar temp1;

	if((row == 0x00)&&(col==0x00))
	{
		for(i=0;i<length;i++)
		{
			UID[i]=buffer[i];
		}
	}
	else
	{
		temp=buffer[0];
		temp1=UID[row-1];
		switch(col)
		{
			case 0:
			temp1=0x00;
			row=row+1;
			break;
			case 1:
			temp=temp&0xFE;
			temp1=temp1&0x01;
			break;
			case 2:
			temp=temp&0xFC;
			temp1=temp1&0x03;
			break;
			case 3:
			temp=temp&0xF8;
			temp1=temp1&0x07;
			break;
			case 4:
			temp=temp&0xF0;
			temp1=temp1&0x0F;
			break;
			case 5:
			temp=temp&0xE0;
			temp1=temp1&0x1F;
			break;
			case 6:
			temp=temp&0xC0;
			temp1=temp1&0x3F;
			break;
			case 7:
			temp=temp&0x80;
			temp1=temp1&0x7F;
			break;
			default:
			break;
		}
		buffer[0]=temp;
		UID[row-1]=temp1|temp;
		for(i=1;i<length;i++)
		{
			UID[row-1+i]=buffer[i];
		}
	}
}

//*************************************************************
//name: Set_BitFraming
//function: set the byte that ready to send
//input: row : conflict row
//	 col : conflict col
//ouput: N/A
//*************************************************************
void Set_BitFraming(uchar row,uchar col)
{
	switch(row)
	{
		case 0:
		buffer[1]=0x20;
		break;
		case 1:
		buffer[1]=0x30;
		break;
		case 2:
		buffer[1]=0x40;
		break;
		case 3:
		buffer[1]=0x50;
		case 4:
		buffer[1]=0x60;
		break;
		default:
		break;
	}
	
	switch(col)
	{
		case 0:
		Bit_Frame=0x00;
		break;
		case 1:
		Bit_Frame=0x11;
		buffer[1]=(buffer[1]|0x01);
		break;
		case 2:
		Bit_Frame=0x22;
		buffer[1]=(buffer[1]|0x02);
		break;
		case 3:
		Bit_Frame=0x33;
		buffer[1]=(buffer[1]|0x03);
		break;
		case 4:
		Bit_Frame=0x44;
		buffer[1]=(buffer[1]|0x04);		
		break;
		case 5:
		Bit_Frame=0x55;
		buffer[1]=(buffer[1]|0x05);		
		break;
		case 6:
		Bit_Frame=0x66;
		buffer[1]=(buffer[1]|0x06);		
		break;
		case 7:
		Bit_Frame=0x77;
		buffer[1]=(buffer[1]|0x057);		
		break;
		default:
		break;
	}
}

//************************************************************
//name: FM1715_Bus_Sel
//function: this function select FM1715 control bus?
//input: N/A
//output: TRUE, select bus succeed
//	 FALSE, select bus fail
//************************************************************
uchar FM1715_Bus_Sel(void)
{
	uchar i;
	Page_Sel=0x80;			
	
	for(i=0;i<RF_TimeOut;i++)
	{
		if(Command==0x00)
		{
			Page_Sel=0x00;
			return TRUE;
		}
	}
	return FALSE;
}

//********************************************************
//name: Init_FM1715
//function: Init FM1715
//input: mode: 	0 -->TYPEA mode
//	 	1 -->TYPEB mode
//		2 -->shanghai mode
//ouput: N/A
//*********************************************************
void Init_FM1715(uchar mode)
{
//	uchar idata temp;
	uint i;
	
	MFRST=1;		//Reset FM1715
	for(i=0;i<0x1fff;i++)
	{
		_nop_();
	}

	MFRST=0;
	for(i=0;i<0x1fff;i++)
	{
		_nop_();
	}
	
	while(Command!=0)		//waiting command=0, FM1715 Reset succeed
	{
		_nop_();
	}

	FM1715_Bus_Sel();		//Select FM1715 bus

	TimerClock=0x0b;		//151us/per
	TimerControl=0x02;		//发送结束开定时器,发送开始关定时器
	TimerReload=0x42;		//10ms interval
	InterruptEn=0x7f;		//Close all interrupt
//	temp=InterruptEn;		//	?????????????????????????????????????????????????
	Int_Req=0x7f;			//?????????????????????????????????????????????????
//	MFOUTSelect=0x02;		//for debug
	TxControl=0x5b;			//start TX1,TX2
	if(mode==SHANGHAI_MODE)		//shanghai mode
	{
		TypeSH=0x01;
	}
	else
	{
		TypeSH=0x00;
	}

	if(mode==TYPEB_MODE)
	{
		CoderControl=0x20;
		TypeBFraming=0x05;
		DecoderControl=0x19;
		ChannelRedundancy=0x24;
		TxControl=0x4b;
		CWConductance=0x3f;
		ModConductance=0x3f;			//??????这里是否有问题
	}
}

//********************************************************************
//name:Clear_FIFO
//function: Clear the data in FIFO
//input:N/A
//output: TRUE, FIFO has been cleared
//	 FALSE, FIFO hasn't beend cleared
//********************************************************************
uchar Clear_FIFO(void)
{
	uchar temp;
	uint i;
	
	temp=Control;			//Clear FIFO
	temp=(temp|0x01);
	Control=temp;
	for(i=0;i<RF_TimeOut;i++)   	//检查FIFO是否为空
	{
		temp=FIFO_Length;
		if(temp==0)
		{
			return TRUE;
		}
	}
	return FALSE;
}

//******************************************************************
//name: Write_FIFO
//function: write n bytes to FM1715's FIFO
//input: count , the data's length
//       buff, point to the data that to be writen
//output: N/A
//******************************************************************
void Write_FIFO(uchar count,uchar idata *buff)
{
	uchar i;
	for(i=0;i<count;i++)
	{
		FIFO=*(buff+i);
	}
}

//*****************************************************************
//name: Read_FIFO
//function: read data from FM1715's FIFO
//input: buff, 指向读出数据的指针
//output: N/A
//*****************************************************************
uchar Read_FIFO(uchar idata *buff)
{
	uchar temp;
	uchar i;
	
	temp=FIFO_Length;
	if(temp==0)
	{
		return 0;
	}
	if(temp>=24)			//temp=255,会进入死循环,因此增加FIFO_length越界判断
	{
		temp=24;
	}
	for(i=0;i<temp;i++)
	{
		*(buff+i)=FIFO;
	}
	return temp;
}


//******************************************************************
//name: Command_Send
//function: send command to FM1715
//input: count, the length of command
//	 buff, point to the data that ready to send
//	 Comm_Set, command code
//output: TRUE ,  running command succeed
//	  FALSE,  running command error
//******************************************************************
uchar Command_Send(uchar count,uchar idata *buff,uchar Comm_Set)
{
	uint j;
	uchar idata temp,temp1;
	Command=0x00;
	Clear_FIFO();
	Write_FIFO(count,buff);
//	temp=MFOUTSelect;
	Command=Comm_Set;		//running command
	for(j=0;j<RF_TimeOut;j++)
	{
//		temp=MFOUTSelect;
		temp=Command;
		temp1=Int_Req&0x80;
		if((temp==0x00)||(temp1==0x80))
		{
			Green=!Green;
			return TRUE;
		}
	}
	return FALSE;
}


//*****************************************************************
//name: MIF_Halt
//function: halt the mifare card
//input: N/A
//output: FM1715_OK : ack succeed
//	 FM1715_PARITYERR: parity verify error
//	 FM1715_CRCERR:	CRC verify error
//	 FM1715_NOTAGERR: na card
//*****************************************************************
uchar MIF_Halt(void)
{
	uchar temp;
	CRCPresetLSB=0x63;
	CWConductance=0x3f;
	ChannelRedundancy=0x03;
	*buffer=RF_CMD_HALT;
	*(buffer+1)=0x00;
	temp=Command_Send(2,buffer,Transceive);
	if(temp == FALSE)
	{
		return FM1715_OK;
	}
	else
	{
		temp = ErrorFlag;
		if((temp&0x02)==0x02)
		{
			return(FM1715_PARITYERR);
		}
		if((temp&0x04)==0x04)
		{
			return(FM1715_FRAMINGERR);
		}
		return(FM1715_NOTAGERR);
	}
}

//********************************************************************
//name: Load_key_CPY
//function: save E2's password to FM1715's keybuffer
//input: keybuffer: save key to this buffer
//output: TRUE: load key succeed
//	 FLASE: load key fail
//********************************************************************
uchar Load_key_CPY(void)
{
	uchar temp,i;
	uchar ln=0;
	uchar hn=0;
	
	for(i=0;i<6;i++)		//change the key to RC531 format
	{
		ln=keybuffer[i]&0x0f;	//low four bit
		hn=keybuffer[i]>>4;	//high four bit
		buffer[i*2+1]=(~ln<<4)|ln;
		buffer[i*2]=(~hn<<4)|hn;
	}
	temp=Command_Send(12,buffer,LoadKey);
	temp=ErrorFlag&0x40;
	if(temp==0x40)
	{
		return FALSE;
	}
	return TRUE;
}


//***********************************************************************
//name: delay
//function: 
void delay(uchar i)
{
	int j;
	while(i)
	{
		for(j=0x00;j<0x1000;j++);
		i--;
	}
}

//***********************************************************************
//name: Request
//function: response the card's request in FM1715's control range
//input: mode: ALL(monitor card in FM1715 control range)
//       STD:(monitor the halt card in FM1715's control range)
//output: FM1715_NOTAGERR: no card
//  	  FM1715_OK: ack succeed 
//	  FM1715_REQERR: ack error
//************************************************************************
uchar Request(uchar mode)
{
	uchar temp;
	TxControl=0x58;
	delay(1);
	TxControl=0x5b;
	CRCPresetLSB=0x63;
	CWConductance=0x3f;
	buffer[0]=mode;		//select Request mode
	Bit_Frame=0x07;		//send 7 bit
	ChannelRedundancy=0x03;	//closed CRC
	TxControl=0x5b;
	Control=0x01;		//mask CRYPT01 bit

	temp=Command_Send(1,buffer,Transceive);

	if(temp==FALSE)
	{
		return FM1715_NOTAGERR;
	}

	Read_FIFO(buffer);	//read ack information from FIFO
	tagtype[0]=buffer[0];
	tagtype[1]=buffer[1];
	
	return FM1715_OK;
}

//**************************************************************************
//name: AntiColl(void)
//function: anticollision for the card in FM1715's control range
//input: N/A
//output: FM1715_NOTAGERR: no card
//	  FM1715_BYTECOUNTERR: receive byte error
//	  FM1715_SERNRERR: card's serial ack error
//	  FM1715_OK: card ack succeed
//**************************************************************************
uchar AntiColl(void)
{
	uchar temp;
	uchar i;
	uchar row,col;
	uchar pre_row;
	
	row=0;
	col=0;
	pre_row=0;

	CRCPresetLSB=0x63;
	CWConductance=0x3f;
	ModConductance=0x3f;
//	ChannelRedundancy=0x03;				//closed CRC,start parity

	buffer[0]=RF_CMD_ANTICOL;
	buffer[1]=0x20;
	ChannelRedundancy=0x03;				//closed CRC,start parity
	temp=Command_Send(2,buffer,Transceive);

	while(1)
	{
		if(temp==FALSE)
		{
			return(FM1715_NOTAGERR);
		}
		temp = ErrorFlag;
		if((temp & 0x02)==0x02)
			return(FM1715_PARITYERR);
		if((temp & 0x04)==0x04)
			return(FM1715_FRAMINGERR);
		temp=FIFO_Length;
		if (temp==0)
		{
			return FM1715_BYTECOUNTERR;
		}

⌨️ 快捷键说明

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