当前位置:   article > 正文

Arduino MFRC522库的使用,ReadNUID例程注释

mfrc522库

  对Arduino MFRC522库中的ReadNUID示例(可用于读取卡号)进行了详细的注释,供大家参考。如有错误还请指正!

  如使用ESP32,接线采用VSPI,对照见下图。

  使用该方式后需将代码中的SS_PIN 10更改为SS_PIN 5,RST_PIN 9更改为RST_PIN 27

 

  1. /*
  2. * --------------------------------------------------------------------------------------------------------------------
  3. * Example sketch/program showing how to read new NUID from a PICC to serial.
  4. * --------------------------------------------------------------------------------------------------------------------
  5. * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6. *
  7. * Example sketch/program showing how to the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
  8. * Reader on the Arduino SPI interface.
  9. *
  10. * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
  11. * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
  12. * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
  13. * will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages
  14. * when removing the PICC from reading distance too early.
  15. *
  16. * @license Released into the public domain.
  17. * "上面是一些介绍"
  18. * Typical pin layout used:
  19. * -----------------------------------------------------------------------------------------
  20. * MFRC522 Arduino Arduino Arduino Arduino Arduino
  21. * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
  22. * Signal Pin Pin Pin Pin Pin Pin
  23. * -----------------------------------------------------------------------------------------
  24. * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
  25. * SPI SS SDA(SS) 10 53 D10 10 10
  26. * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
  27. * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
  28. * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
  29. -----------------------------------------------------------------
  30. RC522 --> ESP32
  31. SS --> GPIO5
  32. SCK --> GPIO18
  33. MOSI --> GPIO23
  34. MISO --> GPIO19
  35. IRQ --> 中断(可不接)
  36. GND --> GND
  37. RST --> GPIO27(复位)
  38. VCC --> 3.3V
  39. -----------------------------------------------------------------
  40. *"^^^^^^引脚接线说明^^^^^^
  41. * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
  42. */
  43. #include <SPI.h> //包含SPI库
  44. #include <MFRC522.h> //包含MFRC522库
  45. #define SS_PIN 10 //定义引脚,SPI中的SDA(SS)
  46. #define RST_PIN 9 //定义引脚,复位
  47. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class 类的实例
  48. MFRC522::MIFARE_Key key; //使用类 MFRC522 中定义的结构体MIFARE_Key 声明一个结构体变量key,主要是用来存储密钥
  49. // Init array that will store new NUID 将存储新 NUID 的初始化数组
  50. byte nuidPICC[4]; //用来存储NUID
  51. void setup() {
  52. Serial.begin(9600); //初始化串口
  53. SPI.begin(); // Init SPI bus 初始化SPI
  54. rfid.PCD_Init(); // Init MFRC522 初始化MFRC522
  55. for (byte i = 0; i < 6; i++) { //将密钥写入用于存储密钥的数组
  56. key.keyByte[i] = 0xFF; //默认密钥 FF FF FF FF FF FF
  57. }
  58. Serial.println(F("This code scan the MIFARE Classsic NUID.")); //打印“此代码扫描 MIFARE Classsic NUID。”
  59. Serial.print(F("Using the following key:")); //打印“使用以下密钥:”
  60. printHex(key.keyByte, MFRC522::MF_KEY_SIZE); //以十六进制打印密钥,第一个参数为密钥,第二个参数为密钥大小
  61. }
  62. void loop() {
  63. // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  64. // 如果传感器/读卡器上没有新卡,则重置环路。这样可以在空闲时保存整个过程。
  65. if ( ! rfid.PICC_IsNewCardPresent()) //如果没有新卡返回0,因为有运算符!表达式(! rfid.PICC_IsNewCardPresent())为1,执行 return
  66. return;
  67. // Verify if the NUID has been readed 验证是否已读取 NUID
  68. if ( ! rfid.PICC_ReadCardSerial()) //如果没读取NUID返回0,同上
  69. return;
  70. Serial.print(F("PICC type: "));
  71. MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak); //应该是打印IC类型时需要做的一步转换
  72. Serial.println(rfid.PICC_GetTypeName(piccType)); //打印IC卡类型
  73. // Check is the PICC of Classic MIFARE type
  74. // 检查是经典MIFARE类型的外周中心
  75. if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI && //读取到的卡如果不是这3种类型的卡,执行return
  76. piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  77. piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  78. Serial.println(F("Your tag is not of type MIFARE Classic.")); //您的标签不是 MIFARE 经典类型。
  79. return;
  80. }
  81. if (rfid.uid.uidByte[0] != nuidPICC[0] || //比对读取到的卡号是否与之前保存的相同
  82. rfid.uid.uidByte[1] != nuidPICC[1] ||
  83. rfid.uid.uidByte[2] != nuidPICC[2] ||
  84. rfid.uid.uidByte[3] != nuidPICC[3] ) {
  85. Serial.println(F("A new card has been detected.")); //打印“检测到新卡。”
  86. // Store NUID into nuidPICC array 将 NUID 存储到 nuidPICC 数组中
  87. for (byte i = 0; i < 4; i++) {
  88. nuidPICC[i] = rfid.uid.uidByte[i]; //保存卡号到数组
  89. }
  90. Serial.println(F("The NUID tag is:")); //NUID 标记是:
  91. Serial.print(F("In hex: ")); //十六进制
  92. printHex(rfid.uid.uidByte, rfid.uid.size); //以十六进制打印卡号
  93. Serial.println();
  94. Serial.print(F("In dec: ")); //十进制
  95. printDec(rfid.uid.uidByte, rfid.uid.size); //以十进制打印卡号
  96. Serial.println();
  97. }
  98. else Serial.println(F("Card read previously.")); //否则打印“以前读过的卡。”
  99. // Halt PICC 停止PICC
  100. rfid.PICC_HaltA();
  101. // Stop encryption on PCD 停止 PCD 上的加密
  102. rfid.PCD_StopCrypto1();
  103. }
  104. /**
  105. * Helper routine to dump a byte array as hex values to Serial.
  106. 将字节数组作为十六进制值转储到串行的帮助程序例程。
  107. */
  108. void printHex(byte *buffer, byte bufferSize) {
  109. for (byte i = 0; i < bufferSize; i++) {
  110. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  111. Serial.print(buffer[i], HEX);
  112. }
  113. }
  114. /**
  115. * Helper routine to dump a byte array as dec values to Serial.
  116. 将字节数组作为十进制值转储到串行的帮助程序例程。
  117. */
  118. void printDec(byte *buffer, byte bufferSize) {
  119. for (byte i = 0; i < bufferSize; i++) {
  120. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  121. Serial.print(buffer[i], DEC);
  122. }
  123. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号