muezza29

Read Mifare Classic Data

Jun 29th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.15 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <Adafruit_PN532.h>
  4.  
  5. // If using the breakout with SPI, define the pins for SPI communication.
  6. #define PN532_SCK  (PA5)
  7. #define PN532_MOSI (PA7)
  8. #define PN532_SS   (PA4)
  9. #define PN532_MISO (PA6)
  10.  
  11. // If using the breakout or shield with I2C, define just the pins connected
  12. // to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
  13. #define PN532_IRQ   (2)
  14. #define PN532_RESET (3)  // Not connected by default on the NFC Shield
  15.  
  16. // Uncomment just _one_ line below depending on how your breakout or shield
  17. // is connected to the Arduino:
  18.  
  19. // Use this line for a breakout with a SPI connection:
  20. Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
  21.  
  22. // Use this line for a breakout with a hardware SPI connection.  Note that
  23. // the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
  24. // hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
  25. // SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
  26. //Adafruit_PN532 nfc(PN532_SS);
  27.  
  28. // Or use this line for a breakout or shield with an I2C connection:
  29. //Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
  30.  
  31. #if defined(ARDUINO_ARCH_SAMD)
  32. // for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
  33. // also change #define in Adafruit_PN532.cpp library file
  34. #define Serial SerialUSB
  35. #endif
  36.  
  37. void setup(void) {
  38. #ifndef ESP8266
  39.   while (!Serial); // for Leonardo/Micro/Zero
  40. #endif
  41.   Serial.begin(115200);
  42.   Serial.println("Hello!");
  43.  
  44.   nfc.begin();
  45.  
  46.   uint32_t versiondata = nfc.getFirmwareVersion();
  47.   if (! versiondata) {
  48.     Serial.print("Didn't find PN53x board");
  49.     while (1); // halt
  50.   }
  51.   // Got ok data, print it out!
  52.   Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX);
  53.   Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC);
  54.   Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC);
  55.  
  56.   // configure board to read RFID tags
  57.   nfc.SAMConfig();
  58.  
  59.   Serial.println("Waiting for an ISO14443A Card ...");
  60. }
  61.  
  62.  
  63. void loop(void) {
  64.   uint8_t success;
  65.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  66.   uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  67.  
  68.   // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found
  69.   // 'uid' will be populated with the UID, and uidLength will indicate
  70.   // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
  71.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  72.  
  73.   if (success) {
  74.     // Display some basic information about the card
  75.     Serial.println("Found an ISO14443A card");
  76.     Serial.print("  UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes");
  77.     Serial.print("  UID Value: ");
  78.     Serial.println(nfc.PrintHex(uid, uidLength));
  79. //    if (nfc.PrintHex == '0xC6 0xE6 0x0F 0x1E')
  80. //    {
  81. //      Serial.println("NFC tag dikenali sebagai Arif");
  82. //    }
  83. //    else
  84. //    {
  85. //      Serial.println("NFC tag tidak dikenali");
  86. //    }
  87.     Serial.println("");
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment