為什么要使用app_button來控制uart的開啟和關閉
還是先上datesheet中uart開啟的時候需要HFCLK,需要消耗大量大電流。所以在我們需要的時候需要通過io來通知nrf51822開啟uart的功能。
主要是上個app_button添加
static void button_handler(uint8_t pin_no, uint8_t button_action) { if(button_action == APP_BUTTON_PUSH) { switch(pin_no) { case BUTTON_1: NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos); NRF_UART0->TASKS_STARTRX = 1; NRF_UART0->TASKS_STARTTX = 1; break; } } if(button_action == APP_BUTTON_RELEASE) { switch(pin_no) { case BUTTON_1: NRF_UART0->TASKS_STOPTX = 1; NRF_UART0->TASKS_STOPRX = 1; NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Disabled << UART_ENABLE_ENABLE_Pos); break; } } }
添加uart的相關函數
/**@brief Function for handling app_uart events. * * @details This function will receive a single character from the app_uart module 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. */ /**@snippet [Handling the data received over UART] */ void uart_event_handle(app_uart_evt_t * p_event) { static uint8_t data_temp; uint32_t err_code; switch (p_event->evt_type) { case APP_UART_DATA_READY: UNUSED_VARIABLE(app_uart_get(&data_temp)); app_uart_put(data_temp); 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; } } /**@snippet [Handling the data received over UART] */ /**@brief Function for initializing the UART module. */ /**@snippet [UART Initialization] */ static void uart_init(void) { uint32_t err_code; const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, 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, uart_event_handle, APP_IRQ_PRIORITY_LOW, err_code); APP_ERROR_CHECK(err_code); } /**@snippet [UART Initialization] */