nrf51822 按键蓝牙通知 例程 加入 串口 打印 log

用一个 miniUSB线 连接 板子 和 PC,安装cp2102 win7 64位 驱动
修改 《第三部分:BLE蓝牙实验\BLE实验3:按键蓝牙通知\》 代码
1, 添加 文件
这里写图片描述

该文件位于:

第三部分:BLE蓝牙实验\BLE实验3:按键蓝牙通知\Source\simple_uart\simple_uart.c

然后在 main.c中加入如下代码:

#include "simple_uart.h"

/**@brief  Function for initializing the UART module.
 */
static void uart_init(void)
{
    /**@snippet [UART Initialization] */
    simple_uart_config(RTS_PIN_NUMBER, TX_PIN_NUMBER, CTS_PIN_NUMBER, RX_PIN_NUMBER, HWFC);

    NRF_UART0->INTENSET = UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos;

    NVIC_SetPriority(UART0_IRQn, APP_IRQ_PRIORITY_LOW);
    NVIC_EnableIRQ(UART0_IRQn);
    /**@snippet [UART Initialization] */
}


/**@brief   Function for handling UART interrupts.
 *
 * @details This function will receive a single character from the UART and append it to a string.
 *          The string will be be sent over BLE when the last character received was a 'new line'
 *          i.e '\n' (hex 0x0D) or if the string has reached a length of @ref NUS_MAX_DATA_LENGTH.
 */
void UART0_IRQHandler(void)
{
    static uint8_t data_array[23];
    static uint8_t index = 0;
    uint32_t err_code;

    /**@snippet [Handling the data received over UART] */

    data_array[index] = simple_uart_get();
    index++;

    if ((data_array[index - 1] == '\n') || (index >= (23 - 1)))
    {
        #if 0
        err_code = ble_nus_send_string(&m_nus, data_array, index + 1);
        if (err_code != NRF_ERROR_INVALID_STATE)
        {
            APP_ERROR_CHECK(err_code);
        }
        #endif

        index = 0;
    }

    /**@snippet [Handling the data received over UART] */
}

main函数如下:

int main(void)
{
    // Initialize
    leds_init();
    timers_init();
    gpiote_init();
    buttons_init();
    uart_init();//外设初始化

    ble_stack_init();
    scheduler_init();    
    gap_params_init();
    services_init();
    advertising_init();
    conn_params_init();
    sec_params_init();

    simple_uart_putstring("ble_app_led");  
  // Start execution
    timers_start();

    advertising_start();

    // Enter main loop
    for (;;)
    {
        app_sched_execute();
        power_manage();
    }
}

现在只是能够打印字符串 ,如果打印数字,或者是如何使用 printf ,需要进一步研究

实验的时候,是用在板子的 j2上介入一个miniUSB线 连接到 PC的USB口,
在PC 的win7 64位系统中,安装 cp2102的驱动,然后通过 UartAssist.exe
设置正确的串口号和波特率 38400,就可以看到正确的数据了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值