rc522.c
如下:
#include <string.h>
#include "sys.h"
#include "delay.h"
#include "rc522.h"
#define MF522_RST PCout(0)
#define MF522_SI PCout(1)
#define MF522_SCK PCout(2)
#define MF522_NSS PCout(3)
#define MF522_SO PCin(4)
void rc522_init ( void ) { /* rc522模块的初始化 */
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOC, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /* 推挽输出 */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOC, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init ( GPIOC, &GPIO_InitStructure );
}
/*--------------------------------------------------------------------------------------
* 功能:寻卡,成功则返回MI_OK
* 参数:req_code:寻卡方式,0x52为寻感应区内所有符合14443A标准的卡,0x26为寻未进入休眠状态的卡
* pTagType:卡片类型代码。0x4400为Mifare_UltraLight,0x0400为Mifare_One(S50),
* 0x0200为Mifare_One(S70),0x0800为Mifare_Pro(X),0x4403为Mifare_DESFire
*-------------------------------------=-----------------------------------------------*/
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;
}
char PcdAnticoll ( unsigned char *pSnr ) { /* 防冲突。参数pSnr是卡片序列号(4字节) */
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;
}
char PcdSelect ( unsigned char *pSnr ) { /* 选定卡片。参数pSnr是卡片序列号(4字节) */
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;
}
/*-----------------------------------------------------------------------------------------------------------
* 功能:验证卡片密码。成功则返回MI_OK
* 参数:auth_mode:密码验证模式,0x60为验证A密钥,0x61为验证B密钥;addr:块地址;pKey:密码;pSnr:卡片序列号(4字节)
* ---------------------------------------------------------------------------------------------------------*/
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 );
}
status = PcdComMF522 ( PCD_AUTHENT, ucComMF522Buf, 12, ucComMF522Buf, &unLen );
if ( ( status != MI_OK ) || ( ! ( ReadRawRC ( Status2Reg ) & 0x08 ) ) ) {
status = MI_ERR;
}
return status;
}
char PcdRead ( unsigned char addr, unsigned char *pData ) { /* 读取M1卡数据。参数addr是块地址,pData是读出的数据(16字节) */
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 ) ) {
for ( i = 0; i < 16; i++ ) {
* ( pData + i ) = ucComMF522Buf[i];
}
} else {
status = MI_ERR;
}
return status;
}
char PcdWrite ( unsigned char addr, unsigned char *pData ) { /* 写数据到M1卡。参数addr是块地址,pData是写入的数据(16字节) */
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 ) {
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;
}
char PcdHalt ( void ) { /* 命令卡片进入休眠状态,成功则返回MI_OK */
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 status;
}
void CalulateCRC ( unsigned char *pIndata, unsigned char len, unsigned char *pOutData ) { /* 用MF522计算CRC16函数 */
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 );
}
char PcdReset ( void ) { /* 复位RC522,成功则返回MI_OK */
MF522_RST = 1;
delay_us ( 50 );
MF522_RST = 0;
delay_us ( 50 );
MF522_RST = 1;
delay_us ( 50 );
WriteRawRC ( CommandReg, PCD_RESETPHASE );
delay_us ( 50 );
WriteRawRC ( ModeReg, 0x3D ); /* 和Mifare卡通讯,CRC初始值为0x6363 */
WriteRawRC ( TReloadRegL, 30 );
WriteRawRC ( TReloadRegH, 0 );
WriteRawRC ( TModeReg, 0x8D );
WriteRawRC ( TPrescalerReg, 0x3E );
WriteRawRC ( TxAutoReg, 0x40 );
return MI_OK;
}
char M500PcdConfigISOType ( unsigned char type ) { /* 设置RC632的工作方式 */
if ( type == 'A' ) { /* ISO14443_A */
ClearBitMask ( Status2Reg, 0x08 );
WriteRawRC ( ModeReg, 0x3D ); /* 3F */
WriteRawRC ( RxSelReg, 0x86 ); /* 84 */
WriteRawRC ( RFCfgReg, 0x7F ); /* 4F */
WriteRawRC ( TReloadRegL, 30 );
WriteRawRC ( TReloadRegH, 0 );
WriteRawRC ( TModeReg, 0x8D );
WriteRawRC ( TPrescalerReg, 0x3E );
delay_ms ( 10 );
PcdAntennaOn();
} else {
return MI_ERR;
}
return MI_OK;
}
unsigned char ReadRawRC ( unsigned char Address ) { /* 读RC632寄存器。参数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 |= MF522_SO;
MF522_SCK = 0;
}
MF522_NSS = 1;
MF522_SCK = 1;
return ucResult;
}
void WriteRawRC ( unsigned char Address, unsigned char value ) { /* 写RC632寄存器。参数Address是寄存器地址,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;
}
void SetBitMask ( unsigned char reg, unsigned char mask ) { /* 置RC522寄存器位。参数reg是寄存器地址,mask是置位值 */
char tmp = 0x0;
tmp = ReadRawRC ( reg );
WriteRawRC ( reg, tmp | mask ); /* set bit mask */
}
void ClearBitMask ( unsigned char reg, unsigned char mask ) { /* 清RC522寄存器位。参数reg是寄存器地址,mask是清位值 */
char tmp = 0x0;
tmp = ReadRawRC ( reg );
WriteRawRC ( reg, tmp & ~mask ); /* clear bit mask */
}
/*----------------------------------------------------------------------------------------
* 功能:通过RC522和ISO14443卡通讯
* 参数:Command是RC522命令字,pInData是通过RC522发送到卡片的数据,InLenByte是发送数据的字节长度,
pOutData是接收到的卡片返回数据,pOutLenBit是返回数据的位长度
-----------------------------------------------------------------------------------------*/
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 ); /* fifo清零 */
for ( i = 0; i < InLenByte; i++ ) {
WriteRawRC ( FIFODataReg, pInData[i] );
}
WriteRawRC ( CommandReg, Command );
if ( Command == PCD_TRANSCEIVE ) {
SetBitMask ( BitFramingReg, 0x80 );
}
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;
}
void PcdAntennaOn() { /* 开启天线,每次启动或关闭天险发射之间应至少有1ms的间隔 */
unsigned char i;
i = ReadRawRC ( TxControlReg );
if ( ! ( i & 0x03 ) ) {
SetBitMask ( TxControlReg, 0x03 );
}
}
void PcdAntennaOff() { /* 关闭天线 */
ClearBitMask ( TxControlReg, 0x03 );
}
/* 扣款和充值。参数dd_mode是命令字,0xC0为扣款,0xC1为充值;addr是钱包地址;pValue是数值,4字节增(减)值,低位在前 */
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 );
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;
}
char PcdBakValue ( unsigned char sourceaddr, unsigned char goaladdr ) { /* 备份钱包。参数sourceaddr是源地址,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;
}
rc522.h
如下:
#ifndef MFC522_H
#define MFC522_H
#define MAXRLEN 18
void rc522_init ( void );
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 );
/* 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 /* 休眠 */
#define DEF_FIFO_LENGTH 64 /* MF522的FIFO长度定义 */
/* 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
#endif
main.c
如下:
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "string.h"
#include "rc522.h"
unsigned char RevBuffer[30] = {0}; /* 接收缓冲区 */
unsigned char DefaultKey[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; /* 密码 */
unsigned char MLastSelectedSnr[4] = {0};
int main ( void ) {
SystemInit();
delay_init ( 72 );
NVIC_Configuration();
uart_init ( 9600 );
rc522_init();
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
M500PcdConfigISOType ( 'A' );
while ( 1 ) {
if ( PcdRequest ( PICC_REQALL, &RevBuffer[2] ) == MI_OK ) { /* 寻卡 */
if ( PcdAnticoll ( MLastSelectedSnr ) == MI_OK ) { /* 防冲突 */
if ( PcdSelect ( MLastSelectedSnr ) == MI_OK ) { /* 选择卡号 */
printf ( "I get a card! " );
if ( PcdAuthState ( PICC_AUTHENT1A, 63, DefaultKey, MLastSelectedSnr ) == MI_OK ) { /* 校验卡密码 */
printf ( "ok\r\n" );
memset ( RevBuffer, 0, 16 );
}
}
}
}
delay_ms ( 50 );
}
}