Advertisement
freeridre

PN532 IRQ test

Mar 8th, 2022
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "emulatetag.h"
  2. #include "NdefMessage.h"
  3. #include <avr/wdt.h>
  4.  
  5. #include <SPI.h>
  6. #include <PN532_SPI.h>
  7. #include "PN532.h"
  8. //MEGA
  9. #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  10. PN532_SPI pn532spi(SPI, 53);
  11. //UNO
  12. #elif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
  13. PN532_SPI pn532spi(SPI, 10);
  14. #endif
  15.  
  16.  
  17. #define PN532IRQPIN (2)
  18. PN532 nfc(pn532spi);
  19. void cardreading();
  20.  
  21. void setup() {
  22.   // put your setup code here, to run once:
  23.   //pinMode(PN532IRQPIN, INPUT);
  24.   Serial.begin(115200);
  25.   Serial.println("\nHello!");
  26.   nfc.begin();
  27.   uint32_t versiondata = nfc.getFirmwareVersion();
  28.   if (! versiondata) {
  29.     Serial.print("Didn't find PN53x board");
  30.     while (1); // halt
  31.   }
  32.   // Got ok data, print it out!
  33.   Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  34.   Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  35.   Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
  36.  
  37.   nfc.SAMConfig();
  38.   //pinMode(PN532IRQPIN, INPUT_PULLUP);
  39.   //nfc.setPassiveActivationRetries(0x00);
  40.   attachInterrupt(digitalPinToInterrupt(PN532IRQPIN), cardreading, FALLING);
  41.   //attachInterrupt(0, cardreading, FALLING);
  42. }
  43.  
  44. void loop() {
  45.   // put your main code here, to run repeatedly:
  46.  
  47. }
  48. void cardreading()
  49. {
  50.   Serial.println("Interrupted");
  51.   uint8_t success;
  52.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  53.   uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  54.   // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  55.   // 'uid' will be populated with the UID, and uidLength will indicate
  56.   // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  57.   //nfc.inListPassiveTarget();
  58.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  59.  
  60.   if (success)
  61.   {
  62.     // Display some basic information about the card
  63.     Serial.println("Found an ISO14443A card");
  64.     Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
  65.     Serial.print("  UID Value: ");
  66.     nfc.PrintHex(uid, uidLength);
  67.     Serial.println("");
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement