PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

98 行
3.9KB

  1. /**
  2. * --------------------------------------------------------------------------------------------------------------------
  3. * Example sketch/program showing how to read data from more than one PICC to serial.
  4. * --------------------------------------------------------------------------------------------------------------------
  5. * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6. *
  7. * Example sketch/program showing how to read data from more than one PICC (that is: a RFID Tag or Card) using a
  8. * MFRC522 based RFID Reader on the Arduino SPI interface.
  9. *
  10. * Warning: This may not work! Multiple devices at one SPI are difficult and cause many trouble!! Engineering skill
  11. * and knowledge are required!
  12. *
  13. * @license Released into the public domain.
  14. *
  15. * Typical pin layout used:
  16. * -----------------------------------------------------------------------------------------
  17. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  18. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  19. * Signal Pin Pin Pin Pin Pin Pin
  20. * -----------------------------------------------------------------------------------------
  21. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  22. * SPI SS 1 SDA(SS) ** custom, take a unused pin, only HIGH/LOW required **
  23. * SPI SS 2 SDA(SS) ** custom, take a unused pin, only HIGH/LOW required **
  24. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  25. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  26. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  27. *
  28. */
  29. #include <SPI.h>
  30. #include <MFRC522.h>
  31. constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
  32. constexpr uint8_t SS_1_PIN = 10; // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 2
  33. constexpr uint8_t SS_2_PIN = 8; // Configurable, take a unused pin, only HIGH/LOW required, must be diffrent to SS 1
  34. constexpr uint8_t NR_OF_READERS = 2;
  35. byte ssPins[] = {SS_1_PIN, SS_2_PIN};
  36. MFRC522 mfrc522[NR_OF_READERS]; // Create MFRC522 instance.
  37. /**
  38. * Initialize.
  39. */
  40. void setup() {
  41. Serial.begin(9600); // Initialize serial communications with the PC
  42. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  43. SPI.begin(); // Init SPI bus
  44. for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  45. mfrc522[reader].PCD_Init(ssPins[reader], RST_PIN); // Init each MFRC522 card
  46. Serial.print(F("Reader "));
  47. Serial.print(reader);
  48. Serial.print(F(": "));
  49. mfrc522[reader].PCD_DumpVersionToSerial();
  50. }
  51. }
  52. /**
  53. * Main loop.
  54. */
  55. void loop() {
  56. for (uint8_t reader = 0; reader < NR_OF_READERS; reader++) {
  57. // Look for new cards
  58. if (mfrc522[reader].PICC_IsNewCardPresent() && mfrc522[reader].PICC_ReadCardSerial()) {
  59. Serial.print(F("Reader "));
  60. Serial.print(reader);
  61. // Show some details of the PICC (that is: the tag/card)
  62. Serial.print(F(": Card UID:"));
  63. dump_byte_array(mfrc522[reader].uid.uidByte, mfrc522[reader].uid.size);
  64. Serial.println();
  65. Serial.print(F("PICC type: "));
  66. MFRC522::PICC_Type piccType = mfrc522[reader].PICC_GetType(mfrc522[reader].uid.sak);
  67. Serial.println(mfrc522[reader].PICC_GetTypeName(piccType));
  68. // Halt PICC
  69. mfrc522[reader].PICC_HaltA();
  70. // Stop encryption on PCD
  71. mfrc522[reader].PCD_StopCrypto1();
  72. } //if (mfrc522[reader].PICC_IsNewC
  73. } //for(uint8_t reader
  74. }
  75. /**
  76. * Helper routine to dump a byte array as hex values to Serial.
  77. */
  78. void dump_byte_array(byte *buffer, byte bufferSize) {
  79. for (byte i = 0; i < bufferSize; i++) {
  80. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  81. Serial.print(buffer[i], HEX);
  82. }
  83. }