PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

113 lines
4.2KB

  1. /*
  2. * --------------------------------------------------------------------------------------------------------------------
  3. * Example to change UID of changeable MIFARE card.
  4. * --------------------------------------------------------------------------------------------------------------------
  5. * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6. *
  7. * This sample shows how to set the UID on a UID changeable MIFARE card.
  8. * NOTE: for more informations read the README.rst
  9. *
  10. * @author Tom Clement
  11. * @license Released into the public domain.
  12. *
  13. * Typical pin layout used:
  14. * -----------------------------------------------------------------------------------------
  15. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  16. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  17. * Signal Pin Pin Pin Pin Pin Pin
  18. * -----------------------------------------------------------------------------------------
  19. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  20. * SPI SS SDA(SS) 10 53 D10 10 10
  21. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  22. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  23. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  24. */
  25. #include <SPI.h>
  26. #include <MFRC522.h>
  27. #include <MFRC522Hack.h>
  28. constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
  29. constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
  30. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
  31. MFRC522Hack mfrc522Hack(&mfrc522); // Create MFRC522Hack instance.
  32. /* Set your new UID here! */
  33. byte newUid[] = {0xDE, 0xAD, 0xBE, 0xEF};
  34. MFRC522::MIFARE_Key key;
  35. void setup() {
  36. Serial.begin(9600); // Initialize serial communications with the PC
  37. while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  38. SPI.begin(); // Init SPI bus
  39. mfrc522.PCD_Init(); // Init MFRC522 card
  40. Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!"));
  41. // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
  42. for (byte i = 0; i < 6; i++) {
  43. key.keyByte[i] = 0xFF;
  44. }
  45. }
  46. // Setting the UID can be as simple as this:
  47. //void loop() {
  48. // byte newUid[] = NEW_UID;
  49. // if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
  50. // Serial.println("Wrote new UID to card.");
  51. // }
  52. // delay(1000);
  53. //}
  54. // But of course this is a more proper approach
  55. void loop() {
  56. // Look for new cards, and select one if present
  57. if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
  58. delay(50);
  59. return;
  60. }
  61. // Now a card is selected. The UID and SAK is in mfrc522.uid.
  62. // Dump UID
  63. Serial.print(F("Card UID:"));
  64. for (byte i = 0; i < mfrc522.uid.size; i++) {
  65. Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
  66. Serial.print(mfrc522.uid.uidByte[i], HEX);
  67. }
  68. Serial.println();
  69. // Dump PICC type
  70. // MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
  71. // Serial.print(F("PICC type: "));
  72. // Serial.print(mfrc522.PICC_GetTypeName(piccType));
  73. // Serial.print(F(" (SAK "));
  74. // Serial.print(mfrc522.uid.sak);
  75. // Serial.print(")\r\n");
  76. // if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
  77. // && piccType != MFRC522::PICC_TYPE_MIFARE_1K
  78. // && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  79. // Serial.println(F("This sample only works with MIFARE Classic cards."));
  80. // return;
  81. // }
  82. // Set new UID
  83. if ( mfrc522Hack.MIFARE_SetUid(newUid, (byte)4, true) ) {
  84. Serial.println(F("Wrote new UID to card."));
  85. }
  86. // Halt PICC and re-select it so DumpToSerial doesn't get confused
  87. mfrc522.PICC_HaltA();
  88. if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
  89. return;
  90. }
  91. // Dump the new memory contents
  92. Serial.println(F("New UID and contents:"));
  93. mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
  94. delay(2000);
  95. }