nrf51822 button Bluetooth notification routine is added to serial port to print log

Connect the board and the PC with a miniUSB cable, install the cp2102 win7 64-bit driver, and
modify the code 1 of "Part 3: BLE Bluetooth Experiment\BLE Experiment 3: Button Bluetooth Notification\" Code
1, add a file
write picture description here

The file is located at:

Part 3: BLE Bluetooth Experiment\BLE Experiment 3: Button Bluetooth Notification\Source\simple_uart\simple_uart.c

Then add the following code to 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] */
}

The main function is as follows:

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

Now it is only able to print strings, if it prints numbers, or how to use printf, further research is needed

In the experiment, a miniUSB cable is used on the j2 of the board to connect to the USB port of
the PC. In the win7 64-bit system of the PC, install the driver of cp2102, and then
set the correct serial port number and baud rate through UartAssist.exe 38400, you can see the correct data.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326693537&siteId=291194637