Arduino:RFID RC522多次停止读卡

时间:2014-04-02 10:29:27

标签: arduino rfid mifare

我刚刚拿到了用于arduino的RC522 RFID卡,并正在使用提供的草图。 MiFare RFID-RC522

我所坚持的是了解在读卡时如何暂停草图,因此只读一次。当卡连接到RDIF阅读器时,草图将保持循环并每次读取卡片。

可以设置延迟,但如果连接时间超过延迟,最终会再次读取卡。

我想要的是能够在卡连接时说出来,只读一次卡ID,然后在卡连接断开时继续草图。

这是主要草图片段:

    void loop()
{

    uchar status;
    uchar str[MAX_LEN];


    // Search card, return card types
    status = MFRC522_Request(PICC_REQIDL, str);
    if (status != MI_OK)
    {
        return;
    }


    // Show card type
    ShowCardType(str);

    //Prevent conflict, return the 4 bytes Serial number of the card
    status = MFRC522_Anticoll(str);

    // str[0..3]: serial number of the card
    // str[4]: XOR checksum of the SN.
    if (status == MI_OK)
    {
        Serial.print("The card's number is: ");
        memcpy(serNum, str, 5);
        ShowCardID(serNum);

        // Check people associated with card ID
        uchar* id = serNum;
        if (id[0] == 0x4B && id[1] == 0xE6 && id[2] == 0xD1 && id[3] == 0x3B) {
            Serial.println("Hello Mary!");
        }
        else if (id[0] == 0x3B && id[1] == 0xE6 && id[2] == 0xD1 && id[3] == 0x3B) {
            Serial.println("Hello Greg!");
        }
        else{
            Serial.println("Hello unkown guy!");
        }
    }


    MFRC522_Halt(); //command the card into sleep mode 

    delay(2000);
}

谢谢大家。

2 个答案:

答案 0 :(得分:1)

通过查看MFRC522_Request(PICC_REQIDL, str)的返回值,我能够解决这个问题。

我创建了RFID_status作为int,并创建了RFID_read作为int并设置为0。

然后我使用RFID_status进行测试,看看是否存在有效的卡连接。 如果是,并且RFID_read仍为0,则读取卡并将RFID_read设置为1.这样,在下一个循环中,卡将不会被读取。

一旦卡被取走,RFID_status将读为0(无连接),RFID_read将被设置为0。

    int RFID_status;
    int RFID_read = 0;    

    void loop()
{

    uchar status;
    uchar str[MAX_LEN];


    // Search card, return card types
    RFID_status = MFRC522_Request(PICC_REQIDL, str);
    if (RFID_status != 1)
    {

         RFID_read 0;

    }


    // Show card type
    ShowCardType(str);

    //Prevent conflict, return the 4 bytes Serial number of the card
    status = MFRC522_Anticoll(str);

    // str[0..3]: serial number of the card
    // str[4]: XOR checksum of the SN.
    if (RFID_status == 1 && RFID_read == 0)
    {
        Serial.print("The card's number is: ");
        memcpy(serNum, str, 5);
        ShowCardID(serNum);
        RFID_read = 1;

        // Check people associated with card ID
        uchar* id = serNum;
        if (id[0] == 0x4B && id[1] == 0xE6 && id[2] == 0xD1 && id[3] == 0x3B) {
            Serial.println("Hello Mary!");
            RFID_read = 1;
        }
        else if (id[0] == 0x3B && id[1] == 0xE6 && id[2] == 0xD1 && id[3] == 0x3B) {
            Serial.println("Hello Greg!");
            RFID_read = 1;
        }
        else{
            Serial.println("Hello unkown guy!");
            RFID_read = 1;
        }
    }


    MFRC522_Halt(); //command the card into sleep mode 

    delay(2000);
}

注意:这个草图中有很多代码我已经拿出来了。我只包含了void循环和我创建的两个全局变量。 原始草图可在此处找到:MiFare RFID-RC522 Sketch

答案 1 :(得分:1)

我是这样做的:

// RFID Includes
#include <SPI.h>
#include <MFRC522.h>

// RFID Defines
#define RST_PIN    9
#define SS_PIN    10

const int NO_CARD_DETECTIONS_BEFORE_NEW_READ = 40;

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance

int noCardCount = 0;

void setup() {
  // This is for Trinket 5V 16MHz
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code

  Serial.begin(9600); // Initialize serial communications with the PC
  while (!Serial); // Do nothing if no serial port is opened (needed for Arduinos based on ATMEGA32U4)
  SPI.begin(); // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522
  Serial.println("Waiting for RFID-chip...");
}

void loop() {
  Serial.println(noCardCount);
  if (mfrc522.PICC_IsNewCardPresent()) {
    if(noCardCount > NO_CARD_DETECTIONS_BEFORE_NEW_READ){
      Serial.println("Card present!");
      //mfrc522.PICC_ReadCardSerial();
      //mfrc522.PICC_HaltA();
    }
    noCardCount = 0;
  }else{ // not present
    noCardCount++;
  }
}

基本上我只计算连续没有卡的频率,当卡片放在读卡器上时,noCardCount会保持低位。只有连续多次没有检测到卡,它才会再次读取。

相关问题