Guest User

Untitled

a guest
Mar 21st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. // This example writes a MIFARE memory block 0x80. It is tested with a new MIFARE 1K cards. Uses default keys.
  2. // Note: Memory block 0 is readonly and contains manufacturer data. Do not write to Sector Trailer block
  3. // unless you know what you are doing. Otherwise, the MIFARE card may be unusable in the future.
  4.  
  5. //Contributed by Seeed Technology Inc (www.seeedstudio.com)
  6.  
  7. #include <PN532.h>
  8. #include <SPI.h>
  9.  
  10. /*Chip select pin can be connected to D10 or D9 which is hareware optional*/
  11. /*if you the version of NFC Shield from SeeedStudio is v2.0.*/
  12. #define PN532_CS 10
  13. PN532 nfc(PN532_CS);
  14.  
  15. uint8_t written=0;
  16. #define NFC_DEMO_DEBUG 1
  17.  
  18. void setup(void) {
  19. #ifdef NFC_DEMO_DEBUG
  20. Serial.begin(9600);
  21. Serial.println("Hello!");
  22. #endif
  23. nfc.begin();
  24.  
  25. uint32_t versiondata = nfc.getFirmwareVersion();
  26. if (! versiondata) {
  27. #ifdef NFC_DEMO_DEBUG
  28. Serial.print("Didn't find PN53x board");
  29. #endif
  30. while (1); // halt
  31. }
  32. #ifdef NFC_DEMO_DEBUG
  33. // Got ok data, print it out!
  34. Serial.print("Found chip PN5");
  35. Serial.println((versiondata>>24) & 0xFF, HEX);
  36. Serial.print("Firmware ver. ");
  37. Serial.print((versiondata>>16) & 0xFF, DEC);
  38. Serial.print('.');
  39. Serial.println((versiondata>>8) & 0xFF, DEC);
  40. Serial.print("Supports ");
  41. Serial.println(versiondata & 0xFF, HEX);
  42. #endif
  43. // configure board to read RFID tags and cards
  44. nfc.SAMConfig();
  45. }
  46.  
  47. void loop(void)
  48. {
  49. uint32_t id;
  50. // look for MiFare type cards
  51. id = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A);
  52.  
  53. if (id != 0)
  54. {
  55. #ifdef NFC_DEMO_DEBUG
  56. Serial.print("Read card #");
  57. Serial.println(id);
  58. Serial.println();
  59. #endif
  60. uint8_t keys[]= {
  61. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF };
  62. uint8_t writeBuffer[16];
  63. for(uint8_t i = 0;i < 16;i ++)
  64. {
  65. writeBuffer[i]=i; //Fill buffer with 0,1,2....F
  66. }
  67. if(nfc.authenticateBlock(1, id ,0x80,KEY_A,keys)) //authenticate block 0x80
  68. {
  69. //if authentication successful
  70.  
  71. if(written == 0) //Not written
  72. {
  73. written = nfc.writeMemoryBlock(1,0x80,writeBuffer); // Write writeBuffer[] to block 0x80
  74. if(written)
  75. #ifdef NFC_DEMO_DEBUG
  76. Serial.println("Write Successful");
  77. #endif
  78. }
  79.  
  80.  
  81. uint8_t block[16];
  82. //read memory block 0x80
  83. if(nfc.readMemoryBlock(1,0x80,block))
  84. {
  85. #ifdef NFC_DEMO_DEBUG
  86. Serial.println("Read block 0x80:");
  87. //if read operation is successful
  88. for(uint8_t i=0;i<16;i++)
  89. {
  90. //print memory block
  91. Serial.print(block[i],HEX);
  92. Serial.print(" ");
  93. }
  94. Serial.println();
  95. #endif
  96. }
  97. }
  98. }
  99.  
  100. delay(500);
  101. }
Add Comment
Please, Sign In to add comment