基于51单片机门禁系统

基于51单片机、舵机和射频模块实现的刷卡开门系统

舵机模块
1.利用定时中断实现PWN波控制舵机角度控制
舵机的控制一般需要一个20ms左右的时基脉冲,该脉冲的高电平部分一般为0.5ms-2.5ms范围内的角度控制脉冲部分,总间隔为2ms。以180角度伺服为例。
0.5ms-------------------------0度
1.0ms-------------------------45度
1.5ms-------------------------90度
2.0ms-------------------------135度
2.5ms-------------------------180度
基于51单片机门禁系统_第1张图片
2.例如用按键控制舵机旋转至特定角度。
源码:

#include "reg52.h"

#include "math.h"

typedef unsigned char uchar;
typedef unsigned int uint;

sbit KEY1 = P3^4;    		//按键1
sbit KEY2 = P3^5;			//按键2

sbit PWN_OUT = P2^7;		//PWN输出口

uint PWN_Value;				//定义pwn值
uchar order = 0;

void Delay(uint s);			//延时函数声明

uchar flag;					//舵机按键标志




/*延时函数*/
void Delay(uint s)
{
	uint i;
	for(i = 0; i < s; i++);
	for(i = 0; i < s; i++);
}

/*定时器初始化*/
void Init_Timer0()
{
	TMOD = 0x11;
	TH0 = (65536 - 1500) / 256;
	TL0 = (65536 - 1500) % 256;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
	PT0 = 1;
}

/*主函数*/
void main(void)
{
	Delay(6000);
	PWN_Value = 1500;

	Init_Timer0();

	while(1) {
		if( (KEY1 == 0) | (KEY2 == 0)) { //按键1或按键2被按下
			if(KEY1 == 0) {
				flag = 1;
			}
			if(KEY2 == 0) {
				flag = 2;
			}
		} else {
			flag = 0;
		}
		Delay(20);				//延时20ms
	}
}

/*中断程序*/
void timer0(void) interrupt 1
{
	if(flag == 1) {
		PWN_Value = 2100;
	}
	if(flag == 2) {
		PWN_Value = 500;
	}

	switch(order)
	{
		case 1: PWN_OUT = 1;
				TH0 = (65536 - PWN_Value) >> 8;
				TL0 = (uchar)(65536 - PWN_Value);
				break;

		case 2: PWN_OUT = 0;
				TH0 = (65536 - (5000 - PWN_Value)) >> 8;
				TL0 = (uchar)(65536 - (5000 - PWN_Value));
				break;
		case 3:
				TH0 = 60536 >> 8;
				TL0 = (uchar)60536;
				break;
		case 4: 
				TH0 = 60536 >> 8;
				TL0 = (uchar)60536;
				break;
		case 5:
				TH0 = 60536 >> 8;
				TL0 = (uchar)60536;
				order = 0;
				break;
		default:order = 0;
				break;
	}
	order++;
}

射频模块

详细了解见IC卡扇区解读,这个对你理解IC卡的原理有帮助。

总结
整个系统源码:
main.c

#include "lpcreg.h"
#include "main.h"
#include "mfrc522.h"


typedef unsigned char uchar;
typedef unsigned int uint;


unsigned int PWN_Value;				//定义pwn值
unsigned char order = 0;

unsigned char g_ucTempbuf[20];

                        
void delay1(unsigned int z)
{
	unsigned int x,y;
	for(x=z;x>0;x--)
	for(y=110;y>0;y--);	
} 


void main( )
{    
	 
     unsigned char status,i;
	 unsigned char id[4];

	 delay1(200);

     InitializeSystem();
     PcdReset();
     PcdAntennaOff();
	 delay1(10); 
     PcdAntennaOn();
	  
     while ( 1 )
     {   


    	status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡
        if (status != MI_OK)
        {    
       		InitializeSystem();
           	PcdReset();
     		PcdAntennaOff();
			delay1(10); 
     		PcdAntennaOn(); 
			continue;
        }
			     
			
       	status = PcdAnticoll(g_ucTempbuf);//防冲撞
        if (status != MI_OK) {    
			continue;    
		}

		for(i = 0; i < 4; i++) {	 		//将IC卡序列号存取到id中
			id[i] = g_ucTempbuf[i];
		}

		if((id[0] == 0xBB && id[1] == 0x88 && id[2] == 0x1A && id[3] == 0xD3) || 
		   (id[0] == 0xB3 && id[1] == 0x44 && id[2] == 0x70 && id[3] == 0x36) ||
		   (id[0] == 0x15 && id[1] == 0xE2 && id[2] == 0x8E && id[3] == 0x15) ||
		   (id[0] == 0x2A && id[1] == 0xA9 && id[2] == 0x74 && id[3] == 0x19) ||
		   (id[0] == 0x52 && id[1] == 0xD4 && id[2] == 0x6C && id[3] == 0xFC) ||
		   (id[0] == 0xB5 && id[1] == 0x44 && id[2] == 0x90 && id[3] == 0x15) ||
		   (id[0] == 0x32 && id[1] == 0x9F && id[2] == 0x13 && id[3] == 0x64) ||
		   (id[0] == 0x85 && id[1] == 0x58 && id[2] == 0x90 && id[3] == 0x15) ||
		   (id[0] == 0x95 && id[1] == 0x35 && id[2] == 0x7B && id[3] == 0x15)
		) {//可以下载手机app:NFC Writer 来读取卡的序列号
			doorOpen();

			delay1(4000);	  //等待数秒,可调

			servoRet();
		}

		PcdHalt();
    }
}


/
//系统初始化
/
void InitializeSystem()
{
	P0M1 = 0x0; P0M2 = 0x0;
	P1M1 = 0x0;	P1M2 = 0x0;                      
	P3M1 = 0x0; P3M2 = 0xFF; 
	P0 = 0xFF; P1 = 0xFF; P3 = 0xFF;P2 = 0xFF;

 	TMOD=0x21;		   //设T0为方式1,GATE=1;
	SCON=0x50;
	TH1=0xFa;          //波特率为4800bps
	TL1=0xFa;
	TH0=0;
	TL0=0; 
	TR0=1;  
	ET0=1;             //允许T0中断
	TR1=1;			   //开启定时器
	TI=1;
	EA=1;			   	//开启总中断
	
	ES = 1;
	RI = 1;

}
  /*定时器初始化*/
void Init_Timer0()
{
	TMOD = 0x11;
	TH0 = (65536 - 500) / 256;
	TL0 = (65536 - 500) % 256;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
	PT0 = 1;
}


/*中断程序*/
void timer0(void) interrupt 1
{
	
	switch(order)
	{
		case 1: PWN_OUT = 1;
				TH0 = (65536 - PWN_Value) >> 8;
				TL0 = (uchar)(65536 - PWN_Value);
				break;

		case 2: PWN_OUT = 0;
				TH0 = (65536 - (5000 - PWN_Value)) >> 8;
				TL0 = (uchar)(65536 - (5000 - PWN_Value));
				break;
		case 3:
				TH0 = 60536 >> 8;
				TL0 = (uchar)60536;
				break;
		case 4: 
				TH0 = 60536 >> 8;
				TL0 = (uchar)60536;
				break;
		case 5:
				TH0 = 60536 >> 8;
				TL0 = (uchar)60536;
				order = 0;
				break;
		default:order = 0;
				break;
	}
	order++;
}

void doorOpen()
{
    Init_Timer0();
    PWN_Value = 2000;
}

void servoRet()
{
    Init_Timer0();
    PWN_Value = 500;
}

main.h

//
//端口定义
/
//MFRC500
sbit     MF522_NSS  =P2^4 ;			//RC500片选 SDA
sbit     MF522_SCK  = P2^5;
sbit     MF522_SI   = P2^3;         //MOSI
sbit     MF522_SO   = P2^2;         //MISO
sbit     MF522_RST  = P2^1; 
//舵机
sbit PWN_OUT = P2^7;		//PWN输出口 

//函数原型
/
void InitializeSystem();  
void Init_Timer0();             //定时器初始化
void doorOpen();                //开门
void servoRet();                //舵机复位

lpcreg.h

/*--------------------------------------------------------------------------
REG52.H

Header file for generic 80C52 and 80C32 microcontroller.
Copyright (c) 1988-2001 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/

/*  BYTE Registers  */
sfr P0    = 0x80;
sfr P1    = 0x90;
sfr P2    = 0xA0;
sfr P3    = 0xB0;
sfr PSW   = 0xD0;
sfr ACC   = 0xE0;
sfr B     = 0xF0;
sfr SP    = 0x81;
sfr DPL   = 0x82;
sfr DPH   = 0x83;
sfr PCON  = 0x87;
sfr PCONA = 0xB5;
sfr TCON  = 0x88;
sfr TMOD  = 0x89;
sfr TL0   = 0x8A;
sfr TL1   = 0x8B;
sfr TH0   = 0x8C;
sfr TH1   = 0x8D;
sfr IE    = 0xA8;
sfr IEN1  = 0xE8;
sfr IP    = 0xB8;
sfr SCON  = 0x98;
sfr SBUF  = 0x99;

/*  8052 Extensions  */
sfr T2CON  = 0xC8;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2    = 0xCC;
sfr TH2    = 0xCD;

/* LPC */
sfr P0M1   = 0x84;
sfr P0M2   = 0x85;
sfr P1M1   = 0x91;
sfr P1M2   = 0x92;
sfr P3M1   = 0xB1;
sfr P3M2   = 0xB2;

sfr      SSTAT      =    0xBA;
sfr      BRGCON     =    0xBD;
sfr      BRG0       =    0xBE;
sfr      BRG1       =    0xBF;

sfr16    RCAP2LH    =    0xCA;
sfr16    T2LH       =    0xCC;  
sfr16    T1LH       =    0x8A;  
sfr      TAMOD  = 0x8F;
sfr      I2ADR  = 0xDB;
sfr      I2CON  = 0xD8;
sfr      I2DAT  = 0xDA;
sfr      I2STAT  = 0xD9;

sfr    WDCON = 0xA7;

sbit SI    = I2CON^3;
//sbit SI    = I2CON^3;
/*  BIT Registers  */
/*  PSW  */
sbit CY    = PSW^7;
sbit AC    = PSW^6;
sbit F0    = PSW^5;
sbit RS1   = PSW^4;
sbit RS0   = PSW^3;
sbit OV    = PSW^2;
sbit P     = PSW^0; //8052 only

/*  TCON  */
sbit TF1   = TCON^7;
sbit TR1   = TCON^6;
sbit TF0   = TCON^5;
sbit TR0   = TCON^4;
sbit IE1   = TCON^3;
sbit IT1   = TCON^2;
sbit IE0   = TCON^1;
sbit IT0   = TCON^0;

/*  IE  */
sbit EA    = IE^7;
sbit ET2   = IE^5; //8052 only
sbit ES    = IE^4;
sbit ET1   = IE^3;
sbit EX1   = IE^2;
sbit ET0   = IE^1;
sbit EX0   = IE^0;

/*  IP  */
sbit PT2   = IP^5;
sbit PS    = IP^4;
sbit PT1   = IP^3;
sbit PX1   = IP^2;
sbit PT0   = IP^1;
sbit PX0   = IP^0;

/*  P3  */
sbit RD    = P3^7;
sbit WR    = P3^6;
sbit T1    = P3^5;
sbit T0    = P3^4;
sbit INT1  = P3^3;
sbit INT0  = P3^2;
sbit TXD   = P3^1;
sbit RXD   = P3^0;

/*  SCON  */
sbit SM0   = SCON^7;
sbit SM1   = SCON^6;
sbit SM2   = SCON^5;
sbit REN   = SCON^4;
sbit TB8   = SCON^3;
sbit RB8   = SCON^2;
sbit TI    = SCON^1;
sbit RI    = SCON^0;

/*  P1  */
sbit T2EX  = P1^1; // 8052 only
sbit T2    = P1^0; // 8052 only
             
/*  T2CON  */
sbit TF2    = T2CON^7;
sbit EXF2   = T2CON^6;
sbit RCLK   = T2CON^5;
sbit TCLK   = T2CON^4;
sbit EXEN2  = T2CON^3;
sbit TR2    = T2CON^2;
sbit C_T2   = T2CON^1;
sbit CP_RL2 = T2CON^0;

mfrc522.h

/
//函数原型
/
char PcdReset(void);
void PcdAntennaOn(void);
void PcdAntennaOff(void);
char PcdRequest(unsigned char req_code,unsigned char *pTagType);   
char PcdAnticoll(unsigned char *pSnr);
                                 
char PcdHalt(void);
char PcdComMF522(unsigned char Command, 
                 unsigned char *pInData, 
                 unsigned char InLenByte,
                 unsigned char *pOutData, 
                 unsigned int  *pOutLenBit);
void CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData); 
void WriteRawRC(unsigned char Address,unsigned char value);
unsigned char ReadRawRC(unsigned char Address); 
void SetBitMask(unsigned char reg,unsigned char mask); 
void ClearBitMask(unsigned char reg,unsigned char mask); 

/
//MF522命令字
/
#define PCD_IDLE              0x00               //取消当前命令
#define PCD_AUTHENT           0x0E               //验证密钥
#define PCD_RECEIVE           0x08               //接收数据
#define PCD_TRANSMIT          0x04               //发送数据
#define PCD_TRANSCEIVE        0x0C               //发送并接收数据
#define PCD_RESETPHASE        0x0F               //复位
#define PCD_CALCCRC           0x03               //CRC计算

/
//Mifare_One卡片命令字
/
#define PICC_REQIDL           0x26               //寻天线区内未进入休眠状态
#define PICC_REQALL           0x52               //寻天线区内全部卡
#define PICC_ANTICOLL1        0x93               //防冲撞
#define PICC_ANTICOLL2        0x95               //防冲撞
#define PICC_AUTHENT1A        0x60               //验证A密钥
#define PICC_AUTHENT1B        0x61               //验证B密钥
#define PICC_READ             0x30               //读块
#define PICC_WRITE            0xA0               //写块
#define PICC_DECREMENT        0xC0               //扣款
#define PICC_INCREMENT        0xC1               //充值
#define PICC_RESTORE          0xC2               //调块数据到缓冲区
#define PICC_TRANSFER         0xB0               //保存缓冲区中数据
#define PICC_HALT             0x50               //休眠

/
//MF522 FIFO长度定义
/
#define DEF_FIFO_LENGTH       64                 //FIFO size=64byte

/
//MF522寄存器定义
/
// PAGE 0
#define     RFU00                 0x00    
#define     CommandReg            0x01    
#define     ComIEnReg             0x02    
#define     DivlEnReg             0x03    
#define     ComIrqReg             0x04    
#define     DivIrqReg             0x05
#define     ErrorReg              0x06    
#define     Status1Reg            0x07    
#define     Status2Reg            0x08    
#define     FIFODataReg           0x09
#define     FIFOLevelReg          0x0A
#define     WaterLevelReg         0x0B
#define     ControlReg            0x0C
#define     BitFramingReg         0x0D
#define     CollReg               0x0E
#define     RFU0F                 0x0F
// PAGE 1     
#define     RFU10                 0x10
#define     ModeReg               0x11
#define     TxModeReg             0x12
#define     RxModeReg             0x13
#define     TxControlReg          0x14
#define     TxAutoReg             0x15
#define     TxSelReg              0x16
#define     RxSelReg              0x17
#define     RxThresholdReg        0x18
#define     DemodReg              0x19
#define     RFU1A                 0x1A
#define     RFU1B                 0x1B
#define     MifareReg             0x1C
#define     RFU1D                 0x1D
#define     RFU1E                 0x1E
#define     SerialSpeedReg        0x1F
// PAGE 2    
#define     RFU20                 0x20  
#define     CRCResultRegM         0x21
#define     CRCResultRegL         0x22
#define     RFU23                 0x23
#define     ModWidthReg           0x24
#define     RFU25                 0x25
#define     RFCfgReg              0x26
#define     GsNReg                0x27
#define     CWGsCfgReg            0x28
#define     ModGsCfgReg           0x29
#define     TModeReg              0x2A
#define     TPrescalerReg         0x2B
#define     TReloadRegH           0x2C
#define     TReloadRegL           0x2D
#define     TCounterValueRegH     0x2E
#define     TCounterValueRegL     0x2F
// PAGE 3      
#define     RFU30                 0x30
#define     TestSel1Reg           0x31
#define     TestSel2Reg           0x32
#define     TestPinEnReg          0x33
#define     TestPinValueReg       0x34
#define     TestBusReg            0x35
#define     AutoTestReg           0x36
#define     VersionReg            0x37
#define     AnalogTestReg         0x38
#define     TestDAC1Reg           0x39  
#define     TestDAC2Reg           0x3A   
#define     TestADCReg            0x3B   
#define     RFU3C                 0x3C   
#define     RFU3D                 0x3D   
#define     RFU3E                 0x3E   
#define     RFU3F		  0x3F

/
//和MF522通讯时返回的错误代码
/
#define MI_OK                          0
#define MI_NOTAGERR                    (-1)
#define MI_ERR                         (-2)


rc522.c

#include 
#include "lpcreg.h"
#include "main.h"
#include "mfrc522.h"

#define MAXRLEN 18
                 


           
/
//功    能:寻卡
//参数说明: req_code[IN]:寻卡方式
//                0x52 = 寻感应区内所有符合14443A标准的卡
//                0x26 = 寻未进入休眠状态的卡
//          pTagType[OUT]:卡片类型代码
//                0x4400 = Mifare_UltraLight
//                0x0400 = Mifare_One(S50)
//                0x0200 = Mifare_One(S70)
//                0x0800 = Mifare_Pro(X)
//                0x4403 = Mifare_DESFire
//返    回: 成功返回MI_OK
/
char PcdRequest(unsigned char req_code,unsigned char *pTagType)
{
   char status;  
   unsigned int  unLen;
   unsigned char ucComMF522Buf[MAXRLEN]; 

   ClearBitMask(Status2Reg,0x08);
   WriteRawRC(BitFramingReg,0x07);
   SetBitMask(TxControlReg,0x03);
 
   ucComMF522Buf[0] = req_code;

   status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);
   
   if ((status == MI_OK) && (unLen == 0x10))
   {    
       *pTagType     = ucComMF522Buf[0];
       *(pTagType+1) = ucComMF522Buf[1];
   }
   else
   {   status = MI_ERR;  
 


	}
   
   return status;
}

/
//功    能:防冲撞
//参数说明: pSnr[OUT]:卡片序列号,4字节
//返    回: 成功返回MI_OK
/  
char PcdAnticoll(unsigned char *pSnr)
{
    char status;
    unsigned char i,snr_check=0;
    unsigned int  unLen;
    unsigned char ucComMF522Buf[MAXRLEN]; 
    

    ClearBitMask(Status2Reg,0x08);
    WriteRawRC(BitFramingReg,0x00);
    ClearBitMask(CollReg,0x80);
 
    ucComMF522Buf[0] = PICC_ANTICOLL1;
    ucComMF522Buf[1] = 0x20;

    status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,2,ucComMF522Buf,&unLen);

    if (status == MI_OK)
    {
    	 for (i=0; i<4; i++)
         {   
             *(pSnr+i)  = ucComMF522Buf[i];
             snr_check ^= ucComMF522Buf[i];

         }
         if (snr_check != ucComMF522Buf[i])
         {   status = MI_ERR;    }
    }
    
    SetBitMask(CollReg,0x80);
    return status;
}



/
//功    能:复位RC522
//返    回: 成功返回MI_OK
/
char PcdReset(void)
{
	//unsigned char i;
    MF522_RST=1;

		_nop_();                  

    MF522_RST=0;

		_nop_();                   

    MF522_RST=1;

		_nop_();                 
	
    WriteRawRC(CommandReg,PCD_RESETPHASE);

		_nop_();                  
	
    
    WriteRawRC(ModeReg,0x3D);            //和Mifare卡通讯,CRC初始值0x6363
    WriteRawRC(TReloadRegL,30);           
    WriteRawRC(TReloadRegH,0);
    WriteRawRC(TModeReg,0x8D);
    WriteRawRC(TPrescalerReg,0x3E);
   WriteRawRC(TxAutoReg,0x40);
    return MI_OK;
}

/
//功    能:读RC632寄存器
//参数说明:Address[IN]:寄存器地址
//返    回:读出的值
/
unsigned char ReadRawRC(unsigned char Address)
{
     unsigned char i, ucAddr;
     unsigned char ucResult=0;

     MF522_SCK = 0;
     MF522_NSS = 0;
     ucAddr = ((Address<<1)&0x7E)|0x80;

     for(i=8;i>0;i--)
     {
         MF522_SI = ((ucAddr&0x80)==0x80);
         MF522_SCK = 1;
         ucAddr <<= 1;
         MF522_SCK = 0;
     }

     for(i=8;i>0;i--)
     {
         MF522_SCK = 1;
         ucResult <<= 1;
         ucResult|=(bit)MF522_SO;
         MF522_SCK = 0;
     }

     MF522_NSS = 1;
     MF522_SCK = 1;
     return ucResult;
}

/
//功    能:写RC632寄存器
//参数说明:Address[IN]:寄存器地址
//          value[IN]:写入的值
/
void WriteRawRC(unsigned char Address, unsigned char value)
{  
    unsigned char i, ucAddr;

    MF522_SCK = 0;
    MF522_NSS = 0;
    ucAddr = ((Address<<1)&0x7E);

    for(i=8;i>0;i--)
    {
        MF522_SI = ((ucAddr&0x80)==0x80);
        MF522_SCK = 1;
        ucAddr <<= 1;
        MF522_SCK = 0;
    }

    for(i=8;i>0;i--)
    {
        MF522_SI = ((value&0x80)==0x80);
        MF522_SCK = 1;
        value <<= 1;
        MF522_SCK = 0;
    }
    MF522_NSS = 1;
    MF522_SCK = 1;
}

/
//功    能:置RC522寄存器位
//参数说明:reg[IN]:寄存器地址
//          mask[IN]:置位值
/
void SetBitMask(unsigned char reg,unsigned char mask)  
{
    char tmp = 0x0;
    tmp = ReadRawRC(reg);
    WriteRawRC(reg,tmp | mask);  // set bit mask
}

/
//功    能:清RC522寄存器位
//参数说明:reg[IN]:寄存器地址
//          mask[IN]:清位值
/
void ClearBitMask(unsigned char reg,unsigned char mask)  
{
    char tmp = 0x0;
    tmp = ReadRawRC(reg);
    WriteRawRC(reg, tmp & ~mask);  // clear bit mask
} 

/
//功    能:通过RC522和ISO14443卡通讯
//参数说明:Command[IN]:RC522命令字
//          pInData[IN]:通过RC522发送到卡片的数据
//          InLenByte[IN]:发送数据的字节长度
//          pOutData[OUT]:接收到的卡片返回数据
//          *pOutLenBit[OUT]:返回数据的位长度
/
char PcdComMF522(unsigned char Command, 
                 unsigned char *pInData, 
                 unsigned char InLenByte,
                 unsigned char *pOutData, 
                 unsigned int  *pOutLenBit)
{
    char status = MI_ERR;
    unsigned char irqEn   = 0x00;
    unsigned char waitFor = 0x00;
    unsigned char lastBits;
    unsigned char n;
    unsigned int i;
    switch (Command)
    {
       case PCD_AUTHENT:
          irqEn   = 0x12;
          waitFor = 0x10;
          break;
       case PCD_TRANSCEIVE:
          irqEn   = 0x77;
          waitFor = 0x30;
          break;
       default:
         break;
    }
   
    WriteRawRC(ComIEnReg,irqEn|0x80);
    ClearBitMask(ComIrqReg,0x80);
    WriteRawRC(CommandReg,PCD_IDLE);
    SetBitMask(FIFOLevelReg,0x80);
    
    for (i=0; i<InLenByte; i++)
    {   WriteRawRC(FIFODataReg, pInData[i]);    }
    WriteRawRC(CommandReg, Command);
   
    
    if (Command == PCD_TRANSCEIVE)
    {    SetBitMask(BitFramingReg,0x80);  }
    
    i = 600;//根据时钟频率调整,操作M1卡最大等待时间25ms
    do 
    {
         n = ReadRawRC(ComIrqReg);
         i--;
    }
    while ((i!=0) && !(n&0x01) && !(n&waitFor));
    ClearBitMask(BitFramingReg,0x80);
	      
    if (i!=0)
    {    
         if(!(ReadRawRC(ErrorReg)&0x1B))
         {
             status = MI_OK;
             if (n & irqEn & 0x01)
             {   status = MI_NOTAGERR;   }
             if (Command == PCD_TRANSCEIVE)
             {
               	n = ReadRawRC(FIFOLevelReg);
              	lastBits = ReadRawRC(ControlReg) & 0x07;
                if (lastBits)
                {   *pOutLenBit = (n-1)*8 + lastBits;   }
                else
                {   *pOutLenBit = n*8;   }
                if (n == 0)
                {   n = 1;    }
                if (n > MAXRLEN)
                {   n = MAXRLEN;   }
                for (i=0; i<n; i++)
                {   pOutData[i] = ReadRawRC(FIFODataReg);    }
            }
         }
         else
         {   status = MI_ERR;   }
        
   }
   

   SetBitMask(ControlReg,0x80);           // stop timer now
   WriteRawRC(CommandReg,PCD_IDLE); 
   return status;
}


/
//开启天线  
//每次启动或关闭天险发射之间应至少有1ms的间隔
/
void PcdAntennaOn()
{
    unsigned char i;
    i = ReadRawRC(TxControlReg);
    if (!(i & 0x03))
    {
        SetBitMask(TxControlReg, 0x03);
    }
}


/
//关闭天线
/
void PcdAntennaOff()
{
    ClearBitMask(TxControlReg, 0x03);
}

  /
//用MF522计算CRC16函数
/
void CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData)
{
    unsigned char i,n;
    ClearBitMask(DivIrqReg,0x04);
    WriteRawRC(CommandReg,PCD_IDLE);
    SetBitMask(FIFOLevelReg,0x80);
    for (i=0; i<len; i++)
    {   WriteRawRC(FIFODataReg, *(pIndata+i));   }
    WriteRawRC(CommandReg, PCD_CALCCRC);
    i = 0xFF;
    do 
    {
        n = ReadRawRC(DivIrqReg);
        i--;
    }
    while ((i!=0) && !(n&0x04));
    pOutData[0] = ReadRawRC(CRCResultRegL);
    pOutData[1] = ReadRawRC(CRCResultRegM);
}

 /
//功    能:命令卡片进入休眠状态
//返    回: 成功返回MI_OK
/
char PcdHalt(void)
{
    char status;
    unsigned int  unLen;
    unsigned char ucComMF522Buf[MAXRLEN]; 

    ucComMF522Buf[0] = PICC_HALT;
    ucComMF522Buf[1] = 0;
    CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
 
    status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);

    return MI_OK;
}

你可能感兴趣的:(笔记,单片机)