#include "spi.h"
#include "sys.h" //ϵͳ×Óº¯Êý
#include "delay.h" //ÑÓʱ×Óº¯Êý
#include "MFRC522.h"
unsigned char CT[2];//¿¨ÀàÐÍ
unsigned char SN[4]; //¿¨ºÅ
unsigned char RFID[16]; //´æ·ÅRFID
u8 KEY[6]={0xff,0xff,0xff,0xff,0xff,0xff};
void test_rf552(void);
int main(void)
{
SystemInit();
delay_init(72); //ÑÓʱ³õʼ»¯
NVIC_Configuration();
InitRc522();
delay_ms(100);
while(1)
{
//status = PcdRequest(PICC_REQALL,CT);
test_rf552();
}
}
void test_rf552(void)
{
u8 status = 0;
unsigned char s=0x11;
status = PcdRequest(PICC_REQALL,CT);//寻卡
if(0 == status)
{
status=MI_ERR;
status = PcdAnticoll(SN); /防冲撞
}
if (status==MI_OK) /防冲撞成功
{
status=MI_ERR;
status =PcdSelect(SN);//选卡
}
if(status==MI_OK)//选卡成功
{
status=MI_ERR;
status=PcdAuthState(0x60,0x09,KEY,SN);//验证
}
if(status==MI_OK)//
{
status=MI_ERR;
status=PcdRead(s,RFID);//读卡
//status=PcdWrite(s,RFID1);//写卡
}
if(status==MI_OK)//读写成功
{
status=MI_ERR;
delay_ms(100);
}
}
RCC522配置函数及其头文件
#include "sys.h"
#include "rc522.h"
#include "delay.h"
#include "string.h"
void delay_ns(u32 ns)
{
u32 i;
for(i=0;i
{
__nop();
__nop();
__nop();
}
}
u8 SPIWriteByte(u8 Byte)
{
while((SPI2->SR&0X02)==0); //
SPI2->DR=Byte; //
while((SPI2->SR&0X01)==0);
return SPI2->DR; //
}
u8 SPI2_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位
{
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI2, TxData); //发数据
retry=0;
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) //检查指定的SPI标志位
{
retry++;
if(retry>200)return 0;
}
return SPI_I2S_ReceiveData(SPI2); //返回的数据
}
void SPI2_SetSpeed(u8 SPI_BaudRatePrescaler)
{
assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));
SPI2->CR1&=0XFFC7;
SPI2->CR1|=SPI_BaudRatePrescaler; //速度设置
SPI_Cmd(SPI2,ENABLE);
}
void SPI2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA, ENABLE );//PORTBʱÖÓʹÄÜ
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2ʱÖÓʹÄÜ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_6|GPIO_Pin_7);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15¸´ÓÃÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIOB
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //PB13/14/15ÉÏÀ
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
MFRC522_CS(1);
SPI_Cmd(SPI2, ENABLE);
//SPI2_ReadWriteByte(0xff);
}
void InitRc522(void)
{
SPI2_Init();
PcdReset();
PcdAntennaOff();
delay_ms(2);
PcdAntennaOn();
M500PcdConfigISOType( 'A' );
}
void Reset_RC522(void)
{
PcdReset();
PcdAntennaOff();
delay_ms(2);
PcdAntennaOn();
}
/////////////////////////////////////////////////////////////////////
//参数说明: req_code[IN]:寻卡方式
// 0x4400 = Mifare_UltraLight
// 0x0400 = Mifare_One(S50)
// 0x0200 = Mifare_One(S70)
// 0x0800 = Mifare_Pro(X)
// 0x4403 = Mifare_DESFire
/////////////////////////////////////////////////////////////////////
char PcdRequest(u8 req_code,u8 *pTagType)
{
char status;
u8 unLen;
u8 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(u8 *pSnr)
{
char status;
u8 i,snr_check=0;
u8 unLen;
u8 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(u8 *pSnr)
{
char status;
u8 i;
u8 unLen;
u8 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字节
/////////////////////////////////////////////////////////////////////
char PcdAuthState(u8 auth_mode,u8 addr,u8 *pKey,u8 *pSnr)
{
char status;
u8 unLen;
u8 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;
}
/////////////////////////////////////////////////////////////////////
//读卡
/////////////////////////////////////////////////////////////////////
char PcdRead(u8 addr,u8 *p )
{
char status;
u8 unLen;
u8 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(p , ucComMF522Buf, 16); }
{
for (i=0; i<16; i++)
{ *(p +i) = ucComMF522Buf[i]; }
}
else
{ status = MI_ERR; }
return status;
}
/////////////////////////////////////////////////////////////////////
//写卡addr[IN]:地址
//p [IN]:数据
/////////////////////////////////////////////////////////////////////
char PcdWrite(u8 addr,u8 *p )
{
char status;
u8 unLen;
u8 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, p , 16);
for (i=0; i<16; i++)
{
ucComMF522Buf[i] = *(p +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)
{
u8 status;
u8 unLen;
u8 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;
}
/////////////////////////////////////////////////////////////////////
//计算函数
/////////////////////////////////////////////////////////////////////
void CalulateCRC(u8 *pIn ,u8 len,u8 *pOut )
{
u8 i,n;
ClearBitMask(DivIrqReg,0x04);
WriteRawRC(CommandReg,PCD_IDLE);
SetBitMask(FIFOLevelReg,0x80);
for (i=0; i
{ WriteRawRC(FIFODataReg, *(pIn +i)); }
WriteRawRC(CommandReg, PCD_CALCCRC);
i = 0xFF;
do
{
n = ReadRawRC(DivIrqReg);
i--;
}
while ((i!=0) && !(n&0x04));
pOut [0] = ReadRawRC(CRCResultRegL);
pOut [1] = ReadRawRC(CRCResultRegM);
}
/////////////////////////////////////////////////////////////////////
//复位
/////////////////////////////////////////////////////////////////////
char PcdReset(void)
{
//PORTD|=(1<
MFRC522_Rst(1);
delay_ns(10);
//PORTD&=~(1<
MFRC522_Rst(0);
delay_ns(10);
//PORTD|=(1<
MFRC522_Rst(1);
delay_ns(10);
WriteRawRC(CommandReg,PCD_RESETPHASE);
WriteRawRC(CommandReg,PCD_RESETPHASE);
delay_ns(10);
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(u8 type)
{
if (type == 'A') //ISO14443_A
{
ClearBitMask(Status2Reg,0x08);
WriteRawRC(ModeReg,0x3D);//3F
WriteRawRC(RxSelReg,0x86);//84
WriteRawRC(RFCfgReg,0x7F); //4F
WriteRawRC(TReloadRegL,30);//tmoLength);// TReloadVal = 'h6a =tmoLength(dec)
WriteRawRC(TReloadRegH,0);
WriteRawRC(TModeReg,0x8D);
WriteRawRC(TPrescalerReg,0x3E);
delay_ns(1000);
PcdAntennaOn();
}
else{ return 1; }
return MI_OK;
}
/////////////////////////////////////////////////////////////////////
//读寄存器地址 Address[IN]
//返回读出的值
/////////////////////////////////////////////////////////////////////
u8 ReadRawRC(u8 Address)
{
u8 ucAddr;
u8 ucResult=0;
MFRC522_CS(0);
ucAddr = ((Address<<1)&0x7E)|0x80;
SPIWriteByte(ucAddr);
ucResult=SPIReadByte();
MFRC522_CS(1);
return ucResult;
}
/////////////////////////////////////////////////////////////////////
//写寄存器地址 Address[IN]//返回写的值
/////////////////////////////////////////////////////////////////////
void WriteRawRC(u8 Address, u8 value)
{
u8 ucAddr;
// u8 tmp;
MFRC522_CS(0);
ucAddr = ((Address<<1)&0x7E);
SPIWriteByte(ucAddr);
SPIWriteByte(value);
MFRC522_CS(1);
// tmp=ReadRawRC(Address);
//
// if(value!=tmp)
// printf("wrong\n");
}
/////////////////////////////////////////////////////////////////////
//置寄存器位
/////////////////////////////////////////////////////////////////////
void SetBitMask(u8 reg,u8 mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg,tmp | mask); // set bit mask
}
/////////////////////////////////////////////////////////////////////
//¹¦ ÄÜ£ºÇåRC522¼Ä´æÆ÷λ
//²ÎÊý˵Ã÷£ºreg[IN]:¼Ä´æÆ÷µØÖ·
// mask[IN]:Çåλֵ
/////////////////////////////////////////////////////////////////////
void ClearBitMask(u8 reg,u8 mask)
{
char tmp = 0x0;
tmp = ReadRawRC(reg);
WriteRawRC(reg, tmp & ~mask); // clear bit mask
}
/////////////////////////////////////////////////////////////////////
//RCC和卡通信
//Command[IN]命令
// pIn [IN]数据
//InLenByte[IN]长度
//pOut [OUT]接收到卡返回的数据
//*pOutLenBit[OUT]返回的长度
/////////////////////////////////////////////////////////////////////
char PcdComMF522(u8 Command,
u8 *pIn ,
u8 InLenByte,
u8 *pOut ,
u8 *pOutLenBit)
{
char status = MI_ERR;
u8 irqEn = 0x00;
u8 waitFor = 0x00;
u8 lastBits;
u8 n;
u16 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
{ WriteRawRC(FIFODataReg, pIn [i]); }
WriteRawRC(CommandReg, Command);
// n = ReadRawRC(CommandReg);
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
{ pOut [i] = ReadRawRC(FIFODataReg); }
}
}
else
{ status = MI_ERR; }
}
SetBitMask(ControlReg,0x80); // stop timer now
WriteRawRC(CommandReg,PCD_IDLE);
return status;
}
/////////////////////////////////////////////////////////////////////
//启动天线
//1m的间隔
/////////////////////////////////////////////////////////////////////
void PcdAntennaOn(void)
{
u8 i;
i = ReadRawRC(TxControlReg);
if (!(i & 0x03))
{
SetBitMask(TxControlReg, 0x03);
}
}
/////////////////////////////////////////////////////////////////////
//关闭天线
/////////////////////////////////////////////////////////////////////
void PcdAntennaOff(void)
{
ClearBitMask(TxControlReg, 0x03);
}
头文件:
/////////////////////////////////////////////////////////////////////
//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 0
#define MI_NOTAGERR (1)
#define MI_ERR (2)
#define SHAQU1 0X01
#define KUAI4 0X04
#define KUAI7 0X07
#define REGCARD 0xa1
#define CONSUME 0xa2
#define READCARD 0xa3
#define ADDMONEY 0xa4
//
//#define spi_cs 1;
//sbit spi_ck=P0^6;
//sbit spi_mosi=P0^7;
//sbit spi_miso=P4^1;
//sbit spi_rst=P2^7;
#define SPIReadByte() SPIWriteByte(0)
u8 SPIWriteByte(u8 byte);
void SPI2_Init(void);
#define MFRC522_CS(x) x ? GPIO_SetBits(GPIOA,GPIO_Pin_6):GPIO_ResetBits(GPIOA,GPIO_Pin_6)
#define MFRC522_Rst(x) x ? GPIO_SetBits(GPIOA,GPIO_Pin_7):GPIO_ResetBits(GPIOA,GPIO_Pin_7)
void InitRc522(void);
void ClearBitMask(u8 reg,u8 mask);
void WriteRawRC(u8 Address, u8 value);
void SetBitMask(u8 reg,u8 mask);
char PcdComMF522(u8 Command,
u8 *pIn ,
u8 InLenByte,
u8 *pOut ,
u8 *pOutLenBit);
void CalulateCRC(u8 *pIn ,u8 len,u8 *pOut );
u8 ReadRawRC(u8 Address);
void PcdAntennaOn(void);
char PcdReset(void);
char PcdRequest(unsigned char req_code,unsigned char *pTagType);
void PcdAntennaOn(void);
void PcdAntennaOff(void);
char M500PcdConfigISOType(unsigned char type);
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 PcdWrite(unsigned char addr,unsigned char *pData);
char PcdRead(unsigned char addr,unsigned char *pData);
char PcdHalt(void);
void Reset_RC522(void);
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』