识别标签ID

来自Microduino Wikipedia
跳转至: 导航搜索

识别标签ID


在下面的示例中,使用Core+核心控制NFC通信模块识别标签ID。

所需硬件


电路搭建

将Battery+、Core+、MCookie_SD、MCookie-NFC堆叠在一起,通过MicroUSB数据线接入电脑。

代码

#include <Microduino_NFC.h> 
 
#define IRQ   (D2) 
 
NFC nfc(IRQ); 
 
void setup(void) 
{ 
    Serial.begin(115200); 
    Serial.println("Hello!"); 
 
    uint32_t versiondata = nfc.begin(); 
 
    if (! versiondata) 
    { 
        Serial.print("Didn't find PN53x board"); 
        while (1); // halt 
    } 
 
    // Got ok data, print it out! 
    Serial.print("Found chip PN5"); 
    Serial.println((versiondata >> 24) & 0xFF, HEX); 
    Serial.print("Firmware ver. "); 
    Serial.print((versiondata >> 16) & 0xFF, DEC); 
    Serial.print('.'); 
    Serial.println((versiondata >> 8) & 0xFF, DEC); 
 
    // Set the max number of retry attempts to read from a card 
    // This prevents us from waiting forever for a card, which is 
    // the default behaviour of the PN532. 
    nfc.setPassiveActivationRetries(0xFF); 
 
    Serial.println("Waiting for an ISO14443A card"); 
} 
 
void loop(void) 
{ 
    boolean success; 
    uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID 
    uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type) 
 
    // Wait for an ISO14443A type cards (Mifare, etc.).  When one is found 
    // 'uid' will be populated with the UID, and uidLength will indicate 
    // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) 
    success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength); 
 
    if (success) 
    { 
        Serial.println("Found a card!"); 
        Serial.print("UID Length: "); 
        Serial.print(uidLength, DEC); 
        Serial.println(" bytes"); 
        Serial.print("UID Value: "); 
        for (uint8_t i = 0; i < uidLength; i++) 
        { 
            Serial.print(" 0x"); 
            Serial.print(uid[i], HEX); 
        } 
        Serial.println(""); 
        // Wait 1 second before continuing 
        delay(1000); 
    } 
    else 
    { 
        // PN532 probably timed out waiting for a card 
        Serial.println("Timed out waiting for a card"); 
    } 
 
}
  • 确认读卡器已经连接好,如图所示。并且将卡片放好,贴紧读卡器。
NFC example1-2a.JPG
  • 下载成功后打开串口监视器,波特率选择115200.
NFC example1.png
  • 可以通过串口观察到返回信息,可以看到标签的UID与UID长度。

到这一步就可以简单的形成一个NFC标签控制应用的基础了,通过识别正确的UID执行对应功能。


返回MCookie-NFC/zh页面