nrf51822 --- 串口通信

来源:互联网 发布:淘宝所有订单不全 编辑:程序博客网 时间:2024/06/07 03:20

1.目的

     串口通信,用于打印log,方便测试

2.分析

    串口通信,用于打印log,方便测试

3.平台:

协议栈版本:SDK10.0.0

编译软件:keil 5.12

硬件平台:微雪开发板nrf51822

例子:SDK 10.0.0\SDK10.0\examples\ble_peripheral\ble_app_hrs\pca10028\s110\arm4

4.步骤

 1.查看main()函数里面的

   app_trace_init();

跳进去可以看到: 函数没有具体的内容。查看可以知道是没有定义ENABLE_DEBUG_LOG_SUPPORT 

#else // ENABLE_DEBUG_LOG_SUPPORT#define app_trace_init(...)#define app_trace_log(...)#define app_trace_dump(...)#endif // ENABLE_DEBUG_LOG_SUPPORT/** @} */


2.在app_trace.h里面定义定义宏

#define  ENABLE_DEBUG_LOG_SUPPORT  1

3.app_trace.c里面定义 添加 #include  “app_trace.h”

#include <stdio.h>#include <stdint.h>#include <string.h>#include <stdarg.h>#include "app_trace.h" //add

4.查看app_trace_init初始化:

void app_trace_init(void){    uint32_t err_code = NRF_SUCCESS;    const app_uart_comm_params_t comm_params =      {        RX_PIN_NUMBER,  //定义RX脚        TX_PIN_NUMBER, //定义TX脚        RTS_PIN_NUMBER,         CTS_PIN_NUMBER,         APP_UART_FLOW_CONTROL_DISABLED,  //流控关闭        false,           UART_BAUDRATE_BAUDRATE_Baud38400 //选择波特率    };             APP_UART_FIFO_INIT(&comm_params,                        UART_RX_BUF_SIZE,                        UART_TX_BUF_SIZE,        <strong>                 uart_event_handle, //uart_error_handle,     这里把uart_errror_handle改为uart_event_handle</strong>                       APP_IRQ_PRIORITY_LOW,                       err_code);    UNUSED_VARIABLE(err_code);}

uart_event_handle代码如下:

void uart_event_handle(app_uart_evt_t * p_event){    static uint8_t data_array[20];    static uint8_t index = 0;    uint32_t       err_code;    switch (p_event->evt_type)    {        case APP_UART_DATA_READY:            UNUSED_VARIABLE(app_uart_get(&data_array[index]));<strong>//接收数据地方</strong>    printf("data_array[0]: %x \r\n",data_array[0]);//printf("data_array[1]: %x \r\n",data_array[1]); //}       //SEGGER_RTT_printf(0,"data_array: %x \r\n",data_array[0]);//            index++;//            if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))//            {//               err_code = ble_nus_string_send(&m_nus, data_array, index);//                if (err_code != NRF_ERROR_INVALID_STATE)//                {//                    APP_ERROR_CHECK(err_code);//                }//                //                index = 0;//            }            break;        case APP_UART_COMMUNICATION_ERROR:            APP_ERROR_HANDLER(p_event->data.error_communication);            break;        case APP_UART_FIFO_ERROR:            APP_ERROR_HANDLER(p_event->data.error_code);            break;        default:            break;    }}




主函数代码如下:

int main(void){    uint32_t err_code;    bool erase_bonds;    // Initialize.    app_trace_init();    timers_init();    buttons_leds_init(&erase_bonds);    ble_stack_init();    device_manager_init(erase_bonds);    gap_params_init();    advertising_init();    services_init();    sensor_simulator_init();    conn_params_init();    // Start execution.    application_timers_start();    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);    APP_ERROR_CHECK(err_code);    app_trace_log("hello world");    // Enter main loop.    for (;;)    {        power_manage();    }}

测试打印结果

 hello world


发送和接收一样

ok!!测试ok


 

0 0