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

📄 weigand_26.c

📁 很强的射频卡reader源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*                                                                           
* File:         CV3500AT WIEGAND INTERFACE PROGRAM RESOURCE CODE Reader.c                                  
*                                                                           
* Created:      14/JAN/03                                                    
*                                                                           
* Editor        PANG PENG
*                                                                           
* Compiler:     KEIL C51 V7.02a                                             
*                                                                           
* Description:  T89C51RD2-Firmware for MIFARE WIEGAND Reader               
*                                                                           
*****************************************************************************
*                                                                           
* Version | Date     |  PCB  | Description                                          
* --------+----------+-------+------------------------------------------------ 
* V00.00  | 14/01/03 | 00.01 | First version  

****************************************************************************/

#include "reader.h"
#include "Rc632cv.h"
#include "Rc632Reg.h"
#include "632MfErrNo.h"
#include "wiegand.h"
#include "Rc632CMD.h"

#define	FALSE	1
#define	TRUE	0

extern unsigned char getkey();
extern unsigned char get_key();

extern bit RedLED_Flag;
extern bit GreenLED_Flag;
extern bit Display_Flag;
extern unsigned char display1[]; 
extern unsigned char select_key[17];

static code unsigned char wgdisplay4[]="ID:";
static code unsigned char cls[]="                  ";


extern unsigned char weigandFlag;
extern idata unsigned char wiegand_mode; 	   	//wiegand 格式选择
//extern idata unsigned char wiegand_safety;  	//安全模式选择
extern idata unsigned char wiegand_block;  		//wiegand 数据存放块地址
extern idata unsigned char iso15693block;
extern idata unsigned char wiegand_requst;  	//Request 模式选择
extern idata unsigned char wiegand_key_d;	  	//wiegand 数据存放扇区密码编号
extern idata unsigned char wiegand_key_g;   	//wiegand 管理卡密码编号
//extern idata unsigned char wiegand_io_m; 		//I/O 报警模式选择
extern idata unsigned char key_outmode;			//键盘输出模式
extern idata unsigned char csn_outmode;			//输出控制模式
extern data unsigned char SecondCNT_t1;
extern bit BuzFlag;
extern idata unsigned char Buz_T1,Buz_T2,Buz_T3,Buz_T4,Buz_Period,Buz_Step,Buz_CNT;
void weigand(void);
extern unsigned char weigandFlag;
extern unsigned char DlyforWiegand;

extern unsigned char des_app_no[3];		//应用号
extern unsigned char des_file_no;			//文件号
//extern unsigned char des_file_type;
//extern unsigned char des_encrypt_mode;
extern unsigned char des_offset[3];
extern unsigned char des_read_len;
extern unsigned char SEQ;
extern void return_nodata_ack(status);
extern unsigned char read(unsigned char app_no[3],
		                  unsigned char file_no,
		                  unsigned char file_offset[3],
		                  unsigned char read_length);

unsigned char data out_format;


unsigned char HL_FindCard(unsigned char *Buffer, unsigned char *len)
{
	unsigned char CMD_status,i;
	unsigned char mode;
	unsigned char x;
	unsigned char temp[16];


	if( (MCTYPE==CV5600S)||(wiegand_block==00)  )										//只读序列号
	{
		while (1)
		{
			if (wiegand_requst==R_idle)
			{
				 mode=R_idle;
			} 
			else 
			{
				mode=R_all;
			}	
		
			CMD_status=Mf500PiccCommonRequest(mode,Buffer);
			if(CMD_status !=0x00)
				CMD_status=Mf500PiccCommonRequest(mode,Buffer);
			if(CMD_status != 0x00)	break;
	
			CMD_status=Mf500PiccCascAnticoll(PICC_ANTICOLL1,Buffer);
			if(CMD_status != 0x00)	break;
			CMD_status=Mf500PiccCascSelect(PICC_ANTICOLL1,Buffer);
			mode = 0;
			if (CMD_status ==0)	break;
			if (CMD_status != 0x46)	break;
	
			CMD_status=Mf500PiccCascAnticoll(PICC_ANTICOLL2,Buffer+4);
			if(CMD_status != 0x00)	break;
			CMD_status=Mf500PiccCascSelect(PICC_ANTICOLL2,Buffer+4);
			mode = 1;
			if (CMD_status ==0)	break;
			if (CMD_status != 0x46)	break;

			CMD_status=Mf500PiccCascAnticoll(PICC_ANTICOLL3,Buffer+8);
			if(CMD_status != 0x00)	break;
			CMD_status=Mf500PiccCascSelect(PICC_ANTICOLL3,Buffer+8);
			mode = 2;
			break;
		}	
	
		if (CMD_status==0)
		{
			if (mode == 0)
			{
				*len = 4;
			}
			else if (mode == 1)
			{
				for (i = 0; i<7; i++)
				{
					*(Buffer+i)	= *(Buffer+1+i);
				}
				*len = 7;
			}
			else
			{
				for (i = 0; i<7; i++)
				{
					*(Buffer+4+i)	= *(Buffer+5+i);
				}

				for (i = 0; i<10; i++)
				{
					*(Buffer+i)	= *(Buffer+1+i);
				}
				*len = 10;
			}
			return 1;
		}
	}
	else                          	//14443A 读扇区
	{	

//				for(i=0;i<17;i++)
//				{
//					select_key[i]=0;
//				}
//				CMD_status=read(1,1,0,1);   //application_no				
//				EA=0;
//				SBUF=0x55;
//				while(TI);
//				TI=0;
//				EA=1;

			    
				CMD_status=read(des_app_no,des_file_no,des_offset,des_read_len);   //application_no
				if (!CMD_status)			
				{
			    	if (SEQ==0xf0)
					   SEQ=0x80;
					else
					   SEQ=SEQ+0x10;

					for(i=0;i<4;i++)
					{
						Buffer[i]=card_buffer[i];
					//Buffer++;
					}

					*len=4;
					return 4;
				
				}
		//		return 0 ;
			 
			
				 				
				if (wiegand_requst==R_idle)
					x=0; 
				else 
					x=0x01;
				CMD_status=MF_HL_Auth(x,&Buffer[0],wiegand_block);
				if (CMD_status==0)
				{	
					CMD_status=Mf500PiccRead(wiegand_block,&Buffer[0]);
					if (CMD_status==0)
					{
						*len=4;
						return 4;
					}
				}

			

		
	}
	key_bcdweigand();


#if (MCTYPE==CV5100)
	// IS014443B
	Buffer[0] = 0x05;
	Buffer[1] = 0;
	Buffer[2] = 0;
	CMD_status = Mf531Generic_B(3,Buffer);	
	if ((CMD_status ==0)&&(Buffer[0]>10))
	{
		for (i=0; i<8; i++)
		{
			Buffer[i] = Buffer[i+1];
		}
		*len = 8;

		return 2;
	}

				key_bcdweigand();


		// ISO15693
	if(iso15693block==0XFF)              //读序列号
	{	CMD_status=ISO15693_Get_System_Information(0x02,&temp[0],&temp[0]);
		if (CMD_status == 0)
		{
			for (i=0; i<8; i++)
			{
				Buffer[i] = temp[i+3];
			}
			*len = 8;

			return 3;
		}
	 }
	else
	{	                        //读块
		CMD_status=ISO15693_Read_sm(0,&temp[0],iso15693block,1,&temp[0]); 
		
		if(CMD_status==0)
		{
			for (i=0; i<4; i++)
			{
				Buffer[i] = temp[i+2];
			}
			*len =	4;

			return 5;
		 }
	}


				
				key_bcdweigand();


#elif(MCTYPE==CV5500)
	if (WeiGendEM_flag==TRUE) 
	{				
		WeiGendEM_flag=FALSE;	
		if(pro_weigen(Buffer)==TRUE)
			*len =	5;
		return 6;

	}
	key_bcdweigand();

#endif



	return 	(0);		
}


void key_bcdweigand()							
{
	unsigned char data state,reverse;
	unsigned char bit_mask;
	unsigned char j,sl;
	unsigned char key[]="key:";
	unsigned char stateA[1];
	state=get_key();
	if	(state==0xff)	
	{
		return;
	}	

	if(state<=9)
			stateA[0]=state+48;
	else if(state==10)
			stateA[0]='*';
	else if(state==11)
			stateA[0]='#';

	Display_Flag=1;	
	SecondCNT_t1=0;
	LCD_PWR=ON;
	DisplayLcd(cls,16,0x00);	
	DisplayLcd(display1,16,0x00);	
	DisplayLcd(cls,16,0x40);
	DisplayLcd(key,4,0x40);	
	DisplayLcd(stateA,1,0x48);

	
	if(csn_outmode&0x02)		//是否允许输出到串口
	{
			out_format=csn_outmode&0x18;
			if(out_format==0x00)			//HEX+protocol
			{
				TxOut(1,0,&state);
			}
			else if(out_format==0x08)		//HEX
			{
				SendNumber(1,&state);
			}
			else if(out_format==0x10)		//ASCII
			{
				SendNumber(1,&stateA);
			}	

			delay_1ms(10);
	}	


	reverse=state^0xFF;
	reverse=reverse<<4;
	state=reverse|state;

	if(key_outmode==0)
	{
		sl=8;
		bit_mask =0x80;
	}
	else if(key_outmode==1)
	{
		sl=4;
		bit_mask =0x08;
	}

	for (j =0; j < sl; j++)
	{	
		if (state & bit_mask ) Wei_D1 =LOW;	//inverter out
		else Wei_D0 = LOW;			
		delay_50us(3);
		Wei_D0 = HIGH;	
		Wei_D1 = HIGH;						//inverter out
		delay_50us(41);
		bit_mask = bit_mask >>1;
	}

	if (csn_outmode&0x04)
	{
		LED_R =OFF;
		LED_G  = ON;	//ON 
		Buzzer = ON;
		delay_1ms(100);	
		LED_R =ON;
		LED_G  = OFF;
		Buzzer = OFF;
	}
}
/*
//显示卡号,以十进制格式输出
void Display_SNR (unsigned char *SNR)
{
	 unsigned char DISPLAY_snr[15]=0;
	unsigned char i,bit_mask;
	 unsigned long int dsnr;
	if 	(wiegand_block!=00)
	{
		switch(wiegand_mode)
		{
				case 	s_weigand_26 :	//输出序列号高3个字节	
						SNR[4]=SNR[2];
						SNR[3]=SNR[1];
						SNR[2]=SNR[0];
						SNR[1]=0x00;
						SNR[0]=0x00;
						break;
				case 	s_weigand_32 :	
						SNR[4]=SNR[3];
						SNR[3]=SNR[2];
						SNR[2]=SNR[1];
						SNR[1]=SNR[0]&0x3f;
						SNR[0]=0x00;
 						break;
 				case 	s_weigand_32e :	
						SNR[4]=SNR[3];
						SNR[3]=SNR[2];
						SNR[2]=SNR[1];
						SNR[1]=SNR[0];
						SNR[0]=0x00;
 						break;		
				case	s_weigand_40:
						SNR[4]=SNR[3];
						SNR[3]=SNR[2];
						SNR[2]=SNR[1];
						SNR[1]=SNR[0];
						SNR[0]=0x00;
						break;
				case 	s_weigand_34:
						SNR[4]=SNR[3];
						SNR[3]=SNR[2];
						SNR[2]=SNR[1];
						SNR[1]=SNR[0];
						SNR[0]=0x00;
						break;
				default:
						break;				
		}
	}
	else
	{
		switch(wiegand_mode)
		{
				case 	s_weigand_26 :	//输出序列号高3个字节	
						SNR[4]=SNR[3];
						SNR[3]=SNR[2];
						SNR[2]=SNR[1];
						SNR[1]=0x00;
						SNR[0]=0x00;
						break;
				case 	s_weigand_32 :	//输出序列号高3个字节	
						SNR[1]=SNR[1]&0x3f;
 						break;
 						
				default:
						break;				
		}
	}
	
	for (i=0;i<16;i++)
	{
		DISPLAY_snr[i]=0;
	}	
	
	dsnr=SNR[4]+SNR[3]*0x100+SNR[2]*0x10000+SNR[1]*0x1000000;
	
	for(i=0;i<11;i++)
	{
			DISPLAY_snr[12-i]=dsnr%10;//+0x30;
			dsnr=dsnr/10;
			if (dsnr<10) 
			{
				DISPLAY_snr[11-i]=dsnr;//+0x30;
				break;
			}
	}
	
	if (SNR[0])
	{
		bit_mask=0x80;
		for (i=0;i<8;i++)
		{ 
			if (SNR[0] & bit_mask)
				{
					switch(i)
						{
							case 0:
								//+549755813888
								DISPLAY_snr[12]+=8;
								DISPLAY_snr[11]+=8;
								DISPLAY_snr[10]+=8;
								DISPLAY_snr[9]+=3;
								DISPLAY_snr[8]+=1;
								DISPLAY_snr[7]+=8;
								DISPLAY_snr[6]+=5;
								DISPLAY_snr[5]+=5;
								DISPLAY_snr[4]+=7;
								DISPLAY_snr[3]+=9;
								DISPLAY_snr[2]+=4;
								DISPLAY_snr[1]+=5;
								break;
							case 1:
								//274877906944
								DISPLAY_snr[12]+=4;
								DISPLAY_snr[11]+=4;
								DISPLAY_snr[10]+=9;
								DISPLAY_snr[9]+=6;
								DISPLAY_snr[8]+=0;
								DISPLAY_snr[7]+=9;
								DISPLAY_snr[6]+=7;
								DISPLAY_snr[5]+=7;
								DISPLAY_snr[4]+=8;
								DISPLAY_snr[3]+=4;
								DISPLAY_snr[2]+=7;
								DISPLAY_snr[1]+=2;
								break;
							case 2:
								//137438953472
								DISPLAY_snr[12]+=2;
								DISPLAY_snr[11]+=7;
								DISPLAY_snr[10]+=4;
								DISPLAY_snr[9]+=3;
								DISPLAY_snr[8]+=5;
								DISPLAY_snr[7]+=9;
								DISPLAY_snr[6]+=8;
								DISPLAY_snr[5]+=3;
								DISPLAY_snr[4]+=4;
								DISPLAY_snr[3]+=7;
								DISPLAY_snr[2]+=3;
								DISPLAY_snr[1]+=1;
								break;
							case 3:
							//68719476736
								DISPLAY_snr[12]+=6;
								DISPLAY_snr[11]+=3;
								DISPLAY_snr[10]+=7;
								DISPLAY_snr[9]+=6;
								DISPLAY_snr[8]+=7;
								DISPLAY_snr[7]+=4;
								DISPLAY_snr[6]+=9;
								DISPLAY_snr[5]+=1;
								DISPLAY_snr[4]+=7;
								DISPLAY_snr[3]+=8;
								DISPLAY_snr[2]+=6;
//								DISPLAY_snr[11]+=5;
								break;
							case 4:
								//34359738368
								DISPLAY_snr[12]+=8;
								DISPLAY_snr[11]+=6;
								DISPLAY_snr[10]+=3;
								DISPLAY_snr[9]+=8;
								DISPLAY_snr[8]+=3;
								DISPLAY_snr[7]+=7;
								DISPLAY_snr[6]+=9;
								DISPLAY_snr[5]+=5;
								DISPLAY_snr[4]+=3;
								DISPLAY_snr[3]+=4;
								DISPLAY_snr[2]+=3;
								break;
							case 5:
								//17179869184
								DISPLAY_snr[12]+=4;
								DISPLAY_snr[11]+=8;
								DISPLAY_snr[10]+=1;
								DISPLAY_snr[9]+=9;
								DISPLAY_snr[8]+=6;
								DISPLAY_snr[7]+=8;
								DISPLAY_snr[6]+=9;
								DISPLAY_snr[5]+=7;
								DISPLAY_snr[4]+=1;
								DISPLAY_snr[3]+=7;
								DISPLAY_snr[2]+=1;
								break;
							case 6:
								//8589934592
								DISPLAY_snr[12]+=2;
								DISPLAY_snr[11]+=9;
								DISPLAY_snr[10]+=5;
								DISPLAY_snr[9]+=4;
								DISPLAY_snr[8]+=3;
								DISPLAY_snr[7]+=9;
								DISPLAY_snr[6]+=9;
								DISPLAY_snr[5]+=8;
								DISPLAY_snr[4]+=5;
								DISPLAY_snr[3]+=8;
								break;
							case 7:
								//4294967296
								DISPLAY_snr[12]+=6;
								DISPLAY_snr[11]+=9;
								DISPLAY_snr[10]+=2;
								DISPLAY_snr[9]+=7;
								DISPLAY_snr[8]+=6;
								DISPLAY_snr[7]+=9;
								DISPLAY_snr[6]+=4;
								DISPLAY_snr[5]+=9;
								DISPLAY_snr[4]+=2;
								DISPLAY_snr[3]+=4;
								break;	
							default:
								break;
						}			
				}
			bit_mask=bit_mask>>1;
		}
	}

	for(i=13;i>1;i--)
	{
		DISPLAY_snr[i-2]+=DISPLAY_snr[i-1]/10;
		DISPLAY_snr[i-1]=DISPLAY_snr[i-1]%10+0x30;
	}
	DISPLAY_snr[0]+=0x30;

	SecondCNT_t1=0;//显示卡号,以十进制格式输出
	LCD_PWR=ON;

}

*/





void weigand()
{	unsigned char CMD_status,n;
	idata unsigned char snr[16],SNRS[16],OutNumber[20];
	unsigned char len[1];		
	
	unsigned char data i,csnlen,out_format;
	
//	WDTD=0x87;
//	WDTC=0x1F;	//Reset watchdog timer

	if (weigandFlag & 0x01)
	{
		LED_G = OutputPort0;
		LED_R =~LED_G;
		Buzzer =OutputPort1;
	}

	if (DlyforWiegand)	return;
	for(n=0;n<16;n++)
		{snr[n]=0;}

	
	CMD_status=HL_FindCard(snr, len);
	
	if (CMD_status)
 	 {	
	    //if (desfire_mode && wiegand_block)
	    //{
	    //	if (weigandFlag & 0x02)
		//	{
		//		LED_R =OFF;
		//		LED_G  = ON;	//ON 
		//		Buzzer = ON;
		//		delay_1ms(200);		
		//		Buzzer = OFF;
		//		LED_G  = OFF;
		//		LED_R =ON;
		//	}
		//	return;		
	    //}
	 	DlyforWiegand = 2;	
		#if (MCTYPE==CV5100)
			DlyforWiegand = 25;				// 500ms
		#else
			Mf500PiccHalt();
		#endif
		csnlen=len[0];	
		if(csn_outmode&0x01)			//序列号输出方向,=1反相
		{
			for(i=0;i<csnlen;i++)
			{	SNRS[i]=snr[csnlen-1-i];	}
	
			for(i=0;i<16;i++)
			 {
				snr[i]=SNRS[i];	
				if(i>=csnlen)
				{
					snr[i]=0;
				}
			 }
	   
		}

		for(i=0;i<len[0];i++)
		{
			OutNumber[2*i]=snr[i]>>4;
			OutNumber[2*i+1]=snr[i]&0x0F;
		}				
		for(i=0;i<(2*len[0]);i++)
		{
			if(OutNumber[i]<=9)
				OutNumber[i]=OutNumber[i]+48;
			else
				OutNumber[i]=OutNumber[i]+55;
		}
		Display_Flag=1;	
		SecondCNT_t1=0;
		LCD_PWR=ON;
		DisplayLcd(cls,16,0x00);	
		DisplayLcd(display1,16,0x00);	
		DisplayLcd(cls,16,0x40);
		DisplayLcd(&wgdisplay4[0],3,0x40);	
		DisplayLcd(OutNumber,2*len[0],0x44);	



		if(csn_outmode&0x02)	//是否允许输出到串口
		{	
			out_format=csn_outmode&0x18;
			if(out_format==0x00)			//HEX+protocol
			{
				TxOut(len[0],0,&snr[0]);
			}
			else if(out_format==0x08)		//HEX
			{
				SendNumber(len[0],&snr[0]);
			}
			else if(out_format==0x10)		//ASCII
			{
				SendNumber(2*len[0],OutNumber);
			}	
			delay_1ms(10);

⌨️ 快捷键说明

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