H32V103配置有3個串口,適用于需要同時使用多個串口工作的環境,例如用在一個采用串口屏來顯示工作界面并繪制數據波形曲線、一個串口用來控制數據記錄儀來存儲原始數據、一個串口來控制MP3音頻播放模塊來播報數據或發出語音提示等。 那么這3個串行通訊口都使用哪些引腳呢? 其使用的引腳情況如表1所示:
其中,USART1主要供打印輸出之用,其接口電路如圖1所示。
圖1 串口1接口電路
那么我們如何才能在使用器件少的情況下,來完成同時測試3路串口通信的任務呢? 這里介紹的方法是,讓2路串口進行收發通信,讓另一路串口來輸出信息。 具體的任務分配是: USART1執行老本行,來完成信息輸出的工作;而將USART2和USART3組成一個模擬雙方收發數據的終端。 那完成這一任務都需要哪些器件呢? 一條杜邦線,一個USB轉TTL通信串口模塊及導線,具體的連接形式如圖2所示。 杜邦線的作用是將USART2的TX連接到USART3的RX,這樣就用一條杜邦線連接起了模擬通信的收發雙發。 USB轉TTL通信串口模塊大的作用,則是將USART1的輸出信息傳輸到電腦,并通過串口助手等工具軟件來顯示信息。 當然了,如果你要想令USART2和USART3的地位平等,那也很容易,無非是再添加一條杜邦線,將空置的2個通訊引腳連接起來便是了! 圖2 多串口通信線路連接
那在程序設計上該如何設計呢? 為了便于測試,這里將待發送的信息存入數組中: u8 TxBuffer[] = "Buffer Send fromUSART2 to USART3 by polling!"; 然后通過USARTx_CFG函數對USART2和USART3進行初始化,其內容如下: - void USARTx_CFG(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB1Periph_USART3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB , ENABLE);
- /* USART2 TX-->A.2 RX-->A.3 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* USART3 TX-->B.10 RX-->B.11 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
- USART_Init(USART2, &USART_InitStructure);
- USART_Cmd(USART2, ENABLE);
- USART_Init(USART3, &USART_InitStructure);
- USART_Cmd(USART3, ENABLE);
- }
復制代碼
實現多串口通信測試的主程序如下: - int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
- Delay_Init();
- USART_Printf_Init(115200);
- printf("SystemClk:%d\r\n",SystemCoreClock);
- printf("USART Polling TEST\r\n");
- USARTx_CFG();
- while(TxCnt<TxSize)
- {
- USART_SendData(USART2, TxBuffer[TxCnt++]);
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
- while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
- RxBuffer[RxCnt++] = (USART_ReceiveData(USART3));
- }
- TransferStatus=Buffercmp(TxBuffer,RxBuffer,TxSize);
- if(TransferStatus)
- {
- printf("send success!\r\n");
- printf("TXBuffer: %s \r\n",TxBuffer);
- printf("RxBuffer: %s \r\n",RxBuffer);
- }
- else
- {
- printf("send fail!\r\n");
- printf("TXBuffer: %s \r\n",TxBuffer);
- printf("RxBuffer: %s \r\n",RxBuffer);
- }
- while(1);
- }
復制代碼
其中關鍵的程序段是:while(TxCnt<TxSize) { USART_SendData(USART2,TxBuffer[TxCnt++]); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); RxBuffer[RxCnt++] = (USART_ReceiveData(USART3)); } 它通過定義循環的次數,由USART2把信息發送出去,而與此同時又通過USART3將信息接收回來,最終由。 USART1把接收的信息原樣顯示出來以供比對判別。 經編譯下載后,其運行效果如圖3所示,說明多串口通信是正確的。 若感興趣的話,在此基礎上可以拓展出許多有應用價值的功能。
圖3 多串口通信測試
|