用一个 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,就可以看到正确的数据了。