PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

128 linhas
4.5KB

  1. /*
  2. * --------------------------------------------------------------------------------------------------------------------
  3. * Example sketch/program showing how to read new NUID from a 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 the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
  8. * Reader on the Arduino SPI interface.
  9. *
  10. * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
  11. * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
  12. * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
  13. * will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages
  14. * when removing the PICC from reading distance too early.
  15. *
  16. * @license Released into the public domain.
  17. *
  18. * Typical pin layout used:
  19. * -----------------------------------------------------------------------------------------
  20. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  21. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  22. * Signal Pin Pin Pin Pin Pin Pin
  23. * -----------------------------------------------------------------------------------------
  24. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  25. * SPI SS SDA(SS) 10 53 D10 10 10
  26. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  27. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  28. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  29. */
  30. #include <SPI.h>
  31. #include <MFRC522.h>
  32. constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
  33. constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
  34. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
  35. MFRC522::MIFARE_Key key;
  36. // Init array that will store new NUID
  37. byte nuidPICC[4];
  38. void setup() {
  39. Serial.begin(9600);
  40. SPI.begin(); // Init SPI bus
  41. rfid.PCD_Init(); // Init MFRC522
  42. for (byte i = 0; i < 6; i++) {
  43. key.keyByte[i] = 0xFF;
  44. }
  45. Serial.println(F("This code scan the MIFARE Classsic NUID."));
  46. Serial.print(F("Using the following key:"));
  47. printHex(key.keyByte, MFRC522::MF_KEY_SIZE);
  48. }
  49. void loop() {
  50. // Look for new cards
  51. if ( ! rfid.PICC_IsNewCardPresent())
  52. return;
  53. // Verify if the NUID has been readed
  54. if ( ! rfid.PICC_ReadCardSerial())
  55. return;
  56. Serial.print(F("PICC type: "));
  57. MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  58. Serial.println(rfid.PICC_GetTypeName(piccType));
  59. // Check is the PICC of Classic MIFARE type
  60. if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  61. piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  62. piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  63. Serial.println(F("Your tag is not of type MIFARE Classic."));
  64. return;
  65. }
  66. if (rfid.uid.uidByte[0] != nuidPICC[0] ||
  67. rfid.uid.uidByte[1] != nuidPICC[1] ||
  68. rfid.uid.uidByte[2] != nuidPICC[2] ||
  69. rfid.uid.uidByte[3] != nuidPICC[3] ) {
  70. Serial.println(F("A new card has been detected."));
  71. // Store NUID into nuidPICC array
  72. for (byte i = 0; i < 4; i++) {
  73. nuidPICC[i] = rfid.uid.uidByte[i];
  74. }
  75. Serial.println(F("The NUID tag is:"));
  76. Serial.print(F("In hex: "));
  77. printHex(rfid.uid.uidByte, rfid.uid.size);
  78. Serial.println();
  79. Serial.print(F("In dec: "));
  80. printDec(rfid.uid.uidByte, rfid.uid.size);
  81. Serial.println();
  82. }
  83. else Serial.println(F("Card read previously."));
  84. // Halt PICC
  85. rfid.PICC_HaltA();
  86. // Stop encryption on PCD
  87. rfid.PCD_StopCrypto1();
  88. }
  89. /**
  90. * Helper routine to dump a byte array as hex values to Serial.
  91. */
  92. void printHex(byte *buffer, byte bufferSize) {
  93. for (byte i = 0; i < bufferSize; i++) {
  94. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  95. Serial.print(buffer[i], HEX);
  96. }
  97. }
  98. /**
  99. * Helper routine to dump a byte array as dec values to Serial.
  100. */
  101. void printDec(byte *buffer, byte bufferSize) {
  102. for (byte i = 0; i < bufferSize; i++) {
  103. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  104. Serial.print(buffer[i], DEC);
  105. }
  106. }