初学者
10 分钟

使用PN7120和ATmega328组合确保您的数据安全

发现近场通信的“近” - NFC尽在指尖

NFC Click with Arduino UNO Rev3

已发布 6月 24, 2024

点击板

NFC Click

开发板

Arduino UNO Rev3

编译器

NECTO Studio

微控制器单元

ATmega328

有了NFC,您的世界就近在咫尺,我们邀请您拥抱NFC体验,享受更加连接和便捷的生活方式。

A

A

硬件概览

它是如何工作的?

NFC Click基于NXP半导体的PN7120,这是一个具有集成固件和NCI接口的完全符合NFC论坛规范的控制器。PN7120实现了射频和所有低级功能,如天线驱动和接收电路,以实现符合NFC论坛规范的读卡器。它利用出色的调制和解调概念来实现不同的非接触式通信方法和协议。PN7120完全符合许多读写器标准,如ISO 14443A/B,最高可达848 kBit/s,ASK调制下不到25%。NFC Click完全符合NFC论坛的规范,意味着它可以作为NFC读卡器、标签,或与另一台NFC设备建立双向连接。为此,NFC Click可以工作在几种模式下。在卡模拟模式下,它表现为智能卡

或标签。它不发起通信;它只响应NFC读卡器。读/写模式使NFC Click表现为NFC读卡器,在该模式下,它可以与被动标签、智能卡或以卡模拟模式运行的NFC设备进行通信。点对点模式在一对NFC启用设备之间建立双向通信通道。NFC Click使用标准的2线I2C接口与主机MCU通信,并支持标准模式(100MHz)、快速模式(400KHz)和高速模式(3.4MHz)。I2C地址可以通过ADDR SEL跳线选择,默认为0。PN7120具有两种类型的集成存储器:RAM和EEPROM。PN7120的内部寄存器存储配置数据,而专用RF协议的RF配置由EEPROM数据定义,通过

主机MCU发出的命令进行复制。这使用户可以从给定的天线设计中实现最大的RF性能。除了来自mikroBUS™插座的I2C接口信号外,该板还使用了其他几个信号。在mikroBUS™插座的RST引脚上路由的复位引脚提供了通用的复位能力,而mikroBUS™插座的IRT引脚代表了中断请求,以通知主控制器各种事件。此Click board™只能使用3.3V逻辑电压电平进行操作。在使用具有不同逻辑电平的MCU之前,板必须执行适当的逻辑电压电平转换。此外,它配备了包含函数和示例代码的库,可用作进一步开发的参考。

NFC Click top side image
NFC Click bottom side image

功能概述

开发板

Arduino UNO 是围绕 ATmega328P 芯片构建的多功能微控制器板。它为各种项目提供了广泛的连接选项,具有 14 个数字输入/输出引脚,其中六个支持 PWM 输出,以及六个模拟输入。其核心组件包括一个 16MHz 的陶瓷谐振器、一个 USB 连接器、一个电

源插孔、一个 ICSP 头和一个复位按钮,提供了为板 子供电和编程所需的一切。UNO 可以通过 USB 连接到计算机,也可以通过 AC-to-DC 适配器或电池供电。作为第一个 USB Arduino 板,它成为 Arduino 平台的基准,"Uno" 符号化其作为系列首款产品的地

位。这个名称选择,意为意大利语中的 "一",是为了 纪念 Arduino Software(IDE)1.0 的推出。最初与 Arduino Software(IDE)版本1.0 同时推出,Uno 自此成为后续 Arduino 发布的基础模型,体现了该平台的演进。

Arduino UNO Rev3 double side image

微控制器概述 

MCU卡片 / MCU

default

建筑

AVR

MCU 内存 (KB)

32

硅供应商

Microchip

引脚数

32

RAM (字节)

2048

你完善了我!

配件

Click Shield for Arduino UNO 具有两个专有的 mikroBUS™ 插座,使所有 Click board™ 设备能够轻松与 Arduino UNO 板进行接口连接。Arduino UNO 是一款基于 ATmega328P 的微控制器开发板,为用户提供了一种经济实惠且灵活的方式来测试新概念并构建基于 ATmega328P 微控制器的原型系统,结合了性能、功耗和功能的多种配置选择。Arduino UNO 具有 14 个数字输入/输出引脚(其中 6 个可用作 PWM 输出)、6 个模拟输入、16 MHz 陶瓷谐振器(CSTCE16M0V53-R0)、USB 接口、电源插座、ICSP 头和复位按钮。大多数 ATmega328P 微控制器的引脚都连接到开发板左右两侧的 IO 引脚,然后再连接到两个 mikroBUS™ 插座。这款 Click Shield 还配备了多个开关,可执行各种功能,例如选择 mikroBUS™ 插座上模拟信号的逻辑电平,以及选择 mikroBUS™ 插座本身的逻辑电压电平。此外,用户还可以通过现有的双向电平转换电压转换器使用任何 Click board™,无论 Click board™ 运行在 3.3V 还是 5V 逻辑电压电平。一旦将 Arduino UNO 板与 Click Shield for Arduino UNO 连接,用户即可访问数百种 Click board™,并兼容 3.3V 或 5V 逻辑电压电平的设备。

Click Shield for Arduino UNO accessories 1 image

使用的MCU引脚

mikroBUS™映射器

NC
NC
AN
Reset
PD2
RST
NC
NC
CS
NC
NC
SCK
NC
NC
MISO
NC
NC
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
NC
NC
PWM
Interrupt
PC3
INT
NC
NC
TX
NC
NC
RX
I2C Clock
PC5
SCL
I2C Data
PC4
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

NFC Click Schematic schematic

一步一步来

项目组装

Click Shield for Arduino UNO front image hardware assembly

从选择您的开发板和Click板™开始。以Arduino UNO Rev3作为您的开发板开始。

Click Shield for Arduino UNO front image hardware assembly
Arduino UNO Rev3 front image hardware assembly
Charger 27 Click front image hardware assembly
Prog-cut hardware assembly
Board mapper by product8 hardware assembly
Necto image step 2 hardware assembly
Necto image step 3 hardware assembly
Necto image step 4 hardware assembly
Necto image step 5 hardware assembly
Necto image step 6 hardware assembly
Arduino UNO MCU Step hardware assembly
Necto No Display image step 8 hardware assembly
Necto image step 9 hardware assembly
Necto image step 10 hardware assembly
Debug Image Necto Step hardware assembly

实时跟踪您的结果

应用程序输出

1. 应用程序输出 - 在调试模式下,“应用程序输出”窗口支持实时数据监控,直接提供执行结果的可视化。请按照提供的教程正确配置环境,以确保数据正确显示。

2. UART 终端 - 使用UART Terminal通过USB to UART converter监视数据传输,实现Click board™与开发系统之间的直接通信。请根据项目需求配置波特率和其他串行设置,以确保正常运行。有关分步设置说明,请参考提供的教程

3. Plot 输出 - Plot功能提供了一种强大的方式来可视化实时传感器数据,使趋势分析、调试和多个数据点的对比变得更加直观。要正确设置,请按照提供的教程,其中包含使用Plot功能显示Click board™读数的分步示例。在代码中使用Plot功能时,请使用以下函数:plot(insert_graph_name, variable_name);。这是一个通用格式,用户需要将“insert_graph_name”替换为实际图表名称,并将“variable_name”替换为要显示的参数。

软件支持

库描述

这个库包含 NFC Click 驱动程序的 API。

关键函数:

  • nfc_hw_reset - 硬件复位函数。

  • nfc_get_data - 获取数据函数。

  • nfc_cmd_disable_standby_mode - 禁用待机模式命令函数。

开源

代码示例

完整的应用程序代码和一个现成的项目可以通过NECTO Studio包管理器直接安装到NECTO Studio 应用程序代码也可以在MIKROE的GitHub账户中找到。

/*!
 * @file main.c
 * @brief NFC Click example
 *
 * # Description
 * This is an example which demonstrates the usage of NFC Click board.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes driver and logger, then performs hardware reset, puts the device in operating mode by 
 * disabling standby mode, performs test procedure, and configures the device to start discovery.
 *
 * ## Application Task
 * NFC Click board can be used for detection of RFiD tag
 * and displays it's value via USART terminal.
 * All data logs write on USB uart changes for every 1 sec.
 *
 * Additional Functions :
 *  -void display_packet ( control_packet_t *ctrl_pck ) - Display packet log data.
 *  -void display_nfc_data ( control_packet_t *ctrl_pck ) - Display packet log data.
 *  -void nfc_read_nfc_data ( nfc_t *ctx, control_packet_t *ctrl_pck ) - Read nfc data function.
 *  -void nfc_test_antenna ( nfc_t *ctx, control_packet_t *ctrl_pck ) - Testing Antenna function.
 * 
 * @author Stefan Ilic
 *
 */

#include "board.h"
#include "log.h"
#include "nfc.h"

static nfc_t nfc;
static log_t logger;
uint8_t n_cnt;

control_packet_t ctrl_pck_data;

/**
 * @brief NFC display packet function.
 * @details This function displays data values.
 */
void display_packet ( control_packet_t *ctrl_pck );

/**
 * @brief NFC display tag info.
 * @details This function displays tag info data.
 */
void nfc_print_info (  control_packet_t *ctrl_pck );

/**
 * @brief NFC display nfc data function.
 * @details This function displays nfc data values.
 */
void display_nfc_data ( control_packet_t *ctrl_pck );

/**
 * @brief NFC read nfc data function.
 * @details This function reads nfc data and displays data.
 */
void nfc_read_nfc_data ( nfc_t *ctx, control_packet_t *ctrl_pck );

/**
 * @brief NFC test antena function.
 * @details This function tests antenna and displays data.
 */
void nfc_test_antenna ( nfc_t *ctx, control_packet_t *ctrl_pck );

void application_init ( void )
{
    log_cfg_t log_cfg;  /**< Logger config object. */
    nfc_cfg_t nfc_cfg;  /**< Click config object. */

    /** 
     * Logger initialization.
     * Default baud rate: 115200
     * Default log level: LOG_LEVEL_DEBUG
     * @note If USB_UART_RX and USB_UART_TX 
     * are defined as HAL_PIN_NC, you will 
     * need to define them manually for log to work. 
     * See @b LOG_MAP_USB_UART macro definition for detailed explanation.
     */
    LOG_MAP_USB_UART( log_cfg );
    log_init( &logger, &log_cfg );
    log_info( &logger, " Application Init " );

    // Click initialization.
    nfc_cfg_setup( &nfc_cfg );
    NFC_MAP_MIKROBUS( nfc_cfg, MIKROBUS_1 );
    err_t init_flag = nfc_init( &nfc, &nfc_cfg );
    if ( I2C_MASTER_ERROR == init_flag ) 
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }

    log_printf( &logger, "        HW Reset       \r\n" );
    nfc_hw_reset( &nfc );
    Delay_ms ( 100 );

    log_printf( &logger, "-----------------------\r\n" );
    log_printf( &logger, " Reset and Init. Core  \r\n" );
    nfc_cmd_core_reset( &nfc );
    Delay_ms ( 100 );

    nfc_read_ctrl_packet_data( &nfc, &ctrl_pck_data );
    Delay_ms ( 100 );

    nfc_cmd_core_init( &nfc );
    Delay_ms ( 100 );

    nfc_read_ctrl_packet_data( &nfc, &ctrl_pck_data );
    Delay_ms ( 100 );
    display_packet( &ctrl_pck_data );

    while ( nfc_check_irq( &nfc ) == NFC_IRQ_STATE_HIGH );

    log_printf( &logger, "-----------------------\r\n" );
    log_printf( &logger, " Disabling Standby Mode \r\n" );
    nfc_cmd_disable_standby_mode( &nfc );
    Delay_ms ( 100 );

    nfc_read_ctrl_packet_data( &nfc, &ctrl_pck_data );
    Delay_ms ( 100 );
    display_packet( &ctrl_pck_data );

    nfc_test_antenna( &nfc, &ctrl_pck_data );

    log_printf( &logger, "-----------------------\r\n" );
    log_printf( &logger, "Starting Test Procedure\r\n" );
    nfc_cmd_test_procedure( &nfc );
    Delay_ms ( 100 );

    nfc_read_ctrl_packet_data( &nfc, &ctrl_pck_data );
    Delay_ms ( 100 );
    display_packet( &ctrl_pck_data );

    nfc_hw_reset( &nfc );
    Delay_ms ( 100 );

    log_printf( &logger, "-----------------------\r\n" );
    log_printf( &logger, "       NFC Config.     \r\n" );
    nfc_default_cfg ( &nfc, &ctrl_pck_data );

    log_printf( &logger, "-----------------------\r\n" );
    log_printf( &logger, "     Discovery Start   \r\n" );
    nfc_cmd_start_discovery( &nfc );
    Delay_ms ( 100 );

    nfc_read_ctrl_packet_data( &nfc, &ctrl_pck_data );
    Delay_ms ( 100 );
    display_packet( &ctrl_pck_data );

    log_printf( &logger, "-----------------------\r\n" );
    log_printf( &logger, "-------- START --------\r\n" );
    log_printf( &logger, "-----------------------\r\n" );
    Delay_ms ( 500 );

    log_info( &logger, " Application Task " );
}

void application_task ( void )
{
    while ( nfc_check_irq( &nfc ) == NFC_IRQ_STATE_HIGH ) 
    {
        nfc_read_nfc_data ( &nfc, &ctrl_pck_data );
    }

    while ( nfc_check_irq( &nfc ) == NFC_IRQ_STATE_LOW );

    log_printf( &logger, "-----------------------\r\n" );
    Delay_ms ( 1000 );
}

int main ( void ) 
{
    /* Do not remove this line or clock might not be set correctly. */
    #ifdef PREINIT_SUPPORTED
    preinit();
    #endif
    
    application_init( );
    
    for ( ; ; ) 
    {
        application_task( );
    }

    return 0;
}

void display_packet ( control_packet_t *ctrl_pck ) 
{
    log_printf( &logger, "- - - - - - - - - - - -\r\n" );
    log_printf( &logger, " Message Type   = %d\r\n", ( uint16_t ) ctrl_pck->message_type );
    log_printf( &logger, " Pck Bound Flag = %d\r\n", ( uint16_t ) ctrl_pck->pck_bound_flag );
    log_printf( &logger, " Group Ident    = %d\r\n", ( uint16_t ) ctrl_pck->group_ident );
    log_printf( &logger, " Opcode Ident   = %d\r\n", ( uint16_t ) ctrl_pck->opcode_ident );
    log_printf( &logger, " Payload Length = %d\r\n", ( uint16_t ) ctrl_pck->payload_length );
    log_printf( &logger, "- - - - - - - - - - - -\r\n" );

    for ( n_cnt = 0; n_cnt < ctrl_pck_data.payload_length; n_cnt++ ) 
    {
        log_printf( &logger, " Payload[ %.2d ]  = 0x%.2X\r\n", ( uint16_t ) n_cnt, ( uint16_t ) ctrl_pck_data.payload[ n_cnt ]  );
    }

    log_printf( &logger, "- - - - - - - - - - - -\r\n" );
    memset( ctrl_pck_data.payload, 0x00, 255 );
}

void nfc_print_info ( control_packet_t *ctrl_pck )
{
    log_printf( &logger, "    NFC Tag info    \r\n" );
    log_printf( &logger, "- - - - - - - - - - - -\r\n" );
    log_printf( &logger, " Serial number = %.2X:%.2X:%.2X:%.2X\r\n", 
                ( uint16_t ) ctrl_pck_data.payload[ 10 ], ( uint16_t ) ctrl_pck_data.payload[ 11 ], 
                ( uint16_t ) ctrl_pck_data.payload[ 12 ], ( uint16_t ) ctrl_pck_data.payload[ 13 ] );
    log_printf( &logger, " ATQA = 0x%.2X%.2X\r\n",  ( uint16_t ) ctrl_pck_data.payload[ 8 ], 
                ( uint16_t ) ctrl_pck_data.payload[ 9 ]  );
    log_printf( &logger, " SAK = 0x%.2X\r\n",  ( uint16_t ) ctrl_pck_data.payload[ 15 ]  );
    
}

void display_nfc_data ( control_packet_t *ctrl_pck ) 
{
    log_printf( &logger, "- - - - - - - - - - - -\r\n");
    log_printf( &logger, " Read Block:\r\n");

    for ( n_cnt = 0; n_cnt < ctrl_pck->payload_length; n_cnt++ )
    {
        log_printf( &logger, "\t 0x%.2X \r\n", ( uint16_t ) ctrl_pck->payload[ n_cnt ] );
    }
    log_printf( &logger, "\t 0x%.2X \r\n", ( uint16_t ) ctrl_pck->payload[ ctrl_pck->payload_length - 2 ] );

    log_printf( &logger, "- - - - - - - - - - - -\r\n" );
    memset( ctrl_pck->payload, 0x00, 255 );
}

void nfc_read_nfc_data ( nfc_t *ctx, control_packet_t *ctrl_pck )
{
    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    nfc_print_info( ctrl_pck );
    Delay_ms ( 100 );

    nfc_activate_rmt_mifare_card( ctx );
    Delay_ms ( 100 );
    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    Delay_ms ( 10 );

    while ( nfc_check_irq( ctx ) == NFC_IRQ_STATE_LOW );

    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    nfc_cmd_authenticate_sector( ctx, 0x30 );
    Delay_ms ( 100 );
    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    Delay_ms ( 10 );

    while ( nfc_check_irq( ctx ) == NFC_IRQ_STATE_LOW );

    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    display_nfc_data( ctrl_pck );
    log_printf( &logger, "    Disconnect Card    \r\n" );
    nfc_cmd_card_disconnected( ctx );
    Delay_ms ( 10 );
    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    Delay_ms ( 10 );

    while ( nfc_check_irq( ctx ) == NFC_IRQ_STATE_LOW );

    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    Delay_ms ( 100 );
}

void nfc_test_antenna ( nfc_t *ctx, control_packet_t *ctrl_pck )
{
    log_printf( &logger, "-----------------------\r\n" );
    log_printf( &logger, "    Testing Antenna    " );
    nfc_cmd_antenna_test( ctx, 0x01 );
    Delay_ms ( 100 );

    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    Delay_ms ( 100 );

    nfc_cmd_antenna_test( ctx, 0x07 );
    Delay_ms ( 100 );
    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    Delay_ms ( 100 );

    nfc_cmd_antenna_test( ctx, 0x0F );
    Delay_ms ( 100 );
    nfc_read_ctrl_packet_data( ctx, ctrl_pck );
    Delay_ms ( 100 );
    display_packet( ctrl_pck );
}
// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

'购买此套件' 按钮会直接带您进入购物车,您可以在购物车中轻松添加或移除产品。