37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
rfid.halt(); // command the card to enter sleeping state
}
void ShowCardType(unsigned char * type)
{
Serial.print("Card type: ");
if (type[0] == 0x04 && type[1] == 0x00)
Serial.println("MFOne-S50");
else if (type[0] == 0x02 && type[1] == 0x00)
Serial.println("MFOne-S70");
else if (type[0] == 0x44 && type[1] == 0x00)
Serial.println("MF-UltraLight");
else if (type[0] == 0x08 && type[1] == 0x00)
Serial.println("MF-Pro");
else if (type[0] == 0x44 && type[1] == 0x03)
Serial.println("MF Desire");
else
Serial.println("Unknown");
}
After including the RFID library, we need to construct a RFID class object before using the function in RFID
library. Its constructor needs to be written to two pins, respectively to the SDA pin and the RST pin.
In setup, initialize the serial port, SPI and RFID.
Serial.begin(9600);
SPI.begin();
rfid.init(); //initialization
In loop (), use .findCard () waiting for the card approaching. Once it detects card contact, this function will
return MI_OK and save the card type data in parameter str. Then enter the if statement.
if (rfid.findCard(PICC_REQIDL, str) == MI_OK) {
After entering if statement, call the sub function ShowCardType(). Then determine the type of the card
according to the content of STR and print the type out through the serial port.
Then use the.anticoll() to read UID of the card and use serial port to print it out.
25
26
27
28
29
30
31
32
33
if (rfid.anticoll(str) == MI_OK) {
Serial.print("The card's number is : ");
//Display card serial number
for (int i = 0; i < 4; i++) {
Serial.print(0x0F & (str[i] >> 4), HEX);
Serial.print(0x0F & str[i], HEX);
}
Serial.println("");
}