RFID射频模块(MFRC522 STM32)
目录
一、介绍
二、传感器原理
1.原理图
2.引脚描述
3.工作原理介绍
三、程序设计
main.c文件
MFRC522.h文件
MFRC522.c文件
四、实验效果
五、资料获取
项目分享
一、介绍
RC522 RFID射频模块是一款广泛应用于非接触式RFID系统中的核心组件,由NXP(前身为Philips半导体)公司设计生产。这个模块基于MFRC522芯片,该芯片是一个高度集成的UHF RFID读卡器/写卡器解决方案,能够支持ISO 14443A标准的卡片和标签。
以下是传感器的参数:
型号 | MFRC-522 |
工作电压 | 3.3V |
工作电流 | 13~26mA |
工作频率 | 13.56MHz |
工作温度 | 20~80℃ |
存储温度 | -40~85℃ |
相对湿度 | 5%~95% |
哔哩哔哩视频:
RFID射频模块(MFRC522 STM32)
(资料分享见文末)
二、传感器原理
1.原理图
2.引脚描述
引脚名称 | 描述 |
VCC | 供给电压DC 3.3V |
GND | 地线 |
RST | 复位 |
MISO | SPI口从机输出 |
MOSI | SPI口从机输入 |
SCK | SPI口时钟 |
NSS | SPI口片选 |
3.工作原理介绍
MFRC522 是高度集成的非接触式(13.56MHz)读写卡芯片。此发送模块利用调制和解调的原理,并将它们完全集成到各种非接触式通信方法和协议中(13.56MHz)。
三、程序设计
使用STM32F103C8T6读取RFID射频模块采集的IC卡数据,通过串口发送至电脑
SDA | PA4 |
SCK | PA5 |
MISO | PA6 |
MOSI | PA7 |
RST | PB0 |
串口 | 串口1 |
main.c文件
#include "stm32f10x.h"
#include "led.h"
#include "usart.h"
#include "delay.h"
#include "oled.h"
#include "MFRC522.h"
/*****************辰哥单片机设计******************
STM32
* 项目 : MFRC522射频模块实验
* 版本 : V1.0
* 日期 : 2024.9.2
* MCU : STM32F103C8T6
* 接口 : 参看MFRC522.h
* BILIBILI : 辰哥单片机设计
* CSDN : 辰哥单片机设计
* 作者 : 辰哥
**********************BEGIN***********************/
unsigned char buf[20]; //存放卡序号
int main(void)
{
unsigned char status; //返回符
unsigned int temp,i;
SystemInit();//配置系统时钟为72M
delay_init(72);
LED_Init();
LED_On();
USART1_Config();
MFRC522_Init();
printf ( "MFRC522 Test\r\n" );
delay_ms(1000);
while (1)
{
status = MFRC522_Request(PICC_REQALL, buf);//寻卡
if (status != MI_OK)
{
MFRC522_Reset();
MFRC522_AntennaOff();
MFRC522_AntennaOn();
continue;
}
printf("卡的类型:");
for(i=0;i<2;i++)
{
temp=buf[i];
printf("%X",temp);
}
status = MFRC522_Anticoll(buf);//防冲突
if (status != MI_OK)
{
continue;
}
以下为超级终端打印出的内容
printf("卡序列号:"); //超级终端显示,
for(i=0;i<4;i++)
{
temp=buf[i];
printf("%X",temp);
}
printf("\r\n");
}
}
MFRC522.h文件
#ifndef __MFRC522_H
#define __MFRC522_H
#include "stm32f10x.h"
#include "delay.h"
#include "math.h"
/*****************辰哥单片机设计******************
STM32
* 文件 : MFRC522射频模块h文件
* 版本 : V1.0
* 日期 : 2024.9.2
* MCU : STM32F103C8T6
* 接口 : 见代码
* BILIBILI : 辰哥单片机设计
* CSDN : 辰哥单片机设计
* 作者 : 辰哥
**********************BEGIN***********************/
/***************根据自己需求更改****************/
// MFRC522射频模块 GPIO宏定义
/*********************************** RC522 引脚定义 *********************************************/
#define MFRC522_GPIO_SDA_PORT GPIOA
#define MFRC522_GPIO_SDA_PIN GPIO_Pin_4
#define MFRC522_GPIO_SCK_PORT GPIOA
#define MFRC522_GPIO_SCK_PIN GPIO_Pin_5
#define MFRC522_GPIO_MOSI_PORT GPIOA
#define MFRC522_GPIO_MOSI_PIN GPIO_Pin_7
#define MFRC522_GPIO_MISO_PORT GPIOA
#define MFRC522_GPIO_MISO_PIN GPIO_Pin_6
#define MFRC522_GPIO_RST_PORT GPIOB
#define MFRC522_GPIO_RST_PIN GPIO_Pin_0
/*********************END**********************/
#define MFRC522_SDA_L GPIO_ResetBits ( MFRC522_GPIO_SDA_PORT, MFRC522_GPIO_SDA_PIN )
#define MFRC522_SDA_H GPIO_SetBits ( MFRC522_GPIO_SDA_PORT, MFRC522_GPIO_SDA_PIN )
#define MFRC522_RST_L GPIO_ResetBits( MFRC522_GPIO_RST_PORT, MFRC522_GPIO_RST_PIN )
#define MFRC522_RST_H GPIO_SetBits ( MFRC522_GPIO_RST_PORT, MFRC522_GPIO_RST_PIN )
#define MFRC522_SCK_L GPIO_ResetBits( MFRC522_GPIO_SCK_PORT, MFRC522_GPIO_SCK_PIN )
#define MFRC522_SCK_H GPIO_SetBits ( MFRC522_GPIO_SCK_PORT, MFRC522_GPIO_SCK_PIN )
#define MFRC522_MOSI_L GPIO_ResetBits( MFRC522_GPIO_MOSI_PORT, MFRC522_GPIO_MOSI_PIN )
#define MFRC522_MOSI_H GPIO_SetBits ( MFRC522_GPIO_MOSI_PORT, MFRC522_GPIO_MOSI_PIN )
#define MFRC522_MISO_READ GPIO_ReadInputDataBit ( MFRC522_GPIO_MISO_PORT, MFRC522_GPIO_MISO_PIN )
void MFRC522_Init(void);
/
//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
#define MAXRLEN 18
/
//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 0x26
#define MI_NOTAGERR 0xcc
#define MI_ERR 0xbb
/
//和MF522通讯时返回的错误代码
/
#define macDummy_Data 0x00
char MFRC522_Reset(void);
void Write_MFRC522(unsigned char Address, unsigned char value);
unsigned char Read_MFRC522(unsigned char Address);
void MFRC522_AntennaOn(void);
void MFRC522_AntennaOff(void);
char MFRC522_Request(unsigned char req_code,unsigned char *pTagType);
char MFRC522_Anticoll(unsigned char *pSnr);
char MFRC522_SelectTag(unsigned char *pSnr);
char MFRC522_AuthState(unsigned char auth_mode,unsigned char addr,unsigned char *pKey,unsigned char *pSnr);
char MFRC522_Read(unsigned char addr,unsigned char *pData);
char MFRC522_Halt(void);
char MFRC522_Write(unsigned char addr,unsigned char *pData);
#endif /* __MFRC522__H */
MFRC522.c文件
#include "MFRC522.h"
/*****************辰哥单片机设计******************
STM32
* 文件 : MFRC522射频模块c文件
* 版本 : V1.0
* 日期 : 2024.9.2
* MCU : STM32F103C8T6
* 接口 : 见代码
* BILIBILI : 辰哥单片机设计
* CSDN : 辰哥单片机设计
* 作者 : 辰哥
**********************BEGIN***********************/
#define MAXRLEN 18
#define RC522_DELAY() delay_us(2)
void MFRC522_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
/* 配置 SPI_RC522_SPI 引脚:SDA */
GPIO_InitStructure.GPIO_Pin = MFRC522_GPIO_SDA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(MFRC522_GPIO_SDA_PORT, &GPIO_InitStructure);
/* 配置 SPI_RC522_SPI 引脚:SCK */
GPIO_InitStructure.GPIO_Pin = MFRC522_GPIO_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(MFRC522_GPIO_SCK_PORT, &GPIO_InitStructure);
/* 配置 SPI_RC522_SPI 引脚:MOSI */
GPIO_InitStructure.GPIO_Pin = MFRC522_GPIO_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(MFRC522_GPIO_MOSI_PORT, &GPIO_InitStructure);
/* 配置 SPI_RC522_SPI 引脚:MISO */
GPIO_InitStructure.GPIO_Pin = MFRC522_GPIO_MISO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(MFRC522_GPIO_MISO_PORT, &GPIO_InitStructure);
/* 配置 SPI_RC522_SPI 引脚:RST */
GPIO_InitStructure.GPIO_Pin = MFRC522_GPIO_RST_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(MFRC522_GPIO_RST_PORT, &GPIO_InitStructure);
}
/
//功能描述从MFRC522的某一寄存器读一个字节数据
//参数说明:Address[IN]:寄存器地址
//返 回:读出的值
/
unsigned char Read_MFRC522(unsigned char Address)
{
unsigned char i, ucAddr;
unsigned char ucResult=0;
MFRC522_SCK_L;
RC522_DELAY();
MFRC522_SDA_L;
RC522_DELAY();
ucAddr = ((Address<<1)&0x7E)|0x80;
for(i=8;i>0;i--)
{
if(ucAddr&0x80)
{
MFRC522_MOSI_H;
}
else
{
MFRC522_MOSI_L;
}
RC522_DELAY();
MFRC522_SCK_H;
RC522_DELAY();
ucAddr <<= 1;
MFRC522_SCK_L;
RC522_DELAY();
}
for(i=8;i>0;i--)
{
MFRC522_SCK_H;
RC522_DELAY();
ucResult <<= 1;
ucResult|= MFRC522_MISO_READ;
MFRC522_SCK_L;
RC522_DELAY();
}
MFRC522_SCK_H;
RC522_DELAY();
MFRC522_SDA_H;
RC522_DELAY();
return ucResult;
}
/
//功能描述向MFRC522的某一寄存器写一个字节数据
//参数说明:Address[IN]:寄存器地址
// value[IN]:写入的值
/
void Write_MFRC522(unsigned char Address, unsigned char value)
{
unsigned char i, ucAddr;
MFRC522_SCK_L;
MFRC522_SDA_L;
ucAddr = ((Address<<1)&0x7E);
for(i=8;i>0;i--)
{
if(ucAddr&0x80)
{
MFRC522_MOSI_H;
}
else
{
MFRC522_MOSI_L;
}
RC522_DELAY();
MFRC522_SCK_H;
RC522_DELAY();
ucAddr <<= 1;
MFRC522_SCK_L;
RC522_DELAY();
}
for(i=8;i>0;i--)
{
if(value&0x80)
{
MFRC522_MOSI_H;
}
else
{
MFRC522_MOSI_L;
}
RC522_DELAY();
MFRC522_SCK_H;
RC522_DELAY();
value <<= 1;
MFRC522_SCK_L;
RC522_DELAY();
}
MFRC522_SCK_H;
RC522_DELAY();
MFRC522_SDA_H;
RC522_DELAY();
}
/
//功 能:复位RC522
//返 回: 成功返回MI_OK
/
char MFRC522_Reset(void)
{
//unsigned char i;
MFRC522_RST_H;
delay_us (1);
MFRC522_RST_L;
delay_us (1);
MFRC522_RST_H;
delay_us (1);
//MFRC522_RST_H;
Write_MFRC522(CommandReg,0x0F); //soft reset
while(Read_MFRC522(CommandReg) & 0x10); //wait chip start ok
delay_us (1);
Write_MFRC522(ModeReg,0x3D); //和Mifare卡通讯,CRC初始值0x6363
Write_MFRC522(TReloadRegL,30);
Write_MFRC522(TReloadRegH,0);
Write_MFRC522(TModeReg,0x8D);
Write_MFRC522(TPrescalerReg,0x3E);
Write_MFRC522(TxAutoReg,0x40);
return MI_OK;
}
/
//功 能:置RC522寄存器位
//参数说明:reg[IN]:寄存器地址
// mask[IN]:置位值
/
void SetBitMask(unsigned char reg,unsigned char mask)
{
char tmp = 0x0;
tmp = Read_MFRC522(reg);
Write_MFRC522(reg,tmp | mask); // set bit mask
}
/
//功 能:清RC522寄存器位
//参数说明:reg[IN]:寄存器地址
// mask[IN]:清位值
/
void ClearBitMask(unsigned char reg,unsigned char mask)
{
char tmp = 0x0;
tmp = Read_MFRC522(reg);
Write_MFRC522(reg, tmp & ~mask); // clear bit mask
}
/
//功 能:通过RC522和ISO14443卡通讯
//参数说明:Command[IN]:RC522命令字
// pInData[IN]:通过RC522发送到卡片的数据
// InLenByte[IN]:发送数据的字节长度
// pOutData[OUT]:接收到的卡片返回数据
// *pOutLenBit[OUT]:返回数据的位长度
/
char MFRC522_ToCard(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;
}
Write_MFRC522(ComIEnReg,irqEn|0x80); //PCD_TRANSCEIVE模式 无命令改变
ClearBitMask(ComIrqReg,0x80); //IRQ开漏输出
Write_MFRC522(CommandReg,PCD_IDLE); //取消当前命令
SetBitMask(FIFOLevelReg,0x80); //清除FIFO Flash 内ErrReg BufferOvfl标志
for (i=0; i<InLenByte; i++)
{
Write_MFRC522(FIFODataReg, pInData[i]); //把数据存到FIFO
}
Write_MFRC522(CommandReg, Command); //发送FIFO内容
if (Command == PCD_TRANSCEIVE)
{
SetBitMask(BitFramingReg,0x80); //立即停止定时器
}
n = Read_MFRC522(ComIrqReg);
i = 1500;//根据时钟频率调整,操作M1卡最大等待时间25ms
do
{
n = Read_MFRC522(ComIrqReg);
i--;
}
while ((i!=0) && !(n&0x01) && !(n&waitFor));
ClearBitMask(BitFramingReg,0x80);
if (i!=0)
{
if(!(Read_MFRC522(ErrorReg)&0x1B))
{
status = MI_OK;
if (n & irqEn & 0x01)
{ status = MI_NOTAGERR; }
if (Command == PCD_TRANSCEIVE)
{
n = Read_MFRC522(FIFOLevelReg);
lastBits = Read_MFRC522(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] = Read_MFRC522(FIFODataReg); }
}
}
else
{
status = MI_ERR;
}
}
SetBitMask(ControlReg,0x80); // stop timer now
Write_MFRC522(CommandReg,PCD_IDLE);
return status;
}
//开启天线
//每次启动或关闭天线发射之间应至少有1ms的间隔
void MFRC522_AntennaOn(void)
{
unsigned char i;
i = Read_MFRC522(TxControlReg);
if (!(i & 0x03))
{
SetBitMask(TxControlReg, 0x03);
}
}
//关闭天线
void MFRC522_AntennaOff(void)
{
ClearBitMask(TxControlReg, 0x03);
}
//功能描述用MF522计算CRC
//输入参数pIndata--要读数CRC的数据len--数据长度pOutData--计算的CRC结果
void CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData)
{
unsigned char i,n;
ClearBitMask(DivIrqReg,0x04);
Write_MFRC522(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for (i=0; i<len; i++)
{ Write_MFRC522(FIFODataReg, *(pIndata+i)); }
Write_MFRC522(CommandReg, PCD_CALCCRC);
i = 0xFF;
do
{
n = Read_MFRC522(DivIrqReg);
i--;
}
while ((i!=0) && !(n&0x04));
pOutData[0] = Read_MFRC522(CRCResultRegL);
pOutData[1] = Read_MFRC522(CRCResultRegM);
}
/
//功 能:命令卡片进入休眠状态
//返 回: 成功返回MI_OK
/
char MFRC522_Halt(void)
{
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
char status;
ucComMF522Buf[0] = PICC_HALT;
ucComMF522Buf[1] = 0;
CalulateCRC(ucComMF522Buf,2,&ucComMF522Buf[2]);
status = MFRC522_ToCard(PCD_TRANSCEIVE,ucComMF522Buf,4,ucComMF522Buf,&unLen);
return MI_OK;
}
/
//功 能:寻卡
//参数说明: 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 MFRC522_Request(unsigned char req_code,unsigned char *pTagType)
{
char status;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask(Status2Reg,0x08);
Write_MFRC522(BitFramingReg,0x07);
SetBitMask(TxControlReg,0x03);
ucComMF522Buf[0] = req_code;
status = MFRC522_ToCard(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 MFRC522_Anticoll(unsigned char *pSnr)
{
char status;
unsigned char i,snr_check=0;
unsigned int unLen;
unsigned char ucComMF522Buf[MAXRLEN];
ClearBitMask(Status2Reg,0x08);
Write_MFRC522(BitFramingReg,0x00);
ClearBitMask(CollReg,0x80);
ucComMF522Buf[0] = PICC_ANTICOLL1;
ucComMF522Buf[1] = 0x20;
status = MFRC522_ToCard(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 MFRC522_SelectTag(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 = MFRC522_ToCard(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 MFRC522_AuthState(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 = MFRC522_ToCard(PCD_AUTHENT,ucComMF522Buf,12,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (!(Read_MFRC522(Status2Reg) & 0x08)))
{ status = MI_ERR; }
return status;
}
/
//功 能:读取M1卡一块数据
//参数说明: addr[IN]:块地址
// pData[OUT]:读出的数据,16字节
//返 回: 成功返回MI_OK
/
char MFRC522_Read(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 = MFRC522_ToCard(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 MFRC522_Write(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 = MFRC522_ToCard(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 = MFRC522_ToCard(PCD_TRANSCEIVE,ucComMF522Buf,18,ucComMF522Buf,&unLen);
if ((status != MI_OK) || (unLen != 4) || ((ucComMF522Buf[0] & 0x0F) != 0x0A))
{ status = MI_ERR; }
}
return status;
}