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

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

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

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

 

/*
 * --------------------------------------------------------------------------------------------------------------------
 * Example sketch/program showing how to read new NUID from a PICC to serial.
 * --------------------------------------------------------------------------------------------------------------------
 * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
 * 
 * Example sketch/program showing how to the read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
 * Reader on the Arduino SPI interface.
 * 
 * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
 * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
 * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
 * will show the type, and the NUID if a new card has been detected. Note: you may see "Timeout in communication" messages
 * when removing the PICC from reading distance too early.
 * 
 * @license Released into the public domain.
 * "上面是一些介绍"

 * Typical pin layout used:
 * -----------------------------------------------------------------------------------------
 *             MFRC522      Arduino       Arduino   Arduino    Arduino          Arduino
 *             Reader/PCD   Uno/101       Mega      Nano v3    Leonardo/Micro   Pro Micro
 * Signal      Pin          Pin           Pin       Pin        Pin              Pin
 * -----------------------------------------------------------------------------------------
 * RST/Reset   RST          9             5         D9         RESET/ICSP-5     RST
 * SPI SS      SDA(SS)      10            53        D10        10               10
 * SPI MOSI    MOSI         11 / ICSP-4   51        D11        ICSP-4           16
 * SPI MISO    MISO         12 / ICSP-1   50        D12        ICSP-1           14
 * SPI SCK     SCK          13 / ICSP-3   52        D13        ICSP-3           15

-----------------------------------------------------------------
   RC522      -->      ESP32

   SS         -->      GPIO5
   SCK        -->      GPIO18
   MOSI       -->      GPIO23
   MISO       -->      GPIO19
   IRQ        -->      中断(可不接)
   GND        -->      GND
   RST        -->      GPIO27(复位)
   VCC        -->      3.3V
-----------------------------------------------------------------
 *"^^^^^^引脚接线说明^^^^^^

 * More pin layouts for other boards can be found here: https://github.com/miguelbalboa/rfid#pin-layout
 */

#include <SPI.h>                                                    //包含SPI库
#include <MFRC522.h>                                                //包含MFRC522库

#define SS_PIN 10                                                   //定义引脚,SPI中的SDA(SS)
#define RST_PIN 9                                                   //定义引脚,复位
 
MFRC522 rfid(SS_PIN, RST_PIN);                                      // Instance of the class 类的实例

MFRC522::MIFARE_Key key;                                            //使用类 MFRC522 中定义的结构体MIFARE_Key 声明一个结构体变量key,主要是用来存储密钥

// Init array that will store new NUID 将存储新 NUID 的初始化数组
byte nuidPICC[4];                                                   //用来存储NUID

void setup() { 
  Serial.begin(9600);                                               //初始化串口
  SPI.begin();                                                      // Init SPI bus 初始化SPI
  rfid.PCD_Init();                                                  // Init MFRC522 初始化MFRC522

  for (byte i = 0; i < 6; i++) {                                    //将密钥写入用于存储密钥的数组
    key.keyByte[i] = 0xFF;                                          //默认密钥 FF FF FF FF FF FF
  }

  Serial.println(F("This code scan the MIFARE Classsic NUID."));    //打印“此代码扫描 MIFARE Classsic NUID。”
  Serial.print(F("Using the following key:"));                      //打印“使用以下密钥:”
  printHex(key.keyByte, MFRC522::MF_KEY_SIZE);                      //以十六进制打印密钥,第一个参数为密钥,第二个参数为密钥大小
}
 
void loop() {

  // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
  // 如果传感器/读卡器上没有新卡,则重置环路。这样可以在空闲时保存整个过程。
  if ( ! rfid.PICC_IsNewCardPresent())                              //如果没有新卡返回0,因为有运算符!表达式(! rfid.PICC_IsNewCardPresent())为1,执行 return
    return;

  // Verify if the NUID has been readed 验证是否已读取 NUID
  if ( ! rfid.PICC_ReadCardSerial())                                //如果没读取NUID返回0,同上
    return;

  Serial.print(F("PICC type: "));
  MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);    //应该是打印IC类型时需要做的一步转换
  Serial.println(rfid.PICC_GetTypeName(piccType));                  //打印IC卡类型

  // Check is the PICC of Classic MIFARE type
  // 检查是经典MIFARE类型的外周中心
  if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&                 //读取到的卡如果不是这3种类型的卡,执行return
    piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
    piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
    Serial.println(F("Your tag is not of type MIFARE Classic."));   //您的标签不是 MIFARE 经典类型。
    return;
  }

  if (rfid.uid.uidByte[0] != nuidPICC[0] ||                         //比对读取到的卡号是否与之前保存的相同
    rfid.uid.uidByte[1] != nuidPICC[1] || 
    rfid.uid.uidByte[2] != nuidPICC[2] || 
    rfid.uid.uidByte[3] != nuidPICC[3] ) {
    Serial.println(F("A new card has been detected."));             //打印“检测到新卡。”

    // Store NUID into nuidPICC array 将 NUID 存储到 nuidPICC 数组中
    for (byte i = 0; i < 4; i++) {
      nuidPICC[i] = rfid.uid.uidByte[i];                            //保存卡号到数组
    }
   
    Serial.println(F("The NUID tag is:"));                          //NUID 标记是:
    Serial.print(F("In hex: "));                                    //十六进制
    printHex(rfid.uid.uidByte, rfid.uid.size);                      //以十六进制打印卡号
    Serial.println();
    Serial.print(F("In dec: "));                                    //十进制
    printDec(rfid.uid.uidByte, rfid.uid.size);                      //以十进制打印卡号
    Serial.println();
  }
  else Serial.println(F("Card read previously."));                  //否则打印“以前读过的卡。”

  // Halt PICC 停止PICC
  rfid.PICC_HaltA();

  // Stop encryption on PCD 停止 PCD 上的加密
  rfid.PCD_StopCrypto1();
}


/**
 * Helper routine to dump a byte array as hex values to Serial. 
   将字节数组作为十六进制值转储到串行的帮助程序例程。
 */
void printHex(byte *buffer, byte bufferSize) {                   
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], HEX);
  }
}

/**
 * Helper routine to dump a byte array as dec values to Serial.
   将字节数组作为十进制值转储到串行的帮助程序例程。
 */
void printDec(byte *buffer, byte bufferSize) {
  for (byte i = 0; i < bufferSize; i++) {
    Serial.print(buffer[i] < 0x10 ? " 0" : " ");
    Serial.print(buffer[i], DEC);
  }
}

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值