MinimalInterrupt.ino 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
  28. *
  29. */
  30. #include <SPI.h>
  31. #include <MFRC522.h>
  32. #define RST_PIN 9 // Configurable, see typical pin layout above
  33. #define SS_PIN 3 // Configurable, see typical pin layout above
  34. #define IRQ_PIN 2 // Configurable, depends on hardware
  35. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  36. volatile bool bNewInt = false;
  37. byte regVal = 0x7F;
  38. void activateRec(MFRC522 mfrc522);
  39. void clearInt(MFRC522 mfrc522);
  40. /**
  41. * Initialize.
  42. */
  43. void setup() {
  44. Serial.begin(115200); // Initialize serial communications with the PC
  45. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  46. SPI.begin(); // Init SPI bus
  47. mfrc522.PCD_Init(); // Init MFRC522 card
  48. /* read and printout the MFRC522 version (valid values 0x91 & 0x92)*/
  49. Serial.print(F("Ver: 0x"));
  50. byte readReg = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
  51. Serial.println(readReg, HEX);
  52. /* setup the IRQ pin*/
  53. pinMode(IRQ_PIN, INPUT_PULLUP);
  54. /*
  55. * Allow the ... irq to be propagated to the IRQ pin
  56. * For test purposes propagate the IdleIrq and loAlert
  57. */
  58. regVal = 0xA0; //rx irq
  59. mfrc522.PCD_WriteRegister(mfrc522.ComIEnReg, regVal);
  60. bNewInt = false; //interrupt flag
  61. /*Activate the interrupt*/
  62. attachInterrupt(digitalPinToInterrupt(IRQ_PIN), readCard, FALLING);
  63. do { //clear a spourious interrupt at start
  64. ;
  65. } while (!bNewInt);
  66. bNewInt = false;
  67. Serial.println(F("End setup"));
  68. }
  69. /**
  70. * Main loop.
  71. */
  72. void loop() {
  73. if (bNewInt) { //new read interrupt
  74. Serial.print(F("Interrupt. "));
  75. mfrc522.PICC_ReadCardSerial(); //read the tag data
  76. // Show some details of the PICC (that is: the tag/card)
  77. Serial.print(F("Card UID:"));
  78. dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
  79. Serial.println();
  80. clearInt(mfrc522);
  81. mfrc522.PICC_HaltA();
  82. bNewInt = false;
  83. }
  84. // The receiving block needs regular retriggering (tell the tag it should transmit??)
  85. // (mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg,mfrc522.PICC_CMD_REQA);)
  86. activateRec(mfrc522);
  87. delay(100);
  88. } //loop()
  89. /**
  90. * Helper routine to dump a byte array as hex values to Serial.
  91. */
  92. void dump_byte_array(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. * MFRC522 interrupt serving routine
  100. */
  101. void readCard() {
  102. bNewInt = true;
  103. }
  104. /*
  105. * The function sending to the MFRC522 the needed commands to activate the reception
  106. */
  107. void activateRec(MFRC522 mfrc522) {
  108. mfrc522.PCD_WriteRegister(mfrc522.FIFODataReg, mfrc522.PICC_CMD_REQA);
  109. mfrc522.PCD_WriteRegister(mfrc522.CommandReg, mfrc522.PCD_Transceive);
  110. mfrc522.PCD_WriteRegister(mfrc522.BitFramingReg, 0x87);
  111. }
  112. /*
  113. * The function to clear the pending interrupt bits after interrupt serving routine
  114. */
  115. void clearInt(MFRC522 mfrc522) {
  116. mfrc522.PCD_WriteRegister(mfrc522.ComIrqReg, 0x7F);
  117. }