中级
30 分钟

使用 WT41u 和 ATmega328P 定制化蓝牙解决方案

无线奇迹触手可及

Bluetooth 2 Click with Arduino UNO Rev3

已发布 6月 24, 2024

点击板

Bluetooth 2 Click

开发板

Arduino UNO Rev3

编译器

NECTO Studio

微控制器单元

ATmega328P

准备好革新您的汽车诊断单元了吗?立即添加此蓝牙解决方案吧!

A

A

硬件概览

它是如何工作的?

Bluetooth 2 Click 基于Silicon Labs的WT41u,这是一款完全集成的Bluetooth 2.1 + EDR,Class 1模块。结合板载芯片天线、蓝牙无线电和板载iWRAP蓝牙堆栈,WT41u为具有极高无线电性能或可靠性要求的蓝牙应用提供了出色的链路预算和650米的视距连接。得益于Bluegiga的iWRAP蓝牙堆栈,WT41u实现了13种不同的蓝牙配置文件和Apple iAP连接,确保快速上市。此Click板™是快速集成高性能蓝牙无线技术的理想解决方案,而无需在蓝牙无线电和堆栈开发上

投入数月时间。WT41u使用UART接口与MCU通信,采用RS232协议,具有常用的UART RX、TX和硬件流控制引脚UART CTS和RTS。UART配置参数,如数据速率和数据包格式,通过WT41u软件设置。除了UART接口,还提供了SPI接口,但仅用于系统调试。WT41u使用16位数据和16位地址串行外围接口来编程Flash内存和设置PSKEY配置。CS引脚具有双重功能,可以作为SPI接口的CS引脚或UART的CTS引脚,通过移动标记为COM SEL的SMD跳线到适当位置来

选择。除了常用的接口引脚外,Bluetooth 2 Click还具有通用复位和双向数字/模拟I/O引脚,分别路由到mikroBUS™插座的RST、PWM和AN引脚。此Click板™只能在3.3V逻辑电压水平下运行。在使用具有不同逻辑电平的MCU之前,板上必须执行适当的逻辑电压电平转换。此外,该Click板™配有一个包含函数和示例代码的库,可用作进一步开发的参考。

Bluetooth 2 Click top side image
Bluetooth 2 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

引脚数

28

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™映射器

Analog Signal
PC0
AN
Reset
PD2
RST
Chip Select
PB2
CS
SPI Clock
PB5
SCK
SPI Data OUT
PB4
MISO
SPI Data IN
PB3
MOSI
Power Supply
3.3V
3.3V
Ground
GND
GND
General Purpose I/O
PD6
PWM
UART RTS
PC3
INT
UART TX
PD0
TX
UART RX
PD1
RX
NC
NC
SCL
NC
NC
SDA
NC
NC
5V
Ground
GND
GND
1

“仔细看看!”

Click board™ 原理图

Bluetooth 2 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
Barometer 13 Click front image hardware assembly
Prog-cut hardware assembly
Arduino UNO Rev3 MB 1 - upright/background 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”替换为要显示的参数。

软件支持

库描述

该库包含 Bluetooth 2 Click 驱动程序的 API。

关键功能:

  • bluetooth2_factory_reset - 此函数将设备恢复出厂设置

  • bluetooth2_set_device_name - 此函数设置本地设备名称

  • bluetooth2_generic_write - 此函数使用UART串行接口写入所需数量的数据字节

开源

代码示例

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

/*!
 * @file main.c
 * @brief Bluetooth 2 Click Example.
 *
 * # Description
 * This example reads and processes data from Bluetooth 2 Clicks.
 *
 * The demo application is composed of two sections :
 *
 * ## Application Init
 * Initializes the driver, then performs a factory reset, removes all pairings, and
 * sets the local device name.
 *
 * ## Application Task
 * Logs all the received messages/responses on the USB UART, and if there's any device
 * connected to the Click board the module sends a desired message every 5 seconds back to it.
 *
 * ## Additional Function
 * - static void bluetooth2_clear_app_buf ( void )
 * - static err_t bluetooth2_process ( void )
 * - static err_t bluetooth2_display_rsp ( uint16_t timeout )
 *
 * @note
 * We have used the Serial Bluetooth Terminal smartphone application for the test. 
 * A smartphone and the Click board must be paired in order to exchange messages with each other.
 * 
 * @author Stefan Filipovic
 *
 */

#include "board.h"
#include "log.h"
#include "bluetooth2.h"

#define PROCESS_BUFFER_SIZE 200

#define RSP_OK              "OK."
#define RSP_READY           "READY."
#define RSP_RING            "RING"
#define RSP_NO_CARRIER      "NO CARRIER"

// Local device name.
#define DEVICE_NAME         "Bluetooth 2 Click"

// Message which will be sent to the connected device.
#define TEXT_TO_SEND        "MikroE - Bluetooth 2 Click\r\n" 

// Text sending frequency in miliseconds.
#define SENDING_FREQ        5000  

static bluetooth2_t bluetooth2;
static log_t logger;

static char app_buf[ PROCESS_BUFFER_SIZE ] = { 0 };
static int32_t app_buf_len = 0;
static int32_t app_buf_cnt = 0;
static uint8_t connection_flag = 0;
static uint16_t send_cnt = 0;

/**
 * @brief Bluetooth 2 clearing application buffer.
 * @details This function clears memory of application buffer and reset its length and counter.
 * @note None.
 */
static void bluetooth2_clear_app_buf ( void );

/**
 * @brief Bluetooth 2 data reading function.
 * @details This function reads data from device and concatenates data to application buffer.
 *
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *         @li @c -2 - Application buffer overflow.
 *
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t bluetooth2_process ( void );

/**
 * @brief Bluetooth 2 display response function.
 * @details This function reads data from device until it sends OK or READY message or until
 * it exceeds the timeout value.
 * @param[in] timeout : Timeout value in miliseconds.
 *
 * @return @li @c  0 - Read some data.
 *         @li @c -1 - Nothing is read.
 *
 * See #err_t definition for detailed explanation.
 * @note None.
 */
static err_t bluetooth2_display_rsp ( uint16_t timeout );

void application_init ( void ) 
{
    log_cfg_t log_cfg;                /**< Logger config object. */
    bluetooth2_cfg_t bluetooth2_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 );
    Delay_ms ( 100 );
    log_info( &logger, " Application Init " );

    // Click initialization.

    bluetooth2_cfg_setup( &bluetooth2_cfg );
    BLUETOOTH2_MAP_MIKROBUS( bluetooth2_cfg, MIKROBUS_1 );
    err_t init_flag  = bluetooth2_init( &bluetooth2, &bluetooth2_cfg );
    if ( UART_ERROR == init_flag ) 
    {
        log_error( &logger, " Application Init Error. " );
        log_info( &logger, " Please, run program again... " );

        for ( ; ; );
    }

    bluetooth2_default_cfg ( &bluetooth2 );
    bluetooth2_process( );
    bluetooth2_clear_app_buf( );
    
    log_printf( &logger, " - Factory Reset -\r\n" );
    bluetooth2_factory_reset ( &bluetooth2 );
    bluetooth2_display_rsp ( 2000 ); 
    
    log_printf( &logger, " - Enable OK response -\r\n" );
    bluetooth2_enable_ok_response ( &bluetooth2 );
    bluetooth2_display_rsp ( 1000 ); 
    
    log_printf( &logger, " - Remove Pairings -\r\n" );
    bluetooth2_remove_pairings ( &bluetooth2 );
    bluetooth2_display_rsp ( 1000 ); 
    
    log_printf( &logger, " - Set Device Name -\r\n" );
    bluetooth2_set_device_name ( &bluetooth2, DEVICE_NAME );
    bluetooth2_display_rsp ( 1000 ); 
    
    log_info( &logger, " Application Task " );
}

void application_task ( void ) 
{
    bluetooth2_process();
    if ( app_buf_len > 0 ) 
    {
        Delay_ms ( 200 );
        bluetooth2_process();
        
        if ( strstr( app_buf, RSP_RING ) )
        {
            connection_flag = 1;
            send_cnt = 0;
        }
        if ( strstr( app_buf, RSP_NO_CARRIER ) )
        {
            connection_flag = 0;
            send_cnt = 0;
        }
        log_printf( &logger, "%s", app_buf );
        bluetooth2_clear_app_buf(  );
    }
    
    if ( connection_flag == 1 && send_cnt++ > SENDING_FREQ )
    {
        bluetooth2_generic_write( &bluetooth2, TEXT_TO_SEND, strlen( TEXT_TO_SEND ) );
        send_cnt = 0;
    }
    
    Delay_ms ( 1 );
}

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;
}

static void bluetooth2_clear_app_buf ( void ) 
{
    memset( app_buf, 0, app_buf_len );
    app_buf_len = 0;
    app_buf_cnt = 0;
}

static err_t bluetooth2_process ( void ) 
{
    int32_t rx_size;
    char rx_buff[ PROCESS_BUFFER_SIZE ] = { 0 };

    rx_size = bluetooth2_generic_read( &bluetooth2, rx_buff, PROCESS_BUFFER_SIZE );

    if ( rx_size > 0 ) 
    {
        int32_t buf_cnt = 0;

        if ( app_buf_len + rx_size >= PROCESS_BUFFER_SIZE ) 
        {
            bluetooth2_clear_app_buf(  );
            return BLUETOOTH2_ERROR;
        } 
        else 
        {
            buf_cnt = app_buf_len;
            app_buf_len += rx_size;
        }

        for ( int32_t rx_cnt = 0; rx_cnt < rx_size; rx_cnt++ ) 
        {
            if ( rx_buff[ rx_cnt ] != 0 ) 
            {
                app_buf[ ( buf_cnt + rx_cnt ) ] = rx_buff[ rx_cnt ];
            }
            else
            {
                app_buf_len--;
                buf_cnt--;
            }

        }
        return BLUETOOTH2_OK;
    }
    return BLUETOOTH2_ERROR;
}

static err_t bluetooth2_display_rsp ( uint16_t timeout )
{
    uint16_t timeout_cnt = 0;
    
    bluetooth2_process(  );
    
    while ( ( strstr( app_buf, RSP_OK ) == 0 ) && ( strstr( app_buf, RSP_READY ) == 0 ) && timeout_cnt++ < timeout )
    {
        bluetooth2_process(  );
        Delay_ms ( 1 );
    }
    
    if ( app_buf_len > 0 )
    {
        for ( int32_t buf_cnt = 0; buf_cnt < app_buf_len; buf_cnt++ )
        {
            log_printf( &logger, "%c", app_buf[ buf_cnt ] );
        }
        bluetooth2_clear_app_buf(  );
        log_printf( &logger, "--------------------------------\r\n" );
        return BLUETOOTH2_OK;
    }
    return BLUETOOTH2_ERROR;
}

// ------------------------------------------------------------------------ END

额外支持

资源

喜欢这个项目吗?

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