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.

138 linhas
4.4KB

  1. /**
  2. * ----------------------------------------------------------------------------
  3. * This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid
  4. * for further details and other examples.
  5. *
  6. * NOTE: The library file MFRC522.h has a lot of useful info. Please read it.
  7. *
  8. * Released into the public domain.
  9. * ----------------------------------------------------------------------------
  10. * Minimal example how to use the interrupts to read the UID of a MIFARE Classic PICC
  11. * (= card/tag).
  12. *
  13. *
  14. * Typical pin layout used:
  15. * -----------------------------------------------------------------------------------------
  16. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  17. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  18. * Signal Pin Pin Pin Pin Pin Pin
  19. * -----------------------------------------------------------------------------------------
  20. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  21. * SPI SS SDA(SS) 10 53 D10 3 10
  22. * IRQ ? ? ? ? 2 10
  23. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  24. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  25. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  26. *
  27. */
  28. #include <SPI.h>
  29. #include <MFRC522.h>
  30. constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
  31. constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
  32. constexpr uint8_t IRQ_PIN = 2; // Configurable, depends on hardware
  33. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  34. MFRC522::MIFARE_Key key;
  35. volatile boolean bNewInt = false;
  36. byte regVal = 0x7F;
  37. void activateRec(MFRC522 mfrc522);
  38. void clearInt(MFRC522 mfrc522);
  39. /**
  40. * Initialize.
  41. */
  42. void setup() {
  43. Serial.begin(115200); // Initialize serial communications with the PC
  44. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  45. SPI.begin(); // Init SPI bus
  46. mfrc522.PCD_Init(); // Init MFRC522 card
  47. /* read and printout the MFRC522 version (valid values 0x91 & 0x92)*/
  48. Serial.print(F("Ver: 0x"));
  49. byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
  50. Serial.println(readReg, HEX);
  51. /* setup the IRQ pin*/
  52. pinMode(IRQ_PIN, INPUT_PULLUP);
  53. /*
  54. * Allow the ... irq to be propagated to the IRQ pin
  55. * For test purposes propagate the IdleIrq and loAlert
  56. */
  57. regVal = 0xA0; //rx irq
  58. mfrc522.PCD_WriteRegister(mfrc522.ComIEnReg, regVal);
  59. bNewInt = false; //interrupt flag
  60. /*Activate the interrupt*/
  61. attachInterrupt(digitalPinToInterrupt(IRQ_PIN), readCard, FALLING);
  62. do { //clear a spourious interrupt at start
  63. ;
  64. } while (!bNewInt);
  65. bNewInt = false;
  66. Serial.println(F("End setup"));
  67. }
  68. /**
  69. * Main loop.
  70. */
  71. void loop() {
  72. if (bNewInt) { //new read interrupt
  73. Serial.print(F("Interrupt. "));
  74. mfrc522.PICC_ReadCardSerial(); //read the tag data
  75. // Show some details of the PICC (that is: the tag/card)
  76. Serial.print(F("Card UID:"));
  77. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  78. Serial.println();
  79. clearInt(mfrc522);
  80. mfrc522.PICC_HaltA();
  81. bNewInt = false;
  82. }
  83. // The receiving block needs regular retriggering (tell the tag it should transmit??)
  84. // (mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg,mfrc522.PICC_CMD_REQA);)
  85. activateRec(mfrc522);
  86. delay(100);
  87. } //loop()
  88. /**
  89. * Helper routine to dump a byte array as hex values to Serial.
  90. */
  91. void dump_byte_array(byte *buffer, byte bufferSize) {
  92. for (byte i = 0; i < bufferSize; i++) {
  93. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  94. Serial.print(buffer[i], HEX);
  95. }
  96. }
  97. /**
  98. * MFRC522 interrupt serving routine
  99. */
  100. void readCard() {
  101. bNewInt = true;
  102. }
  103. /*
  104. * The function sending to the MFRC522 the needed commands to activate the reception
  105. */
  106. void activateRec(MFRC522 mfrc522) {
  107. mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg, mfrc522.PICC_CMD_REQA);
  108. mfrc522.PCD_WriteRegister(mfrc522.CommandReg, mfrc522.PCD_Transceive);
  109. mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87);
  110. }
  111. /*
  112. * The function to clear the pending interrupt bits after interrupt serving routine
  113. */
  114. void clearInt(MFRC522 mfrc522) {
  115. mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F);
  116. }