7
RFID
█ www.freenove.com
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
.
(
"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.
5
RFID rfid
(
10
,
9
);
In setup, initialize the serial port, SPI and RFID.
11
12
13
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.
20
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.
23
ShowCardType
(
str
);
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
.
(
"The card's number is : "
);
//Display card serial number
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
Serial
.
(
0x0F
&
(
str
[
i
]
>>
4
),
HEX
);
Serial
.
(
0x0F
&
str
[
i
],
HEX
);
}
Serial
.
println
(
""
);
}