diff --git a/MFRC522.cpp b/MFRC522.cpp index 2d9f571..01adb66 100644 --- a/MFRC522.cpp +++ b/MFRC522.cpp @@ -10,6 +10,11 @@ ///////////////////////////////////////////////////////////////////////////////////// // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// +/** + * Constructor. + */ +MFRC522::MFRC522() { +} // End constructor /** * Constructor. @@ -216,6 +221,18 @@ void MFRC522::PCD_Init() { PCD_AntennaOn(); // Enable the antenna driver pins TX1 and TX2 (they were disabled by the reset) } // End PCD_Init() +/** + * Initializes the MFRC522 chip. + */ +void MFRC522::PCD_Init( byte chipSelectPin, ///< Arduino pin connected to MFRC522's SPI slave select input (Pin 24, NSS, active low) + byte resetPowerDownPin ///< Arduino pin connected to MFRC522's reset and power down input (Pin 6, NRSTPD, active low) + ) { + _chipSelectPin = chipSelectPin; + _resetPowerDownPin = resetPowerDownPin; + // Set the chipSelectPin as digital output, do not select the slave yet + PCD_Init(); +} // End PCD_Init() + /** * Performs a soft reset on the MFRC522 chip and waits for it to be ready again. */ diff --git a/MFRC522.h b/MFRC522.h index 8a07bf1..00a0dd6 100644 --- a/MFRC522.h +++ b/MFRC522.h @@ -319,6 +319,7 @@ public: ///////////////////////////////////////////////////////////////////////////////////// // Functions for setting up the Arduino ///////////////////////////////////////////////////////////////////////////////////// + MFRC522(); MFRC522(byte chipSelectPin, byte resetPowerDownPin); void setSPIConfig(); @@ -338,6 +339,7 @@ public: // Functions for manipulating the MFRC522 ///////////////////////////////////////////////////////////////////////////////////// void PCD_Init(); + void PCD_Init(byte chipSelectPin, byte resetPowerDownPin); void PCD_Reset(); void PCD_AntennaOn(); void PCD_AntennaOff(); diff --git a/examples/ReadUidMultiReader/ReadUidMultiReader.ino b/examples/ReadUidMultiReader/ReadUidMultiReader.ino new file mode 100644 index 0000000..1c40be1 --- /dev/null +++ b/examples/ReadUidMultiReader/ReadUidMultiReader.ino @@ -0,0 +1,94 @@ +/** + * ---------------------------------------------------------------------------- + * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid + * for further details and other examples. + * + * NOTE: The library file MFRC522.h has a lot of useful info. Please read it. + * + * Released into the public domain. + * ---------------------------------------------------------------------------- + * This sample shows how to read and write data blocks on a MIFARE Classic PICC + * (= card/tag). + * + * BEWARE: Data will be written to the PICC, in sector #1 (blocks #4 to #7). + * + * + * Typical pin layout used: + * ----------------------------------------------------------------------------------------- + * MFRC522 Arduino Arduino Arduino Arduino Arduino + * Reader/PCD Uno Mega Nano v3 Leonardo/Micro Pro Micro + * Signal Pin Pin Pin Pin Pin Pin + * ----------------------------------------------------------------------------------------- + * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST + * SPI SS 1 SDA(SS) 10 53 D10 10 10 + * SPI SS 2 SDA(SS) 2 53 D10 10 10 + * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 + * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 + * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 + * + */ + +#include +#include + +#define RST_PIN 10 // Configurable, see typical pin layout above +#define SS_1_PIN 5 // Configurable, see typical pin layout above +#define SS_2_PIN 3 // Configurable, see typical pin layout above + +#define NR_OF_READERS 2 + +byte ssPins[] = {SS_1_PIN, SS_2_PIN}; + +MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance. + +/** + * Initialize. + */ +void setup() { + + Serial.begin(115200); // Initialize serial communications with the PC + while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) + + SPI.begin(); // Init SPI bus + + for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) { + mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card + } +} + +/** + * Main loop. + */ +void loop() { + + for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) { + // Look for new cards + + if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) { + Serial.print(F("Reader: ")); + Serial.print(reader); + // Show some details of the PICC (that is: the tag/card) + Serial.print(F(" Card UID:")); + dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size); + Serial.println(); + Serial.print(F("PICC type: ")); + byte piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak); + Serial.println(mfrc522[reader].PICC_GetTypeName(piccType)); + + // Halt PICC + mfrc522[reader].PICC_HaltA(); + // Stop encryption on PCD + mfrc522[reader].PCD_StopCrypto1(); + } //if (mfrc522[reader].PICC_IsNewC + } //for(uint8_t reader +} + +/** + * Helper routine to dump a byte array as hex values to Serial. + */ +void dump_byte_array(byte *buffer, byte bufferSize) { + for (byte i = 0; i < bufferSize; i++) { + Serial.print(buffer[i] < 0x10 ? " 0" : " "); + Serial.print(buffer[i], HEX); + } +}