大家好,又见面了,我是你们的朋友全栈君。
代码语言:javascript复制 RFID RC522门禁系统
RFID RC522门禁系统说明: 基于51单片机的RFID RC522门禁系统的主要功能是,在本系统中主要是演示了RFID RC522门禁系统,在单片机内部的RAM中IDBUFFER中存了学号:1505106001对应 16进制就是5b b6 18 51 ;然后在RFID RC522开卡系统中,为第一张卡在块地址0x08处,写入了学号,当卡中的内容和单片机上的内容相配时,门打开。 具体程序如下所示: MAIN.C
代码语言:javascript复制在这里插入代码片
#include "reg52.h"
#include "main.h"
#include "mfrc522.h"
#include <string.h>
//M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
//4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
//unsigned char code data2[4] = {0x12,0,0,0};
unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char g_ucTempbuf[20];
unsigned char idata MLastSelectedSnr[4];
unsigned char idata RevBuffer[30];
unsigned char data IDBuffer[16]={0x59,0xB6,0x18,0x51,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
void ID_Verify();
void delay_ms(unsigned int ms);
void iccardcode()
{
unsigned char cmd;
unsigned char status;
cmd = RevBuffer[0];
switch(cmd)
{
case 1: // Halt the card //终止卡的操作
status= PcdHalt();;
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 2: // Request,Anticoll,Select,return CardType(2 bytes) CardSerialNo(4 bytes)
status= PcdRequest(RevBuffer[1],&RevBuffer[2]);
if(status!=0)
{
status= PcdRequest(RevBuffer[1],&RevBuffer[2]);
if(status!=0)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
}
}
RevBuffer[0]=3;
RevBuffer[1]=status;
break;
case 3: // 防冲突 读卡的系列号 MLastSelectedSnr
status = PcdAnticoll(&RevBuffer[2]);
if(status!=0)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
}
memcpy(MLastSelectedSnr,&RevBuffer[2],4);
RevBuffer[0]=5;
RevBuffer[1]=status;
break;
case 4: // 选择卡 Select Card
status=PcdSelect(MLastSelectedSnr);
if(status!=MI_OK)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
}
RevBuffer[0]=3;
RevBuffer[1]=status;
break;
case 5: // Key loading into the MF RC500's EEPROM
status = PcdAuthState(RevBuffer[1], RevBuffer[3], DefaultKey, MLastSelectedSnr);// 校验卡密码
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 6:
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 7:
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 8: // Read the mifare card
// 读卡
status=PcdRead(RevBuffer[1],&RevBuffer[2]);
if(status==0)
{RevBuffer[0]=17;}
else
{RevBuffer[0]=1;}
RevBuffer[1]=status;
break;
case 9: // Write the mifare card
// 写卡 下载密码
status=PcdWrite(RevBuffer[1],&RevBuffer[2]);
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 10:
PcdValue(RevBuffer[1],RevBuffer[2],&RevBuffer[3]);
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 12: // 参数设置
PcdBakValue(RevBuffer[1], RevBuffer[2]);
RevBuffer[0]=1; //contact
RevBuffer[1]=0;
break;
}
}
/
//系统初始化
/
void InitializeSystem()
{
P0 = 0xFF; P1 = 0xFF; P3 = 0xFF;
SCON=0X50; //设置为工作方式1
TMOD=0X20; //设置计数器工作方式2
PCON=0X80; //波特率加倍
TH1=0XF3; //计数器初始值设置,注意波特率是4800的
TL1=0XF3;
ES=1; //打开接收中断
EA=1; //打开总中断
TR1=1; //打开计数
LED_GREEN = 1;
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
M500PcdConfigISOType( 'A' );
LED_GREEN = 0;
delay_10ms(10);
LED_GREEN = 1;
delay_10ms(10);
LED_GREEN = 0;
}
/
//用T2做延时子程序
/
///
// 接收和发送中断
///
void isr_UART(void) interrupt 4 using 1
{
unsigned char len, i;
if(TI)
{
TI=0;
len=RevBuffer[0];
for(i=0;i<len 1;i )
{
SBUF=RevBuffer[i];
while(!TI);
TI=0;
}
REN=1;
}
}
void main( )
{
InitializeSystem( );
ID_Verify();
}
void delay_ms(unsigned int ms)
{
unsigned int m,n;
for(m=ms;m>0;m--)
for(n=110;n>0;n--);
}
void ID_Verify()
{
while(1)
{
//寻卡
RevBuffer[0]=2;
RevBuffer[1]=0x52;
iccardcode();
if(RevBuffer[1]!=0)
{
continue;
}
TI=1;
//防冲突
RevBuffer[0]=0x03;
iccardcode();
if(RevBuffer[1]!=0)
{
continue;
}
TI=1;
//选择卡
RevBuffer[0]=0x04;
iccardcode();
if(RevBuffer[1]!=0)
{
continue;
}
TI=1;
//密码检验
RevBuffer[0]=0x05;
RevBuffer[1]=0x61;
RevBuffer[3]=0x08;
iccardcode();
if(RevBuffer[1]!=0)
{
continue;
}
TI=1;
//读08块的16个字节数据
RevBuffer[0]=0x08;
RevBuffer[1]=0x08;
iccardcode();
if(RevBuffer[1]!=0)
{
continue;
}
if((RevBuffer[2]==IDBuffer[0])&&(RevBuffer[3]==IDBuffer[1])&&(RevBuffer[4]==IDBuffer[2])&&(RevBuffer[5]==IDBuffer[3]))
{
DOOR_SWITCH=0;
delay_ms(5000);
DOOR_SWITCH=1;
}
TI=1;
}
}
MAIN.H
代码语言:javascript复制在这里插入代码片
//
//端口定义
/
//MFRC522
sbit MF522_RST = P1^4; //RC500片选
sbit MF522_NSS = P1^0;
sbit MF522_SCK = P1^1;
sbit MF522_SI = P1^2;
sbit MF522_SO = P1^3;
//指示灯
sbit LED_GREEN = P1^5;
//继电器
sbit DOOR_SWITCH = P1^6;
/
//函数原型
/
void InitializeSystem();
#define BAUD_115200 256 - (OSC_FREQ/192L)/115200L // 255
#define BAUD_57600 256 - (OSC_FREQ/192L)/57600L // 254
#define BAUD_38400 256 - (OSC_FREQ/192L)/38400L // 253
#define BAUD_28800 256 - (OSC_FREQ/192L)/28800L // 252
#define BAUD_19200 256 - (OSC_FREQ/192L)/19200L // 250
#define BAUD_14400 256 - (OSC_FREQ/192L)/14400L // 248
#define BAUD_9600 256 - (OSC_FREQ/192L)/9600L // 244
#define OSC_FREQ 22118400L
//#define OSC_FREQ 7372800L
#define OSC_FREQ 22118400L
#define RCAP2_50us 65536L - OSC_FREQ/40417L
#define RCAP2_1ms 65536L - OSC_FREQ/2000L
#define RCAP2_10ms 65536L - OSC_FREQ/1200L
#define TIME0_500us 65536L - OSC_FREQ/8000L
#define TIME0_10ms 65536L - OSC_FREQ/200
#define CALL_isr_UART() TI = 1
#define TRUE 1
#define FALSE 0
RC522.C
代码语言:javascript复制在这里插入代码片
#include <intrins.h>
#include "reg52.h"
#include "main.h"
#include "mfrc522.h"
#include <string.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];
// unsigned char xTest ;
ClearBitMask(Status2Reg,0x08);
WriteRawRC(BitFramingReg,0x07);
// xTest = ReadRawRC(BitFramingReg);
// if(xTest == 0x07 )
// { LED_GREEN =0 ;}
// else {LED_GREEN =1 ;while(1){}}
SetBitMask(TxControlReg,0x03);
ucComMF522Buf[0] = req_code;
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,1,ucComMF522Buf,&unLen);
// if(status == MI_OK )
// { LED_GREEN =0 ;}
// else {LED_GREEN =1 ;}
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;
}
/
//功 能:选定卡片
//参数说明: pSnr[IN]:卡片序列号,4字节
//返 回: 成功返回MI_OK
/
char PcdSelect(unsigned char *pSnr)
{
char status;
unsigned char i;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x70;
ucComMF522Buf[6] = 0;
for (i=0; i<4; i )
{
ucComMF522Buf[i 2] = *(pSnr i);
ucComMF522Buf[6] ^= *(pSnr i);
}
CalulateCRC(ucComMF522Buf,7,&ucComMF522Buf[7]);
ClearBitMask(Status2Reg,0x08);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,9,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x18))
{ status = MI_OK; }
else
{ status = MI_ERR; }
return status;
}
/
//功 能:验证卡片密码
//参数说明: auth_mode[IN]: 密码验证模式
// 0x60 = 验证A密钥
// 0x61 = 验证B密钥
// addr[IN]:块地址
// pKey[IN]:密码
// pSnr[IN]:卡片序列号,4字节
//返 回: 成功返回MI_OK
/
char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = auth_mode;
ucComMF522Buf[1] = addr;
for (i=0; i<6; i )
{ ucComMF522Buf[i 2] = *(pKey i); }
for (i=0; i<6; i )
{ ucComMF522Buf[i 8] = *(pSnr i); }
// memcpy(&ucComMF522Buf[2], pKey, 6);
// memcpy(&ucComMF522Buf[8], pSnr, 4);
status = PcdComMF522(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (!(ReadRawRC(Status2Reg) & 0x08)))
{ status = MI_ERR; }
return status;
}
/
//功 能:读取M1卡一块数据
//参数说明: addr[IN]:块地址
// pData[OUT]:读出的数据,16字节
//返 回: 成功返回MI_OK
/
char PcdRead(unsigned char addr,unsigned char *pData)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_READ;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status == MI_OK) && (unLen == 0x90))
// { memcpy(pData, ucComMF522Buf, 16); }
{
for (i=0; i<16; i )
{ *(pData i) = ucComMF522Buf[i]; }
}
else
{ status = MI_ERR; }
return status;
}
/
//功 能:写数据到M1卡一块
//参数说明: addr[IN]:块地址
// pData[IN]:写入的数据,16字节
//返 回: 成功返回MI_OK
/
char PcdWrite(unsigned char addr,unsigned char *pData)
{
char status;
unsigned int unLen;
unsigned char i,ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_WRITE;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
if (status == MI_OK)
{
//memcpy(ucComMF522Buf, pData, 16);
for (i=0; i<16; i )
{ ucComMF522Buf[i] = *(pData i); }
CalulateCRC(ucComMF522Buf,16,&ucComMF522Buf[16]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
}
return status;
}
/
//功 能:命令卡片进入休眠状态
//返 回: 成功返回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;
}
/
//用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);
}
/
//功 能:复位RC522
//返 回: 成功返回MI_OK
/
char PcdReset(void)
{
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的工作方式
//
char M500PcdConfigISOType(unsigned char type)
{
if (type == 'A') //ISO14443_A
{
ClearBitMask(Status2Reg,0x08);
/* WriteRawRC(CommandReg,0x20); //as default
WriteRawRC(ComIEnReg,0x80); //as default
WriteRawRC(DivlEnReg,0x0); //as default
WriteRawRC(ComIrqReg,0x04); //as default
WriteRawRC(DivIrqReg,0x0); //as default
WriteRawRC(Status2Reg,0x0);//80 //trun off temperature sensor
WriteRawRC(WaterLevelReg,0x08); //as default
WriteRawRC(ControlReg,0x20); //as default
WriteRawRC(CollReg,0x80); //as default
*/
WriteRawRC(ModeReg,0x3D);//3F
/* WriteRawRC(TxModeReg,0x0); //as default???
WriteRawRC(RxModeReg,0x0); //as default???
WriteRawRC(TxControlReg,0x80); //as default???
WriteRawRC(TxSelReg,0x10); //as default???
*/
WriteRawRC(RxSelReg,0x86);//84
// WriteRawRC(RxThresholdReg,0x84);//as default
// WriteRawRC(DemodReg,0x4D); //as default
// WriteRawRC(ModWidthReg,0x13);//26
WriteRawRC(RFCfgReg,0x7F); //4F
/* WriteRawRC(GsNReg,0x88); //as default???
WriteRawRC(CWGsCfgReg,0x20); //as default???
WriteRawRC(ModGsCfgReg,0x20); //as default???
*/
WriteRawRC(TReloadRegL,30);//tmoLength);// TReloadVal = 'h6a =tmoLength(dec)
WriteRawRC(TReloadRegH,0);
WriteRawRC(TModeReg,0x8D);
WriteRawRC(TPrescalerReg,0x3E);
// PcdSetTmo(106);
delay_10ms(1);
PcdAntennaOn();
}
else{ return -1; }
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
i = 2000;
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);
}
/
//功 能:扣款和充值
//参数说明: dd_mode[IN]:命令字
// 0xC0 = 扣款
// 0xC1 = 充值
// addr[IN]:钱包地址
// pValue[IN]:4字节增(减)值,低位在前
//返 回: 成功返回MI_OK
/
char PcdValue(unsigned char dd_mode,unsigned char addr,unsigned char *pValue)
{
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = dd_mode;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
if (status == MI_OK)
{
memcpy(ucComMF522Buf, pValue, 4);
// for (i=0; i<16; i )
// { ucComMF522Buf[i] = *(pValue i); }
CalulateCRC(ucComMF522Buf,4,&ucComMF522Buf[4]);
unLen = 0;
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,6,ucComMF522Buf,&unLen);
if (status != MI_ERR)
{ status = MI_OK; }
}
if (status == MI_OK)
{
ucComMF522Buf[0] = PICC_TRANSFER;
ucComMF522Buf[1] = addr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
}
return status;
}
/
//功 能:备份钱包
//参数说明: sourceaddr[IN]:源地址
// goaladdr[IN]:目标地址
//返 回: 成功返回MI_OK
/
char PcdBakValue(unsigned char sourceaddr, unsigned char goaladdr)
{
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ucComMF522Buf[0] = PICC_RESTORE;
ucComMF522Buf[1] = sourceaddr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
if (status == MI_OK)
{
ucComMF522Buf[0] = 0;
ucComMF522Buf[1] = 0;
ucComMF522Buf[2] = 0;
ucComMF522Buf[3] = 0;
CalulateCRC(ucComMF522Buf,4,&ucComMF522Buf[4]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,6,ucComMF522Buf,&unLen);
if (status != MI_ERR)
{ status = MI_OK; }
}
if (status != MI_OK)
{ return MI_ERR; }
ucComMF522Buf[0] = PICC_TRANSFER;
ucComMF522Buf[1] = goaladdr;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = PcdComMF522(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
return status;
}
///
// Delay 10ms
///
void delay_10ms(unsigned int _10ms)
{
#ifndef NO_TIMER2
RCAP2LH = RCAP2_10ms;
T2LH = RCAP2_10ms;
TR2 = TRUE;
while (_10ms--)
{
while (!TF2);
TF2 = FALSE;
}
TR2 = FALSE;
#else
while (_10ms--)
{
delay_50us(19);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid )
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(20);
if (CmdValid)
return;
delay_50us(19);
if (CmdValid)
return;
}
#endif
}
RC522.H
代码语言:javascript复制在这里插入代码片
/
//函数原型
/
char PcdReset(void);
void PcdAntennaOn(void);
void PcdAntennaOff(void);
char PcdRequest(unsigned char req_code,unsigned char *pTagType);
char PcdAnticoll(unsigned char *pSnr);
char PcdSelect(unsigned char *pSnr);
char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr);
char PcdRead(unsigned char addr,unsigned char *pData);
char PcdWrite(unsigned char addr,unsigned char *pData);
char PcdValue(unsigned char dd_mode,unsigned char addr,unsigned char *pValue);
char PcdBakValue(unsigned char sourceaddr, unsigned char goaladdr);
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);
char M500PcdConfigISOType(unsigned char type);
void delay_10ms(unsigned int _10ms);
void iccardcode();
char PcdBakValue(unsigned char sourceaddr, unsigned char goaladdr);
char PcdValue(unsigned char dd_mode,unsigned char addr,unsigned char *pValue);
/
//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)
代码语言:javascript复制在这里插入代码片
当向单片机发送控制的数据的时候要注意的规定为:
发送时格式为:数据长度 控制位(RevBuffer[0]) 数据(RevBuffer[]。。。。。。。。。。。)
发送时格式为:数据长度(RevBuffer[0]) 状态(RevBuffer[1]) 数据(RevBuffer[2]。。。)
(1)0x01时中止卡
控制位为 0x01时中止卡 格式:01 01 数据长度(0x01) RevBuffer[0](0x01)
返回的数据格式为:
(2)寻卡
//功 能:寻卡
//参数说明: 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
控制位为 0x02时寻卡 格式:02 02 52||26
寻卡失败返回:数据长度 状态 eg: 01 FE
寻卡成功返回数据的格式为:数据长度 状态 卡片类型代码 eg: 03 00 04 00
(3)防冲突 读卡的系列号 MLastSelectedSnr
/
//功 能:防冲撞
//参数说明: pSnr[OUT]:卡片序列号,4字节
//返 回: 成功返回MI_OK
/
char PcdAnticoll(unsigned char *pSnr)
status = PcdAnticoll(&RevBuffer[2]);
控制位为 0x03 时防冲撞 格式:01 03
失败返回: 01 FE
成功返回:数据长度 状态 卡片序列号
(4)
/
//功 能:选定卡片
//参数说明: pSnr[IN]:卡片序列号,4字节
//返 回: 成功返回MI_OK
/
char PcdSelect(unsigned char *pSnr)
status=PcdSelect(MLastSelectedSnr);
发送:01 04 数据长度 控制位
失败返回:01 FE
成功返回:01 00 读卡的系列号2个字节 eg:03 00 62 63
(5)
/
//功 能:验证卡片密码
//参数说明: auth_mode[IN]: 密码验证模式
// 0x60 = 验证A密钥
// 0x61 = 验证B密钥
// addr[IN]:块地址 0x00到0x40
// pKey[IN]:密码
// pSnr[IN]:卡片序列号,4字节
//返 回: 成功返回MI_OK
/
char PcdAuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr)
status = PcdAuthState(RevBuffer[1], RevBuffer[3], DefaultKey, MLastSelectedSnr);// 校验卡密码
发送格式:04 05 61 00 01 数据长度 密码验证模式 空 块地址
失败返回:01 FE
成功返回:01 00
(8)读取M1卡一块数据
/
//功 能:读取M1卡一块数据
//参数说明: addr[IN]:块地址
// pData[OUT]:读出的数据,16字节
//返 回: 成功返回MI_OK
/
char PcdRead(unsigned char addr,unsigned char *pData)
status=PcdRead(RevBuffer[1],&RevBuffer[2]);
发送格式:02 08 01 数据长度 控制位 块地址
失败返回格式:01 FE 数据长度 状态
成功返回格式:数据长度 状态 16字节数据 eg:11 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
(9)0x09
/
//功 能:写数据到M1卡一块
//参数说明: addr[IN]:块地址
// pData[IN]:写入的数据,16字节
//返 回: 成功返回MI_OK
/
char PcdWrite(unsigned char addr,unsigned char *pData)
status=PcdWrite(RevBuffer[1],&RevBuffer[2]);
发送格式:12 09 01 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10数据长度1字节 控制位1字节 块地址1字节 16字节数据
失败返回格式:01 FE 数据长度 状态
成功返回格式:01 00 数据长度 状态
(10)0X0A
/
//功 能:扣款和充值
//参数说明: dd_mode[IN]:命令字
// 0xC0 = 扣款
// 0xC1 = 充值
// addr[IN]:钱包地址
// pValue[IN]:4字节增(减)值,低位在前
//返 回: 成功返回MI_OK
/
char PcdValue(unsigned char dd_mode,unsigned char addr,unsigned char *pValue)
PcdValue(RevBuffer[1],RevBuffer[2],&RevBuffer[3]);
发送格式:07 0A C0 01 00 00 00 01 数据长度1字节 控制位1字节 扣充值命令字1字节 钱包地址1字节 增减值4字节
失败返回格式:01 FE 数据长度 失败状态
成功返回格式:01 00 数据长度 成功状态
(11)测试风鸣器ox0b
发送格式:01 OB
失败返回格式:01 FE 数据长度 失败状态
成功返回格式:01 00 数据长度 成功状态
(12)备份钱包0x0c
/
//功 能:备份钱包
//参数说明: sourceaddr[IN]:源地址
// goaladdr[IN]:目标地址
//返 回: 成功返回MI_OK
/
char PcdBakValue(unsigned char sourceaddr, unsigned char goaladdr)
PcdBakValue(RevBuffer[1], RevBuffer[2]);
发送格式:03 0C 01 02 数据长度3字节 控制位1字节 源地址1字节 目标地址1字节
失败返回格式:01 FE 数据长度 失败状态
成功返回格式:01 00 数据长度 成功状态
(13)//设置通讯波特率0x0d
发送格式:02 0d 01 数据长度3字节 控制位1字节 波特率控制位1字节
失败返回格式:01 FE 数据长度 失败状态
成功返回格式:01 00 数据长度 成功状态
汇总:
控制位为 0x01时中止卡 格式:01 01 数据长度(0x01) RevBuffer[0](0x01)
控制位为 0x02时寻卡 格式:02 02 52||26
控制位为 0x03 时防冲撞 格式:01 03
选定卡发送:01 04 数据长度 控制位
密码验证发送格式:04 05 61 00 01 数据长度 密码验证模式 空 块地址
读发送格式:02 08 01 数据长度 控制位 块地址
写发送格式:12 09 01 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10数据长度1字节 控制位1字节 块地址1字节 16字节数据
当要为钱包时要写成如下格式
12 09 04 12 34 56 78 ED CB A9 87 12 34 56 78 04 FB 04 FB
扣充值发送格式:07 0A C0 01 00 00 00 01 数据长度1字节 控制位1字节 扣充值命令字1字节 钱包地址1字节 增减值4字节
备份钱包发送格式:03 0c 01 02 数据长度3字节 控制位1字节 源地址1字节 目标地址1字节
智能门禁系统中
用08.09.0A.0B块
08块中16个字节存放学号:1505106001 对应的十六进制数:59 B6 18 51 00 00 00 00 00 00 00 00 00 00 00 00
09块用于存放姓名16个字节可以存放8个汉字
0A块保留
0B块为保留
0C为密码此处为简单直接保留原始密码
02 02 52
01 03
01 04
04 05 61 00 08
020808
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/191945.html原文链接:https://javaforall.cn