/* * File Name : RFID.pde * Creator : Dr.Leong (WWW.B2CQSHOP.COM) * Create Date: 2011.09.19 * Modified by : * Modify Date: * Chinese comments translated using Google Translate by * Description : Mifare1 -> find cards - > Anti-collision - > Select Card - > reader interface */ // the sensor communicates using SPI, so include the library: #include #define uchar unsigned char #define uint unsigned int // Maximum length of the array #define MAX_LEN 16 ///////////////////////////////////////////////////////////////////// //set the pin ///////////////////////////////////////////////////////////////////// const int chipSelectPin = 10; const int NRSTPD = 5; //MF522 command word #define PCD_IDLE 0x00 //NO action; cancel the current command #define PCD_AUTHENT 0x0E // authentication key #define PCD_RECEIVE 0x08 // receive data #define PCD_TRANSMIT 0x04 // Transmit Data #define PCD_TRANSCEIVE 0x0C // Send and receive data #define PCD_RESETPHASE 0x0F // Reset #define PCD_CALCCRC 0x03 // CRC calculation //Mifare_One card command word #define PICC_REQIDL 0x26 // find the antenna area does not enter hibernation #define PICC_REQALL 0x52 // find all the cards antenna area #define PICC_ANTICOLL 0x93 // anti-collision #define PICC_SElECTTAG 0x93 // election card #define PICC_AUTHENT1A 0x60 // authentication key A #define PICC_AUTHENT1B 0x61 // authentication key B #define PICC_READ 0x30 // Read Block #define PICC_WRITE 0xA0 // write block #define PICC_DECREMENT 0xC0 // debit? #define PICC_INCREMENT 0xC1 // recharge? #define PICC_RESTORE 0xC2 // transfer block data to the buffer #define PICC_TRANSFER 0xB0 // save the data in the buffer #define PICC_HALT 0x50 // Sleep //和MF522 communication error code is returned when #define MI_OK 0 #define MI_NOTAGERR 1 #define MI_ERR 2 //------------------MFRC522 register--------------- //Page 0:Command and Status #define Reserved00 0x00 #define CommandReg 0x01 #define CommIEnReg 0x02 #define DivlEnReg 0x03 #define CommIrqReg 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 Reserved01 0x0F //Page 1:Command #define Reserved10 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 Reserved11 0x1A #define Reserved12 0x1B #define MifareReg 0x1C #define Reserved13 0x1D #define Reserved14 0x1E #define SerialSpeedReg 0x1F //Page 2:CFG #define Reserved20 0x20 #define CRCResultRegM 0x21 #define CRCResultRegL 0x22 #define Reserved21 0x23 #define ModWidthReg 0x24 #define Reserved22 0x25 #define RFCfgReg 0x26 #define GsNReg 0x27 #define CWGsPReg 0x28 #define ModGsPReg 0x29 #define TModeReg 0x2A #define TPrescalerReg 0x2B #define TReloadRegH 0x2C #define TReloadRegL 0x2D #define TCounterValueRegH 0x2E #define TCounterValueRegL 0x2F //Page 3:TestRegister #define Reserved30 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 Reserved31 0x3C #define Reserved32 0x3D #define Reserved33 0x3E #define Reserved34 0x3F //----------------------------------------------- //4 bytes card serial number , the first 5 bytes for the checksum byte uchar serNum[5]; uchar writeData[16]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100}; // Initialize 100 dollars uchar moneyConsume = 18 ; // spending 18 yuan uchar moneyAdd = 10 ; // recharge 10 yuan // Sector A password , 16 sectors , each sector password 6Byte uchar sectorKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, //{0x19, 0x84, 0x07, 0x15, 0x76, 0x14}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, }; uchar sectorNewKeyA[16][16] = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0x19,0x84,0x07,0x15,0x76,0x14}, //you can set another ket , such as " 0x19, 0x84, 0x07, 0x15, 0x76, 0x14 " //{0x19, 0x84, 0x07, 0x15, 0x76, 0x14, 0xff,0x07,0x80,0x69, 0x19,0x84,0x07,0x15,0x76,0x14}, // but when loop, please set the sectorKeyA, the same key, so that RFID module can read the card {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0x19,0x33,0x07,0x15,0x34,0x14}, }; void setup() { Serial.begin(9600); // RFID reader SOUT pin connected to Serial RX pin at 2400bps Serial.println("Ready"); // start the SPI library: 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 10 , Not Reset and Power-down digitalWrite(NRSTPD, HIGH); MFRC522_Init(); } void loop() { uchar i,tmp; uchar status; uchar str[MAX_LEN]; uchar RC_size; uchar blockAddr; // select the operating block addresses 0 to 63 // Find the card , back card type status = MFRC522_Request(PICC_REQIDL, str); if (status == MI_OK) { Serial.println("Find out a card "); Serial.print(str[0],BIN); Serial.print(" , "); Serial.print(str[1],BIN); Serial.println(" "); } // Anti-collision, return card serial number 4 bytes status = MFRC522_Anticoll(str); memcpy(serNum, str, 5); if (status == MI_OK) { Serial.println("The card's number is : "); Serial.print(serNum[0],BIN); Serial.print(" , "); Serial.print(serNum[1],BIN); Serial.print(" , "); Serial.print(serNum[2],BIN); Serial.print(" , "); Serial.print(serNum[3],BIN); Serial.print(" , "); Serial.print(serNum[4],BIN); Serial.println(" "); } // Election card , return to card capacity RC_size = MFRC522_SelectTag(serNum); if (RC_size != 0) { Serial.print("The size of the card is : "); Serial.print(RC_size,DEC); Serial.println(" K bits "); } // Registration Card blockAddr = 7; // data block 7 status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr/4], serNum); // Certification if (status == MI_OK) { // Write data status = MFRC522_Write(blockAddr, sectorNewKeyA[blockAddr/4]); Serial.print("set the new card password, and can modify the data of the Sector "); Serial.print(blockAddr/4,DEC); Serial.println(" : "); for (i=0; i<6; i++) { Serial.print(sectorNewKeyA[blockAddr/4][i],HEX); Serial.print(" , "); } Serial.println(" "); blockAddr = blockAddr - 3 ; status = MFRC522_Write(blockAddr, writeData); if(status == MI_OK) { Serial.println("You are B2CQSHOP VIP Member, The card has $100 !"); } } // Reader blockAddr = 7; // data block 7 status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], serNum); // Certification if (status == MI_OK) { // Read data blockAddr = blockAddr - 3 ; status = MFRC522_Read(blockAddr, str); if (status == MI_OK) { Serial.println("Read from the card ,the data is : "); for (i=0; i<16; i++) { Serial.print(str[i],DEC); Serial.print(" , "); } Serial.println(" "); } } // Spending blockAddr = 7; // data block 7 status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], serNum); // Certification if (status == MI_OK) { // Read data blockAddr = blockAddr - 3 ; status = MFRC522_Read(blockAddr, str); if (status == MI_OK) { if( str[15] < moneyConsume ) { Serial.println(" The money is not enough !"); } else { str[15] = str[15] - moneyConsume; status = MFRC522_Write(blockAddr, str); if(status == MI_OK) { Serial.print("You pay $18 for items in B2CQSHOP.COM . Now, Your money balance is : $"); Serial.print(str[15],DEC); Serial.println(" "); } } } } // Recharge blockAddr = 7; // data block 7 status = MFRC522_Auth(PICC_AUTHENT1A, blockAddr, sectorNewKeyA[blockAddr/4], serNum); // Certification if (status == MI_OK) { // Read data blockAddr = blockAddr - 3 ; status = MFRC522_Read(blockAddr, str); if (status == MI_OK) { tmp = (int)(str[15] + moneyAdd) ; //Serial.println(tmp,DEC); if( tmp < (char)254 ) { Serial.println(" The money of the card can not be more than 255 !"); } else { str[15] = str[15] + moneyAdd ; status = MFRC522_Write(blockAddr, str); if(status == MI_OK) { Serial.print("You add $10 to your card in B2CQSHOP.COM , Your money balance is : $"); Serial.print(str[15],DEC); Serial.println(" "); } } } } Serial.println(" "); MFRC522_Halt(); // command card into hibernation } /* * Function Name : Write_MFRC5200 * Description: MFRC522 of a register to write a byte of data * Input Parameters : addr - register address ; val - the value to be written * Return value: None */ void Write_MFRC522(uchar addr, uchar val) { digitalWrite(chipSelectPin, LOW); // Address Format :0XXXXXX0 SPI.transfer((addr<<1)&0x7E); SPI.transfer(val); digitalWrite(chipSelectPin, HIGH); } /* * Function Name : Read_MFRC522 * Description : From a certain MFRC522 read a byte of data register * Input Parameters : addr - register address * Return value : Returns a byte of data read from the */ uchar Read_MFRC522(uchar addr) { uchar val; digitalWrite(chipSelectPin, LOW); // Address Format : 1XXXXXX0 SPI.transfer(((addr<<1)&0x7E) | 0x80); val =SPI.transfer(0x00); digitalWrite(chipSelectPin, HIGH); return val; } /* * Function Name : SetBitMask * Description: Set RC522 register bit * Input parameters : reg - register address ; mask - set value * Return value: None */ void SetBitMask(uchar reg, uchar mask) { uchar tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp | mask); // set bit mask } /* * Function Name : ClearBitMask * Description : clear RC522 register bit * Input parameters : reg - register address ; mask - clear bit value * Return value: None */ void ClearBitMask(uchar reg, uchar mask) { uchar tmp; tmp = Read_MFRC522(reg); Write_MFRC522(reg, tmp & (~mask)); // clear bit mask } /* * Function Name : AntennaOn * Description : Open antennas, each time you start or shut down the natural barrier between the transmitter should be at least 1ms interval * Input: None * Return value: None */ void AntennaOn(void) { uchar temp; temp = Read_MFRC522(TxControlReg); if (!(temp & 0x03)) { SetBitMask(TxControlReg, 0x03); } } /* * Function Name : AntennaOff * Description : Close antennas, each time you start or shut down the natural barrier between the transmitter should be at least 1ms interval * Input: None * Return value: None * / */ void AntennaOff(void) { ClearBitMask(TxControlReg, 0x03); } /* * Function Name : ResetMFRC522 * Description : Reset RC522 * Input: None * Return value: None */ void MFRC522_Reset(void) { Write_MFRC522(CommandReg, PCD_RESETPHASE); } /* * Function Name : InitMFRC522 * Description : Initialize RC522 * Input: None * Return value: None */ void MFRC522_Init(void) { digitalWrite(NRSTPD,HIGH); MFRC522_Reset(); //Timer: TPrescaler*TreloadVal/6.78MHz = 24ms Write_MFRC522(TModeReg, 0x8D); //Tauto=1; f(Timer) = 6.78MHz/TPreScaler Write_MFRC522(TPrescalerReg, 0x3E); //TModeReg[3..0] + TPrescalerReg Write_MFRC522(TReloadRegL, 30); Write_MFRC522(TReloadRegH, 0); Write_MFRC522(TxAutoReg, 0x40); //100%ASK Write_MFRC522(ModeReg, 0x3D); //CRC初始值0x6363 ??? //ClearBitMask(Status2Reg, 0x08); //MFCrypto1On=0 //Write_MFRC522(RxSelReg, 0x86); //RxWait = RxSelReg[5..0] //Write_MFRC522(RFCfgReg, 0x7F); //RxGain = 48dB AntennaOn(); //打开天线 } /* * Function Name : MFRC522_Request * Description : Find cards , read the card type number * Input parameters : reqMode - find cards way * TagType - Return Card Type * 0x4400 = Mifare_UltraLight * 0x0400 = Mifare_One (S50) * 0x0200 = Mifare_One (S70) * 0x0800 = Mifare_Pro (X) * 0x4403 = Mifare_DESFire * Return value: the successful return MI_OK */ uchar MFRC522_Request(uchar reqMode, uchar *TagType) { uchar status; uint backBits; // received data bits Write_MFRC522(BitFramingReg, 0x07); //TxLastBists = BitFramingReg[2..0] ??? TagType[0] = reqMode; status = MFRC522_ToCard(PCD_TRANSCEIVE, TagType, 1, TagType, &backBits); if ((status != MI_OK) || (backBits != 0x10)) { status = MI_ERR; } return status; } /* * Function Name : MFRC522_ToCard * Description : RC522 and ISO14443 card communication * Input Parameters : command - MF522 command word, * SendData - RC522 sent to the card via the data * SendLen - length of data sent * BackData - received the card returns data, * BackLen - return data bit length * Return value: the successful return MI_OK */ 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: // certification cards close { irqEn = 0x12; waitIRq = 0x10; break; } case PCD_TRANSCEIVE: // transmit FIFO data { irqEn = 0x77; waitIRq = 0x30; break; } default: break; } Write_MFRC522(CommIEnReg, irqEn|0x80); // enable interrupt request ClearBitMask(CommIrqReg, 0x80); // clear all interrupt request bit SetBitMask(FIFOLevelReg, 0x80); // FlushBuffer = 1, FIFO initialization Write_MFRC522(CommandReg, PCD_IDLE); // NO action; cancel the current command ? ? ? // Write data to the FIFO for (i=0; i MAX_LEN) { n = MAX_LEN; } // Read the received data in FIFO for (i=0; i