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

📄 main.c

📁 RFID 使用C8051F020和周立功RC500开发模块
💻 C
字号:
#define __SRC
#include "main.h"
#undef  __SRC

#include "zlg500B.h"

#define MF1_S50		0			//卡的类型
#define MF1_S70		1
#define MF0_ULIGHT	2
#define MF1_LIGHT	3


sbit zlg500B_RST=P1^3;		//change
sbit LED=P1^4;
/*
sbit TI1   = SCON1 ^ 1;
sbit RI1   = SCON1 ^ 0;
sbit REN1  = SCON1 ^ 4;
*/
uchar baud_num;				//波特率选择序号
uchar keyvalue;				//从串口和键盘收到的数据
uchar card_snr[8];			//卡的序列号		//进行两轮验证,
uchar databuf[16];			//从卡中一个段的数据
uchar adr1,adr2;
uchar *value;
uchar num_key;
uchar keyvalue;

uchar code Nkey_a[6]={0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};		//密码
uchar code Nkey_b[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
uchar code Nkey_c[6]={0xee, 0xee, 0xee, 0xee, 0xee, 0xee};		//testkey

uchar code KEY_CHECK[24]=
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,
 0xBB,0xCC,0xDD,0xEE,
};

uchar code KEY_MAP[24]=
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,		//10
 'r','w','a','m', 										//4
'?','?','?','?','?','?','?','?','?','?',                 //10
};

main()
{	
	

	uchar cardtype=MF1_S50;		//卡的类型:0
	uchar status;				//状态值列表		
	uchar i,j=40;
	bit  dosnr=1; 
	long value=1;

	//以下对测试变量的定义
//	 uchar databufSEQ[16];		//RC500的序列号
//	 uchar databuf14[16];	
//	 uchar databuf15[16];

//
    baud_num=2;	
	F020Config();				//仅仅完成引脚分配
	serial_init();		
	Timer_4_init();

	for(i=255;i>0;i--)
		for(j=10;j>0;j--);
	zlg500B_RST=0;					//对整个系统进行复位
	for(i=255;i>0;i--)
		for(j=20;j>0;j--);	

    
	//以下开始对RWD开始进行操作
	i=mifs_config();		//RWD
	i=mifs_get_info(databuf);		//读取RC500序列号
	Uart_1_send(10);
							
 //   i=mifs_load_key(KEYA,15,Nkey_a);	//只是写入到RC500的密码
  //  i=mifs_load_key(KEYA,5,Nkey_b);		//_SecNr所访问卡对应的扇区号
    
	
	//test();
   // i=mifs_read_E2(0x10,16,databuf);	//不知道为什么,程序到这里就死了,是不延时不对
//	i=mifs_read_E2(0x20,16,databuf);		//暂时不读,读也没有用



	while(1)		//利用查询操作,也可以用其它
	{	
		while(mifs_request(IDLE,databuf)!=MI_OK);		//一直在查有没有卡
	    //test();
		
		if(mifs_cascanticoll(ANTICOLL1,0,card_snr)!=MI_OK)	continue;			//返回卡的序列号了.	
	
		
		if(mifs_cascselect(ANTICOLL1,card_snr,&i)!=MI_OK)	continue;
		if(i&0x04)			//
		{
			if(mifs_cascanticoll(ANTICOLL2,0,card_snr+4)!=MI_OK)	continue;	//第二层防冲突,返回卡的序列号在高字节
			if(mifs_cascselect(ANTICOLL2,card_snr+4,&i)!=MI_OK)	continue;			//进行第二层选择
		}

		//完成卡的选择,
		if(dosnr)	
		{
			for(i=0;i<4;i++)
			{
				databuf[i]=card_snr[1];
			}
			Uart_1_send(4);
			dosnr=0;			//以后就不重新发了
		}
		
		if(cardtype == MF1_S50)				//如果是这种卡的话受理,否则不进行受理
		{   
		    status=mifs_authentication(KEYA,15);
			if(status!=0)			continue;		//进行验证
			
		}
			
		
     	
	}
}

	

	


/****************************************************************************
*                                                                           *
* Function:     serial_init                                                        *
*                                                                           *
* Input:        -                                                           *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
*                                                                           *
****************************************************************************/

void 	serial_init (void)
{
	PCON = 0x00;              	// SMOD = 0;
	SCON0 = 0x50;              	// Mode 1, 8-bit UART, enable receiption
	CKCON = 0x00;   // Clock Control Registe  	//定时器使用系统时钟12分颁
    TMOD&=0x0f;					//定时器1进行全清零,不管定时器0
	TMOD|=0x20;	      		// Timer 1, mode 2, 8-bit auto reload,


    TH1=0xFA;			//波特率的重装值


    IE        = 0x91;			//开了所有的中断了
    EIE2      = 0x40;
/*	switch(baud_num)				//由于实际对波特率进行设置		
	{	
		case 0:	TH1=BAUD_2400;	break;	
		case 1:	TH1=BAUD_4800;	break;
		case 2:	TH1=BAUD_9600;	break;
		case 3:	TH1=BAUD_19200;	break;
		case 4:	TH1=BAUD_28800;	break;
		case 5:	TH1=BAUD_57600;	break;	
	}
*/	
	CON_485=0;						//平时为低,
	ES0 = 1;							//开串口中断					
	ET1 = 0;						//禁定时器1中断
	TR1 = 1;						//开外部中断
}

#pragma noaregs

/****************************************************************************
*                                                                           *
* Function:     delay_50us                                                  *
*                                                                           *
* Input:        _50us                                                       *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
* Time delay with a resolution of 50 us.                                    *
*                                                                           *
****************************************************************************/

void 	delay_50us (uchar _50us)			//延时50US
{

  RCAP2LH = RCAP2_50us;
  T2LH    = RCAP2_50us;
  ET2 = 0; 	// Disable timer2 interrupt
  T2CON = 0x04;	// 16-bit auto-reload, clear TF2, start timer
  				//开定时器2
				//89C51中并没有定时器2  只有在89C52中才有
  while (_50us--)
  {
	while (!TF2);		//溢出标志:等待溢出标志
	TF2 = FALSE;			//溢出标志清零
  }

  TR2 = FALSE;			//关定时器2

}


/****************************************************************************
*                                                                           *
* Function:     delay_1ms                                                   *
*                                                                           *
* Input:        _1ms                                                        *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
* Time delay with a resolution of 1 ms.                                     *
*                                                                           *
****************************************************************************/

void 	delay_1ms (uint _1ms)
{

  RCAP2LH = RCAP2_1ms;
  T2LH    = RCAP2_1ms;
  ET2 = 0; 	// Disable timer2 interrupt
  T2CON = 0x04;	// 16-bit auto-reload, clear TF2, start timer
  
  while (_1ms--)
  {
	while (!TF2);
	TF2 = FALSE;
  }
  TR2 = FALSE;

}


/****************************************************************************
*                                                                           *
* Function:     delay_10ms                                                  *
*                                                                           *
* Input:        _10ms                                                       *
* Output:       -                                                           *
*                                                                           *
* Description:                                                              *
*                                                                           *
* Time delay with a resolution of 10 ms.                                    *
*                                                                           *
****************************************************************************/

void 	delay_10ms (uint _10ms)
{

  RCAP2LH = RCAP2_10ms;
  T2LH    = RCAP2_10ms;
  ET2 = 0; 	// Disable timer2 interrupt
  T2CON = 0x04;	// 16-bit auto-reload, clear TF2, start timer
  
  while (_10ms--)
  {
	while (!TF2);			//查询方式
	TF2 = FALSE;
  }
  TR2 = FALSE;			//完成指定延时任务,收工

}

void F020Config(void)
{
	int i;                              // delay counter
	WDTCN = 0x07;	// Watchdog Timer Control Register
    WDTCN = 0xDE;   // Disable WDT
    WDTCN = 0xAD;	
   
	OSCXCN = 0x67;                      // start external oscillator with
                                       // 22.1184MHz crystal

   for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)

   while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle

   OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                       // source and enable missing clock
                                       // detector
	XBR0      = 0x07;
    XBR1      = 0x04;
    XBR2      = 0x44;
	P0MDOUT = 0x05; // Output configuration for P0 
    P1MDOUT = 0x00; // Output configuration for P1 
    P2MDOUT = 0x00; // Output configuration for P2 
    P3MDOUT = 0x00; // Output configuration for P3 
    P74OUT = 0x00;  // Output configuration for P4-7

    P1MDIN = 0xFF;  // Input configuration for P1

	// P0.0  -  TX0 (UART0), Open-Drain, Digital
    // P0.1  -  RX0 (UART0), Open-Drain, Digital
    // P0.2  -  SCK  (SPI0), Open-Drain, Digital
    // P0.3  -  MISO (SPI0), Open-Drain, Digital
    // P0.4  -  MOSI (SPI0), Open-Drain, Digital
    // P0.5  -  NSS  (SPI0), Open-Drain, Digital
    // P0.6  -  SDA (SMBus), Open-Drain, Digital
    // P0.7  -  SCL (SMBus), Open-Drain, Digital

    // P1.0  -  TX1 (UART1), Open-Drain, Digital
    // P1.1  -  RX1 (UART1), Open-Drain, Digital
    // P1.2  -  INT0 (Tmr0), Open-Drain, Digital

}


void test(void)
{
	uchar cnt;
	uint iq0;					//延时系数
	LED=0;
	for(cnt=2;cnt>0;cnt--)
	{
		for(iq0=0xFFFF;iq0>0x00;iq0--);	
	}
	LED=1;
}
void Timer_4_init(void)		//系统时钟的12分频
{							//做UART1的波特率发生器
	T4CON     = 0x34;
    RCAP4L    = 0xFA;			//波特率9600
    RCAP4H    = 0xFF;
	SCON1=0x50;				//串口二的工作方式
}

void Uart_1_send(uchar cnt)
{	

	uchar q0;
	EIE2&=0xBF;	        	//禁止串口
	SCON1&=0xEF;			//REN1=0;
	for(q0=0;q0<cnt;q0++)
	{
	   SBUF1=databuf[q0];
	   while((SCON1&0x02)!=0x02);
//	   SCON1&=0xFD;			//TI1=0;
		SCON1=0x40;			//为了清除标志位
	 }

}

void Uart1_int(void) interrupt 20			//串口中断程序
{
	SCON1&=0xEF;			//REN1=0;	REN1=0;		//不要接收到重复的数据,等数据处理完了再处理	
    SCON1&=0xFE;	    	//RI1=0;			//清空接收中断标志 
	keyvalue=SBUF1;
    
	keyv_pro();		    //键值处
	SCON1|=0x10;		//REN1=1;
}

void keyv_pro(void)
{
	uchar i,key;
	for(i=0;i<23;i++)
	{
		if(KEY_CHECK[i]==keyvalue)
		break;
	};
	if(i==24)
		return;
	key=KEY_MAP[i];
		switch(key)
		{
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9:
			case 0:     KEY_number(key);
					 break;
			case 'r':	To_mifs_read();
						break;
			case 'w':   To_mifs_write();
						break;
			case 'a':   To_mifs_add();
						break;
			case 'm':	To_mifs_minus();
						break;
			default:	break;
		  }
}

key_number(uchar key)
{
    num_key=key;
	To_mifs_read();
	Uart_1_send(4);
}

void To_mifs_read()
{
     mifs_read(num_key, databuf);	
}

void To_mifs_write()
{
	mifs_write(num_key, databuf);
}

void To_mifs_add()
{	
	mifs_increment(adr1,*value);
	//mifs_value(PICC_INCREMENT,adrfrom,&value,adrto);
}

void To_mifs_minus(void)
{
		mifs_decrement(adr1,*value);
		
}
/*		以后做一定的扩展
void To_mifs_value(void)
{
		mifs_value(_mode,adrfrom,&value,adrto);
}
*/


#pragma aregs



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

⌨️ 快捷键说明

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