串行通讯是开发板的重要功能之一,这不仅是因为在调试中需借助它来输出调试信息,而且在应用中也需要它将采集的数据发送到其它的设备上进行数据的共享。 M471开发板共可以提供6个串口,即UART0~UART5。 这里重要对3项内容加以检测,即收发检测、发送字节检测及2串行通讯检测。 1.收发检测 在M471开发板上,UART0是供调试来使用的,为便于使用可将PB13 (UART0_TXD)和 PB12 (UART0_RXD) 交叉与USB转TTL转换模块来连接。 收发检测的主程序和相关函数为: - int32_t main(void)
- {
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, peripheral clock and multi-function I/O */
- SYS_Init();
- /* Lock protected registers */
- SYS_LockReg();
- /* Init UART0 for printf and test */
- UART0_Init();
- printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
- printf("\nUART Sample Program\n");
- /* UART sample function */
- UART_FunctionTest();
- while(1);
- }
- void UART_FunctionTest()
- {
- printf("+-----------------------------------------------------------+\n");
- printf("| UART Function Test |\n");
- printf("+-----------------------------------------------------------+\n");
- printf("| Description : |\n");
- printf("| The sample code will print input char on terminal |\n");
- printf("| Please enter any to start (Press '0' to exit) |\n");
- printf("+-----------------------------------------------------------+\n");
- /*
- Using a RS232 cable to connect UART0 and PC.
- UART0 is set to debug port. UART0 is enable RDA and RLS interrupt.
- When inputting char to terminal screen, RDA interrupt will happen and
- UART0 will print the received char on screen.
- */
- /* Enable UART RDA and THRE interrupt */
- NVIC_EnableIRQ(UART0_IRQn);
- UART_EnableInt(UART0, (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk));
- while(g_bWait);
- /* Disable UART RDA and THRE interrupt */
- UART_DisableInt(UART0, (UART_INTEN_RDAIEN_Msk | UART_INTEN_THREIEN_Msk));
- g_bWait = TRUE;
- printf("\nUART Sample Demo End.\n");
- }
经编译下载,其检测效果如图1所示。在输入字符“A”时,可接收到反馈的字符“A”,收发一致,说明功能正确。 在方式字符“0”的情况下,则退出收发检测,见图2所示。 图1 检测结果 图2 退出检测 2.发送字节测试 在串行通讯中,发送字节数据是十分普遍的,单纯靠收发字符是无法解决的。 为便于检测,可直接在前面的程序上添加语句来测试。 修改后的主程序为: - int32_t main(void)
- {
- SYS_UnlockReg();
- SYS_Init();
- SYS_LockReg();
- UART0_Init();
- printf("\n\nCPU @ %d Hz\n", SystemCoreClock);
- printf("\nUART Sample Program\n");
- UART_Write(UART0, g_u8SendData, 8);
- UART_FunctionTest();
- while(1);
- }
经编译下载,其运行结果如图3所示,这说明使用函数UART_Write()是能够进行字节发送的。 图3 发送字节 3.双串口通信 在例程中有一个验证双串口通信的例程,就是将UART1的数据发送到UART2,或是反向UART2的数据发送到UART1。在测试时,需要将PA2和PB0连接起来。 经实际测试,其结果如图4所示,无论是哪个方向传送其结果均为失败,该结果不免一点遗憾。 图4 双串口通信
|