const int chipSelectPin = 10;
//mega2560 use 53 port , uno use 10 port.
const int NRSTPD = 5;
int i=0;
//MF522 command bits
#define PCD_IDLE 0x00 //NO action; cancel current commands
#define PCD_AUTHENT 0x0E //verify password key
#define PCD_RECEIVE 0x08 //receive data
#define PCD_TRANSMIT 0x04 //send data
#define PCD_TRANSCEIVE 0x0C //send and receive data
#define PCD_RESETPHASE 0x0F //reset
#define PCD_CALCCRC 0x03 //CRC check and caculation
//Mifare_One card command bits
#define PICC_REQIDL 0x26 //Search the cards that not into sleep mode in the antenna area
#define PICC_REQALL 0x52 //Search all the cards in the antenna area
#define PICC_ANTICOLL 0x93 //prevent conflict
#define PICC_SElECTTAG 0x93 //select card
#define PICC_AUTHENT1A 0x60 //verify A password key
#define PICC_AUTHENT1B 0x61 //verify B password key
#define PICC_READ 0x30 //read
#define PICC_WRITE 0xA0 //write
#define PICC_DECREMENT 0xC0 //deduct value
#define PICC_INCREMENT 0xC1 //charge up value
#define PICC_RESTORE 0xC2 //Restore data into buffer
#define PICC_TRANSFER 0xB0 //Save data into buffer
#define PICC_HALT 0x50 //sleep mode
//THe mistake code that return when communicate with MF522
#define MI_OK 0
#define MI_NOTAGERR 1
#define MI_ERR 2
//4 bytes Serial number of card, the 5 bytes is verfiy bytes
uchar serNum[5];
void setup()
{
Serial.begin(9600);
SPI.begin();
pinMode(chipSelectPin,OUTPUT); // Set digital pin 10 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(chipSelectPin, LOW); // Activate the RFID reader
pinMode(NRSTPD,OUTPUT); // Set digital pin 5 , Not Reset and Power-down
MFRC522_Init();
}
void loop()
{
uchar status;
uchar str[MAX_LEN];
// Search card, return card types
status = MFRC522_Request(PICC_REQIDL, str);
if (status != MI_OK)
{
return;
}
// Show card type
ShowCardType(str);
//Prevent conflict, return the 4 bytes Serial number of the card
status = MFRC522_Anticoll(str);
// str[0..3]: serial number of the card
// str[4]: XOR checksum of the SN.
if (status == MI_OK)
{
Serial.print("The card's number is: ");
memcpy(serNum, str, 5);
ShowCardID(serNum);
// Check people associated with card ID
uchar* id = serNum;
/*
* Function:Read_MFRC522
* Description:read a byte data into one register of MR RC522
* Input parameter:addr--register address
* Return:return the read value
*/
uchar Read_MFRC522(uchar addr)
{
uchar val;
digitalWrite(chipSelectPin, LOW);
//address format:1XXXXXX0
SPI.transfer(((addr<<1)&0x7E) | 0x80);
val =SPI.transfer(0x00);
/*
* Function:AntennaOn
* Description:Turn on antenna, every time turn on or shut down antenna need at least 1ms delay
* Input parameter:null
* Return:null
*/
void AntennaOn(void)
{
uchar temp;
/*
* Function:AntennaOff
* Description:Turn off antenna, every time turn on or shut down antenna need at least 1ms delay
* Input parameter:null
* Return:null
*/
void AntennaOff(void)
{
ClearBitMask(TxControlReg, 0x03);
}
TagType[0] = reqMode;
status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits);
if ((status != MI_OK) || (backBits != 0x10))
{
status = MI_ERR;
}
return status;
}
/*
* Function:MFRC522_ToCard
* Description:communicate between RC522 and ISO14443
* Input parameter:command--MF522 command bits
* sendData--send data to card via rc522
* sendLen--send data length
* backData--the return data from card
* backLen--the length of return data
* return:return MI_OK if successed
*/
uchar MFRC522_ToCard(uchar command, uchar *sendData, uchar sendLen, uchar *backData, uint *backLen)
{
uchar status = MI_ERR;
uchar irqEn = 0x00;
uchar waitIRq = 0x00;
uchar lastBits;
uchar n;
uint i;
switch (command)
{
case PCD_AUTHENT: //verify card password
{
irqEn = 0x12;
waitIRq = 0x10;
break;
}
case PCD_TRANSCEIVE: //send data in the FIFO
{
irqEn = 0x77;
waitIRq = 0x30;
break;
}
default:
break;
}
Write_MFRC522(CommIEnReg, irqEn|0x80); //Allow interruption
ClearBitMask(CommIrqReg, 0x80); //Clear all the interrupt bits
SetBitMask(FIFOLevelReg, 0x80); //FlushBuffer=1, FIFO initilizate
Write_MFRC522(CommandReg, PCD_IDLE); //NO action;cancel current command ???
//write data into FIFO
for (i=0; i<sendLen; i++)
{
Write_MFRC522(FIFODataReg, sendData);
}
//procceed it
Write_MFRC522(CommandReg, command);
if (command == PCD_TRANSCEIVE)
{
SetBitMask(BitFramingReg, 0x80); //StartSend=1,transmission of data starts
}
//waite receive data is finished
i = 2000; //i should adjust according the clock, the maxium the waiting time should be 25 ms???
do
{
//CommIrqReg[7..0]
//Set1 TxIRq RxIRq IdleIRq HiAlerIRq LoAlertIRq ErrIRq TimerIRq
n = Read_MFRC522(CommIrqReg);
i--;
}
while ((i!=0) && !(n&0x01) && !(n&waitIRq));
ClearBitMask(BitFramingReg, 0x80); //StartSend=0
if (i != 0)
{
if(!(Read_MFRC522(ErrorReg) & 0x1B)) //BufferOvfl Collerr CRCErr ProtecolErr
{
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)
{
*backLen = (n-1)*8 + lastBits;
}
else
{
*backLen = n*8;
}
if (n == 0)
{
n = 1;
}
if (n > MAX_LEN)
{
n = MAX_LEN;
}
//read the data from FIFO
for (i=0; i<n; i++)
{
backData = Read_MFRC522(FIFODataReg);
}
}
}
else
{
status = MI_ERR;
}
if (status == MI_OK)
{
//Verify card serial number
for (i=0; i<4; i++)
{
serNumCheck ^= serNum;
}
if (serNumCheck != serNum)
{
status = MI_ERR;
}
}
//SetBitMask(CollReg, 0x80); //ValuesAfterColl=1
return status;
}
/*
* Function:CalulateCRC
* Description:Use MF522 to caculate CRC
* Input parameter:pIndata--the CRC data need to be read,len--data length,pOutData-- the caculated result of CRC
* return:Null
*/
void CalulateCRC(uchar *pIndata, uchar len, uchar *pOutData)
{
uchar i, n;