Difference between revisions of "PN532 NFC HAT"

From Waveshare Wiki
Jump to: navigation, search
Line 71: Line 71:
 
XXXXXXXXXX in response data is ID (3 bytes) and checksum (1 byte) of card.(Please refer to PN532 User Manual InListPassiveTarget Chapter)
 
XXXXXXXXXX in response data is ID (3 bytes) and checksum (1 byte) of card.(Please refer to PN532 User Manual InListPassiveTarget Chapter)
 
==Using demo codes==
 
==Using demo codes==
 +
PN532 NFC HAT supports UART, I2C and SPI interface. You can use them according to your situation.After connecting PN532 NFC HAT (hereafter called as PN532) to Raspberry PI, then set the L1, L0 jumpers and DIP switch for different interfaces.
 +
 +
Choose the interface:
 +
*UART:By default, UART interface of Raspberry Pi is used for Sheell debugging. If you use serial port for degbugging, you need to add an USB to TTL module for communicating just like what we do on [[#Quick test]]. In this case, the serial port is mapped as ttyUSB0 instead of ttyS0
 +
*I2C: Raspberry Pi doesn't supports I2C Clock Stretching. However, PN532 may use Clock Stretching (slaver pull-down SCL pin of I2C interface). Clock Stretching cause that Raspberry Pi cannot control all I2C devices, therefore, if you need to connect other i2C device, we do not recommend you to use I2C interface.
 +
*SPI:We use D4 (BCM) pin as Chip select pin of SPI interface. Take care about GPIO conflict.
 +
===Raspberry Pi examples===
 +
Download demo code from [#Resoucres]], unzip and copy raspberrypi folder to /home/pi of Raspberry Pi. You can firstly copy it to /boot of SD card, then copy it to /home/pi.
 +
;SPI Interface
 +
1. Set L0 toL and L1 to H by jumpers
 +
 +
2. Connect RSTPDN->D20 by jumper
 +
 +
3. Set DIP switch to
 +
{|class="wikitable" width="500px"
 +
|-align="center"
 +
|SCK||MISO||MOSI||NSS||SCL||SDA||RX||TX
 +
|-align="center"
 +
|ON||ON||ON||ON||OFF||OFF||OFF||OFF
 +
|}
 +
:[[File:PN532_NFC_HAT-2.jpg|700px]]
 +
4. Connect PN532 NFC HAT to Raspberry Pi
 +
{|class="wikitable" width="600px"
 +
|+Connect PN532 NFC HAT to Raspberry Pi via SPI interface
 +
!PN532 NFC HAT!!Raspberry Pi (BCM)
 +
|-align="center"
 +
|SCK||SCK
 +
|-align="center"
 +
|MISO||MISO
 +
|-align="center"
 +
|MOSI||MOSI
 +
|-align="center"
 +
|NSS||P4 (D4)
 +
|}
 +
 +
5. Enable SPI interface
 +
 +
Open Terminal of Raspberry Pi,use command: sudo raspi-config
 +
 +
Choose Interfacing Options -> SPI -> Yes
 +
 +
6. Run demo codes(Use example_get_uid.py and rpi_get_uid.c as example)
 +
 +
Open Terminal, navigate to directory of demo codes
 +
cd ~/raspberrypi/
 +
 +
1) python code:
 +
 +
Enter directory of python codes: cd ~/raspberrypi/python/
 +
 +
Modify example_get_uid.py file,set the initialize code as :
 +
pn532 = PN532_SPI(debug=False, reset=20, cs=4)
 +
#pn532 = PN532_I2C(debug=False, reset=20, req=16)
 +
#pn532 = PN532_UART(debug=False, reset=20)
 +
 +
Save after modifying, then run the codes with command:
 +
python3 example_get_uid.py
 +
 +
2) C codes:
 +
 +
Enter directory of c code: cd ~/raspberrypi/c/example/
 +
 +
Modify rpi_get_uid.c file, set initialize code as:
 +
PN532_SPI_Init(&pn532);
 +
//PN532_I2C_Init(&pn532);
 +
//PN532_UART_Init(&pn532);
 +
 +
Save and compile:sudo make
 +
 +
Run the code:
 +
./rpi_get_uid.exe
 +
 +
7. Expected result:Close card to coil part of PN532, the UID of card is read
 +
;UART interface
 +
1. Set L0 to L and L1 to L by jumpers
 +
 +
2. Connecting RSTPDN ->D20 by jumper
 +
 +
3. Set DIP switch to
 +
{|class="wikitable" width="600px"
 +
|-align="center"
 +
|SCK||MISO||MOSI||NSS||SCL||SDA||RX||TX
 +
|-align="center"
 +
|OFF||OFF||OFF||OFF||OFF||OFF||ON||ON
 +
|}
 +
:[[File:PN532_NFC_HAT-3.jpg|700px]]
 +
 +
4. Connect PN532 to Raspberry Pi
 +
{|class="wikitable" width="600px"
 +
|+Connect PN532 NFC HAT to Raspberry Pi via UART interface
 +
!PN532 NFC HAT!!Raspberry Pi
 +
|-align="center"
 +
|RX||TX
 +
|-align="center"
 +
|TX||RX
 +
|}
 +
 +
5. Enable Serial port. By default, serial port is used for Shell debugging.
 +
 +
Open Terminal of Raspberry PI and run command: sudo raspi-config
 +
 +
Choose Interfacing Options-> Serial -> No -> Yes
 +
 +
【Note】You need to restart Raspberry Pi after enabling serial port
 +
 +
6. Run demo codes(Use example_get_uid.py and rpi_get_uid.c as examples)
 +
 +
Open Terminal and navigate to directory of demo codes:
 +
cd ~/raspberrypi/
 +
 +
1) python code:
 +
 +
Enter directory of python code: cd ~/raspberrypi/python/
 +
 +
Modify example_get_uid.py file, set initialize code to:
 +
#pn532 = PN532_SPI(debug=False, reset=20, cs=4)
 +
#pn532 = PN532_I2C(debug=False, reset=20, req=16)
 +
pn532 = PN532_UART(debug=False, reset=20)
 +
 +
Save.Then run code by command:
 +
python3 example_get_uid.py
 +
 +
2) C code:
 +
 +
Enter directory of c code:cd ~/raspberrypi/c/example/
 +
 +
Modify rpi_get_uid.c file, set initialize code to:
 +
//PN532_SPI_Init(&pn532);
 +
//PN532_I2C_Init(&pn532);
 +
PN532_UART_Init(&pn532);
 +
 +
Save then compile code: sudo make
 +
 +
Run code:
 +
./rpi_get_uid.exe
 +
 +
7. Expected result:Close card to coil part of PN532, the UID of card is read
 +
 +
;If demo code of UART run failed, you can test serial port by this example:
 +
 +
cd ~/raspberrypi/python/
 +
python3 example_uart_hex.py
 +
 +
Enter data and sent, data sent and received should be printed on terminal as expected. e.g. sent data below to wake up PN532:
 +
 +
55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF 03 FD D4 14 01 17 00
 +
 +
The response data should be:
 +
 +
00 00 FF 00 FF 00 00 00 FF 02 FE D5 15 16 00
 +
 +
;I2C interface
 +
1. 使用跳线帽,把I0设置为H,l1设置为L
 +
 +
2. 用跳线帽连接RSTPDN ->D20, 连接INT0 -> D16(避免Clock Stretching)
 +
 +
3. 将拨码开关设置为
 +
{|class="wikitable" width="600px"
 +
|-align="center"
 +
|SCK||MISO||MOSI||NSS||SCL||SDA||RX||TX
 +
|-align="center"
 +
|OFF||OFF||OFF||OFF||ON||ON||OFF||OFF
 +
|}
 +
:[[File:PN532_NFC_HAT-3.jpg|700px]]
 +
 +
4. 将PN532 NFC HAT插入到树莓派的40PIN GPIO上
 +
{|class="wikitable" width="600px"
 +
|+PN532 NFC HAT通过I2C引脚连接树莓派
 +
!PN532 NFC HAT!!Raspberry Pi
 +
|-align="center"
 +
|SCL||SCL
 +
|-align="center"
 +
|SDA||SDA
 +
|}
 +
 +
5. 启动树莓派I2C接口。
 +
 +
打开树莓派终端,输入sudo raspi-config 进入配置界面
 +
 +
选择Interfacing Options-> I2C -> Yes
 +
 +
 +
6. 运行示例程序(以example_get_uid.py和rpi_get_uid.c为例)
 +
 +
打开终端,进入到程序目录:
 +
cd ~/raspberrypi/
 +
 +
1) python程序:
 +
 +
进入python程序目录: cd ~/raspberrypi/python/
 +
 +
修改example_get_uid.py文件,初始化pn532对象相关语句改成:
 +
#pn532 = PN532_SPI(debug=False, reset=20, cs=4)
 +
pn532 = PN532_I2C(debug=False, reset=20, req=16)
 +
#pn532 = PN532_UART(debug=False, reset=20)
 +
 +
修改完之后保存,并运行例程
 +
python3 example_get_uid.py
 +
 +
2) C 程序:
 +
 +
进入c程序目录:cd ~/raspberrypi/c/example/
 +
 +
修改rpi_get_uid.c,初始化pn532相关语句改为:
 +
//PN532_SPI_Init(&pn532);
 +
PN532_I2C_Init(&pn532);
 +
//PN532_UART_Init(&pn532);
 +
 +
保存文件,并重新编译:sudo make
 +
 +
运行程序:
 +
./rpi_get_uid.exe
 +
 +
7. 预期结果:将卡贴近模块的线圈部分,可以读取卡的UID信息
 +
===Arduino 程序===
 +
1. 电脑需要安装Arduino IDE。
 +
 +
2. 在Arduino的项目文件夹下(默认为C:\Program Files (x86)\Arduino\libraries,可通过Arduino IDE的文件首选项项目文件夹位置进行指定)创建新文件夹,名字改成pn532。
 +
 +
3. 把 pn532.c、pn532.h、pn532_uno.cpp、pn532_uno.h这四个文件复制到Arduino\libraries\pn532 文件夹下。
 +
 +
4. 示例程序位于examples\arduino目录下。
 +
 +
5. 下面以Arduino UNO 开发板为例
 +
 +
;SPI通信
 +
1. 通过跳线帽,把I0设置为L,l1设置为H
 +
 +
2. 设置拨码开关为:
 +
 +
{|class="wikitable" width="600px"
 +
|-align="center"
 +
|SCK||MISO||MOSI||NSS||SCL||SDA||RX||TX
 +
|-align="center"
 +
|ON||ON||ON||ON||OFF||OFF||OFF||OFF
 +
|}
 +
 +
3. 连接PN532 NFC HAT与Arduino:
 +
 +
{|class="wikitable" width="700px"
 +
|+PN532 NFC HAT通过SPI接口连接Arduino UNO
 +
!PN532 NFC HAT!!Arduino UNO
 +
|-align="center"
 +
|SCK||D13
 +
|-align="center"
 +
|MISO||D12
 +
|-align="center"
 +
|MOSI||D11
 +
|-align="center"
 +
|NSS||D4
 +
|}
 +
:[[File:PN532_NFC_HAT-4.png|800px]]
 +
4. 执行示例程序(以examples\arduino\uno_get_uid\ uno_get_uid.ino 为例):
 +
 +
双击打开 uno_get_uid.ino文件,初始化pn532相关语句改为:
 +
PN532_SPI_Init(&pn532);
 +
//PN532_I2C_Init(&pn532);
 +
编译和上传程序到Arduino UNO
 +
 +
打开串口监视器,按下Arduino UNO的复位键
 +
 +
5. 预期结果: 把卡贴近模块的线圈部分,即可读取卡的UID
 +
 +
;I2C通信
 +
 +
1. 使用跳线帽,把l0设置为L,l1设置为H
 +
 +
2. 拨码开关设置为
 +
 +
{|class="wikitable" width="600px"
 +
|-align="center"
 +
|SCK||MISO||MOSI||NSS||SCL||SDA||RX||TX
 +
|-align="center"
 +
|OFF||OFF||OFF||OFF||ON||ON||OFF||OFF
 +
|}
 +
 +
3. 连接PN532 NFC HAT和Arduino UNO
 +
 +
{|class="wikitable" width="700px"
 +
|+PN532 NFC HAT通过I2C接口连接Arduino UNO
 +
!PN532 NFC HAT!!Arduino UNO
 +
|-align="center"
 +
|SCL||A5
 +
|-align="center"
 +
|SDA||A4
 +
|}
 +
:[[File:PN532_NFC_HAT-5.png|800px]]
 +
 +
4. 执行示例程序(以examples\arduino\uno_get_uid\ uno_get_uid.ino 为例):
 +
 +
双击打开 uno_get_uid.ino文件,初始化pn532相关语句改为:
 +
//PN532_SPI_Init(&pn532);
 +
PN532_I2C_Init(&pn532);
 +
编译和上传程序到Arduino UNO
 +
 +
打开串口监视器,按下Arduino UNO的复位键
 +
 +
5. 预期结果: 把卡贴近模块的线圈部分,即可读取卡的UID
 +
 +
===STM32程序===
 +
示例程序使用的开发板是Open103C,主控芯片是STM32F103CBT6。STM32程序我们只提供SPI通信方式,如果需要其他通信方式,需要自行添加
 +
;下载工程
 +
1. 使用keil打开工程文件(例程\MDK-ARM\pn532_stm32.uvprojx),点击 Rebuild 编译工程。
 +
 +
2. 选择下载器:Options for Target -> Debug选项卡-> Use,默认情况下是 ST-Link Debugger。
 +
 +
3.  选择下载接口:Options for Target -> Debug选项卡 -> Settings -> Debug 选项卡 -> Port,默认情况下是 JTAG.
 +
 +
4. 通过下载器,连接开发板和电脑。
 +
 +
5. 点击 Download 下载工程。
 +
;硬件连接
 +
1. 通过跳线帽,把 I0 设置为 L,I1 设置为 H。
 +
 +
2. 拨码开关设置为
 +
 +
{|class="wikitable" width="600px"
 +
|-align="center"
 +
|SCK||MISO||MOSI||NSS||SCL||SDA||RX||TX
 +
|-align="center"
 +
|ON||ON||ON||ON||OFF||OFF||OFF||OFF
 +
|}
 +
 +
3.  连接PN532 NFC HAT和STM32开发板
 +
 +
{|class="wikitable" width="700px"
 +
|+PN532 NFC HAT通过SPI接口连接STM32
 +
!PN532 NFC HAT!!STM32F103CBT6
 +
|-align="center"
 +
|SCK||PA5
 +
|-align="center"
 +
|MISO||PA6
 +
|-align="center"
 +
|MOSI||PA7
 +
|-align="center"
 +
|NSS||PA4
 +
|}
 +
 +
:[[File:PN532_NFC_HAT-4.png|800px]]
 +
 +
4. 通过USB串口线或者串口模块,连接STM32 开发板的UART1接口(PA9->RX, PA10->TX)到电脑
 +
 +
5. 打开串口监视器,按下STM32开发板的复位键
 +
 +
6. 预期结果: 把卡贴近模块的线圈部分,即可获取卡的 UID。
  
 
==Resources==
 
==Resources==

Revision as of 10:09, 10 August 2019

PN532 NFC HAT
PN532 NFC HAT

PN532 NFC HAT for Raspberry Pi, I2C / SPI / UART
{{{name2}}}

{{{name3}}}

{{{name4}}}

{{{name5}}}

Instruction

This is a Raspberry Pi NFC HAT based on PN532 operating in the 13.56MHz frequency range. It supports three communication interfaces: I2C, SPI, and UART.

NFC (Near Field Communication) is a wireless technology allows contactless point-to-point data communication between devices within a short distance of 10 cm. It is widely used in applications such as access control system, smart tickets, meal card, etc.

Based on the popular NFC controller PN532 with multi interface options, this HAT will easily enable NFC function for your Raspberry Pi.

Features

  • Standard Raspberry Pi 40PIN GPIO extension header, supports Raspberry Pi series boards
  • Onboard PN532 chip, supports various NFC/RFID cards like MIFARE/NTAG2xx, etc.
  • Three interface options: I2C, SPI, and UART, configured via jumpers and switches
  • Breakout control pins, for easily connecting with host boards like STM32/Arduino
  • Comes with development resources and manual (examples for Raspberry Python/C, STM32, Arduino)

Note

  • This module can only be used to write/read NFC card whose password is known, it cannot be used for decrypting encrypted NFC card. For example, the default password of all blocks of Mifare Classic card is 0xFFFFFFFFFFFF. the Mifare card can be written/read only when the default password isn't changed
  • This module cannot be used to copy card, unless that the card use default password.
  • This module cannot be used to simulate NFC card. Because ID of NFC card are 4 bytes, because of security policy of PN532, it will set the first byte of simulate card to 0x08. For more details, please refer to http://www.nfc-tools.org/index.php?title=PN53x%E3%80%82

Quick testing

You can quick test the module by connecting it to PC with USB to TTL module instead of Raspberry Pi

1. Hardware connection
PN532 NFC HAT USB to TTL Module
3V3 3.3V
GND GND
TX RX
RX TX
2. Set L0 to L and L1 to L by jumpers
3. Connect USB to TTL Module to PC by USB cable
4. Open Serial assistant software, set it
  • 波特率(Baud rate):115200
  • 数据位(Data bits):8
  • 停止位(Stop bits):1
  • 校验位(Parity):None
  • 流控制(Flow control):None
5. Check "HEX发送” and “HEX显示”
PN532 NFC HAT.jpg
6. Select correct serial port and open
7. Send data below to wake up FN532 module:
55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF 03 FD D4 14 01 17 00

(Please refer to PN532 User Manual HSU wake up condition Chapter)

The response from PN532 module should be:

00 00 FF 00 FF 00 00 00 FF 02 FE D5 15 16 00
8. Send data below to scan Mifare Classic card(The blue card provided, hereafter called as "card")
00 00 FF 04 FC D4 4A 01 00 E1 00

Closing card to coil part of module, module scan it and response:

00 00 FF 0C F4 D5 4B 01 01 00 04 08 04 XXXXXXXXXX 00

XXXXXXXXXX in response data is ID (3 bytes) and checksum (1 byte) of card.(Please refer to PN532 User Manual InListPassiveTarget Chapter)

Using demo codes

PN532 NFC HAT supports UART, I2C and SPI interface. You can use them according to your situation.After connecting PN532 NFC HAT (hereafter called as PN532) to Raspberry PI, then set the L1, L0 jumpers and DIP switch for different interfaces.

Choose the interface:

  • UART:By default, UART interface of Raspberry Pi is used for Sheell debugging. If you use serial port for degbugging, you need to add an USB to TTL module for communicating just like what we do on #Quick test. In this case, the serial port is mapped as ttyUSB0 instead of ttyS0
  • I2C: Raspberry Pi doesn't supports I2C Clock Stretching. However, PN532 may use Clock Stretching (slaver pull-down SCL pin of I2C interface). Clock Stretching cause that Raspberry Pi cannot control all I2C devices, therefore, if you need to connect other i2C device, we do not recommend you to use I2C interface.
  • SPI:We use D4 (BCM) pin as Chip select pin of SPI interface. Take care about GPIO conflict.

Raspberry Pi examples

Download demo code from [#Resoucres]], unzip and copy raspberrypi folder to /home/pi of Raspberry Pi. You can firstly copy it to /boot of SD card, then copy it to /home/pi.

SPI Interface

1. Set L0 toL and L1 to H by jumpers

2. Connect RSTPDN->D20 by jumper

3. Set DIP switch to

SCK MISO MOSI NSS SCL SDA RX TX
ON ON ON ON OFF OFF OFF OFF
PN532 NFC HAT-2.jpg

4. Connect PN532 NFC HAT to Raspberry Pi

Connect PN532 NFC HAT to Raspberry Pi via SPI interface
PN532 NFC HAT Raspberry Pi (BCM)
SCK SCK
MISO MISO
MOSI MOSI
NSS P4 (D4)

5. Enable SPI interface

Open Terminal of Raspberry Pi,use command: sudo raspi-config

Choose Interfacing Options -> SPI -> Yes

6. Run demo codes(Use example_get_uid.py and rpi_get_uid.c as example)

Open Terminal, navigate to directory of demo codes

cd ~/raspberrypi/

1) python code:

Enter directory of python codes: cd ~/raspberrypi/python/

Modify example_get_uid.py file,set the initialize code as :

pn532 = PN532_SPI(debug=False, reset=20, cs=4)
#pn532 = PN532_I2C(debug=False, reset=20, req=16)
#pn532 = PN532_UART(debug=False, reset=20)

Save after modifying, then run the codes with command:

python3 example_get_uid.py

2) C codes:

Enter directory of c code: cd ~/raspberrypi/c/example/

Modify rpi_get_uid.c file, set initialize code as:

PN532_SPI_Init(&pn532);
//PN532_I2C_Init(&pn532);
//PN532_UART_Init(&pn532);

Save and compile:sudo make

Run the code:

./rpi_get_uid.exe

7. Expected result:Close card to coil part of PN532, the UID of card is read

UART interface

1. Set L0 to L and L1 to L by jumpers

2. Connecting RSTPDN ->D20 by jumper

3. Set DIP switch to

SCK MISO MOSI NSS SCL SDA RX TX
OFF OFF OFF OFF OFF OFF ON ON
PN532 NFC HAT-3.jpg

4. Connect PN532 to Raspberry Pi

Connect PN532 NFC HAT to Raspberry Pi via UART interface
PN532 NFC HAT Raspberry Pi
RX TX
TX RX

5. Enable Serial port. By default, serial port is used for Shell debugging.

Open Terminal of Raspberry PI and run command: sudo raspi-config

Choose Interfacing Options-> Serial -> No -> Yes

【Note】You need to restart Raspberry Pi after enabling serial port

6. Run demo codes(Use example_get_uid.py and rpi_get_uid.c as examples)

Open Terminal and navigate to directory of demo codes:

cd ~/raspberrypi/

1) python code:

Enter directory of python code: cd ~/raspberrypi/python/

Modify example_get_uid.py file, set initialize code to:

#pn532 = PN532_SPI(debug=False, reset=20, cs=4)
#pn532 = PN532_I2C(debug=False, reset=20, req=16)
pn532 = PN532_UART(debug=False, reset=20)

Save.Then run code by command:

python3 example_get_uid.py

2) C code:

Enter directory of c code:cd ~/raspberrypi/c/example/

Modify rpi_get_uid.c file, set initialize code to:

//PN532_SPI_Init(&pn532);
//PN532_I2C_Init(&pn532);
PN532_UART_Init(&pn532);

Save then compile code: sudo make

Run code:

./rpi_get_uid.exe

7. Expected result:Close card to coil part of PN532, the UID of card is read

If demo code of UART run failed, you can test serial port by this example
cd ~/raspberrypi/python/
python3 example_uart_hex.py

Enter data and sent, data sent and received should be printed on terminal as expected. e.g. sent data below to wake up PN532:

55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF 03 FD D4 14 01 17 00

The response data should be:

00 00 FF 00 FF 00 00 00 FF 02 FE D5 15 16 00
I2C interface

1. 使用跳线帽,把I0设置为H,l1设置为L

2. 用跳线帽连接RSTPDN ->D20, 连接INT0 -> D16(避免Clock Stretching)

3. 将拨码开关设置为

SCK MISO MOSI NSS SCL SDA RX TX
OFF OFF OFF OFF ON ON OFF OFF
PN532 NFC HAT-3.jpg

4. 将PN532 NFC HAT插入到树莓派的40PIN GPIO上

PN532 NFC HAT通过I2C引脚连接树莓派
PN532 NFC HAT Raspberry Pi
SCL SCL
SDA SDA

5. 启动树莓派I2C接口。

打开树莓派终端,输入sudo raspi-config 进入配置界面

选择Interfacing Options-> I2C -> Yes


6. 运行示例程序(以example_get_uid.py和rpi_get_uid.c为例)

打开终端,进入到程序目录:

cd ~/raspberrypi/

1) python程序:

进入python程序目录: cd ~/raspberrypi/python/

修改example_get_uid.py文件,初始化pn532对象相关语句改成:

#pn532 = PN532_SPI(debug=False, reset=20, cs=4)
pn532 = PN532_I2C(debug=False, reset=20, req=16)
#pn532 = PN532_UART(debug=False, reset=20)

修改完之后保存,并运行例程

python3 example_get_uid.py

2) C 程序:

进入c程序目录:cd ~/raspberrypi/c/example/

修改rpi_get_uid.c,初始化pn532相关语句改为:

//PN532_SPI_Init(&pn532);
PN532_I2C_Init(&pn532);
//PN532_UART_Init(&pn532);

保存文件,并重新编译:sudo make

运行程序:

./rpi_get_uid.exe

7. 预期结果:将卡贴近模块的线圈部分,可以读取卡的UID信息

Arduino 程序

1. 电脑需要安装Arduino IDE。

2. 在Arduino的项目文件夹下(默认为C:\Program Files (x86)\Arduino\libraries,可通过Arduino IDE的文件首选项项目文件夹位置进行指定)创建新文件夹,名字改成pn532。

3. 把 pn532.c、pn532.h、pn532_uno.cpp、pn532_uno.h这四个文件复制到Arduino\libraries\pn532 文件夹下。

4. 示例程序位于examples\arduino目录下。

5. 下面以Arduino UNO 开发板为例

SPI通信

1. 通过跳线帽,把I0设置为L,l1设置为H

2. 设置拨码开关为:

SCK MISO MOSI NSS SCL SDA RX TX
ON ON ON ON OFF OFF OFF OFF

3. 连接PN532 NFC HAT与Arduino:

PN532 NFC HAT通过SPI接口连接Arduino UNO
PN532 NFC HAT Arduino UNO
SCK D13
MISO D12
MOSI D11
NSS D4
PN532 NFC HAT-4.png

4. 执行示例程序(以examples\arduino\uno_get_uid\ uno_get_uid.ino 为例):

双击打开 uno_get_uid.ino文件,初始化pn532相关语句改为:

PN532_SPI_Init(&pn532);
//PN532_I2C_Init(&pn532);

编译和上传程序到Arduino UNO

打开串口监视器,按下Arduino UNO的复位键

5. 预期结果: 把卡贴近模块的线圈部分,即可读取卡的UID

I2C通信

1. 使用跳线帽,把l0设置为L,l1设置为H

2. 拨码开关设置为

SCK MISO MOSI NSS SCL SDA RX TX
OFF OFF OFF OFF ON ON OFF OFF

3. 连接PN532 NFC HAT和Arduino UNO

PN532 NFC HAT通过I2C接口连接Arduino UNO
PN532 NFC HAT Arduino UNO
SCL A5
SDA A4
PN532 NFC HAT-5.png

4. 执行示例程序(以examples\arduino\uno_get_uid\ uno_get_uid.ino 为例):

双击打开 uno_get_uid.ino文件,初始化pn532相关语句改为:

//PN532_SPI_Init(&pn532);
PN532_I2C_Init(&pn532);

编译和上传程序到Arduino UNO

打开串口监视器,按下Arduino UNO的复位键

5. 预期结果: 把卡贴近模块的线圈部分,即可读取卡的UID

STM32程序

示例程序使用的开发板是Open103C,主控芯片是STM32F103CBT6。STM32程序我们只提供SPI通信方式,如果需要其他通信方式,需要自行添加

下载工程

1. 使用keil打开工程文件(例程\MDK-ARM\pn532_stm32.uvprojx),点击 Rebuild 编译工程。

2. 选择下载器:Options for Target -> Debug选项卡-> Use,默认情况下是 ST-Link Debugger。

3. 选择下载接口:Options for Target -> Debug选项卡 -> Settings -> Debug 选项卡 -> Port,默认情况下是 JTAG.

4. 通过下载器,连接开发板和电脑。

5. 点击 Download 下载工程。

硬件连接

1. 通过跳线帽,把 I0 设置为 L,I1 设置为 H。

2. 拨码开关设置为

SCK MISO MOSI NSS SCL SDA RX TX
ON ON ON ON OFF OFF OFF OFF

3. 连接PN532 NFC HAT和STM32开发板

PN532 NFC HAT通过SPI接口连接STM32
PN532 NFC HAT STM32F103CBT6
SCK PA5
MISO PA6
MOSI PA7
NSS PA4
PN532 NFC HAT-4.png

4. 通过USB串口线或者串口模块,连接STM32 开发板的UART1接口(PA9->RX, PA10->TX)到电脑

5. 打开串口监视器,按下STM32开发板的复位键

6. 预期结果: 把卡贴近模块的线圈部分,即可获取卡的 UID。

Resources

FAQ

Supports

Support

If you require technical support, please go to the Support page and open a ticket.