串口是 单片机中最基本的操作,今天我们就用FireBLE来操作下串口。
实验目的:
使用FireBLE提供的串口,在上位发送一段数字,下位机再把数字返回给上位机。
实验电路:
FireBLE 开发板使用了一个PL2303芯片,把UART0转换到电脑的USB虚拟串口。
实验步骤:
我们还是使用QBlue的QnDriverTools生成GPIO的配置
- // P0.0 -> UART0 [TX] Pull Up Drive ability: Low
- // P0.3 -> GPIO Pull Up Drive ability: Low
- // P0.6 -> SWD [DAT] Pull Up Drive ability: Low
- // P0.7 -> SWD [CLK] Pull Up Drive ability: Low
- // P1.0 -> GPIO Pull Up Drive ability: Low
- // P1.1 -> GPIO Pull Up Drive ability: Low
- // P1.2 -> GPIO Pull Up Drive ability: Low
- // P1.3 -> GPIO Pull Up Drive ability: Low
- // P1.7 -> UART0 [RX] Pull Up Drive ability: Low
- // P2.3 -> GPIO Pull Up Drive ability: Low
- // P2.4 -> GPIO Pull Up Drive ability: Low
- // P2.6 -> GPIO Pull Up Drive ability: Low
- // P2.7 -> GPIO Pull Up Drive ability: Low
- // P3.0 -> GPIO Pull Up Drive ability: Low
- // P3.1 -> GPIO Pull Up Drive ability: Low
- //Pin Mux Control Register
- syscon_SetPMCR0(QN_SYSCON, P00_UART0_TXD_PIN_CTRL
- | P03_GPIO_3_PIN_CTRL
- | P06_SW_DAT_PIN_CTRL
- | P07_SW_CLK_PIN_CTRL
- | P10_GPIO_8_PIN_CTRL
- | P11_GPIO_9_PIN_CTRL
- | P12_GPIO_10_PIN_CTRL
- | P13_GPIO_11_PIN_CTRL
- | P17_UART0_RXD_PIN_CTRL);
- //Pin Mux Control Register
- syscon_SetPMCR1(QN_SYSCON, P23_GPIO_19_PIN_CTRL
- | P24_GPIO_20_PIN_CTRL
- | P26_GPIO_22_PIN_CTRL
- | P27_GPIO_23_PIN_CTRL
- | P30_GPIO_24_PIN_CTRL
- | P31_GPIO_25_PIN_CTRL);
- //Pull Control Register
- syscon_SetPPCR0(QN_SYSCON, 0xAAAAAAAA);
- //Pull Control Register
- syscon_SetPPCR1(QN_SYSCON, 0xAAAAAAAA);
- //Drive ability Control Register
- syscon_SetPDCR(QN_SYSCON, 0x00000000);
复制代码
把这个代码替换掉system.c的SystemIOCfg函数。
在Driver_config.h文件里,我们可以不用的模块定义成FALSE.
主程序代码如下:
- #include "uart.h"
- #include "system.h"
- /*发送接收缓冲*/
- uint8_t buffer[16];
- /*接收完成回调*/
- void rx_callback(void)
- {
- uart_printf(QN_UART0,(uint8_t*)"FireBLE recvive data:n");
- uart_write(QN_UART0,buffer,8,NULL);
- }
- int main(void)
- {
- /*系统初始化*/
- SystemInit();
- /*串口初始化*/
- uart_init(QN_UART0,__USART_CLK,UART_115200);
- uart_tx_enable(QN_UART0,MASK_ENABLE);
- uart_rx_enable(QN_UART0,MASK_ENABLE);
- uart_clock_on(QN_UART0);
-
- uart_printf(QN_UART0,(uint8_t*)"Hello world,I'm form FireBLE.n");
-
- while(true)
- {
- //接收数据
- uart_read(QN_UART0,buffer,8,rx_callback);
- delay(100000);
- }
- }
复制代码
实验现象:
上位机打开串口,复位FireBLE,上位机上显示
上位机发送文本12347890,下位机接收后回送回来如下:
0
|
|
|
|
|
|