H32V103配置有3個(gè)串口,適用于需要同時(shí)使用多個(gè)串口工作的環(huán)境,例如用在一個(gè)采用串口屏來顯示工作界面并繪制數(shù)據(jù)波形曲線、一個(gè)串口用來控制數(shù)據(jù)記錄儀來存儲(chǔ)原始數(shù)據(jù)、一個(gè)串口來控制MP3音頻播放模塊來播報(bào)數(shù)據(jù)或發(fā)出語音提示等。 那么這3個(gè)串行通訊口都使用哪些引腳呢? 其使用的引腳情況如表1所示:
其中,USART1主要供打印輸出之用,其接口電路如圖1所示。
1.jpg (28.63 KB, 下載次數(shù): 94)
下載附件
2020-11-19 21:27 上傳
圖1 串口1接口電路
那么我們?nèi)绾尾拍茉谑褂闷骷俚那闆r下,來完成同時(shí)測試3路串口通信的任務(wù)呢? 這里介紹的方法是,讓2路串口進(jìn)行收發(fā)通信,讓另一路串口來輸出信息。 具體的任務(wù)分配是: USART1執(zhí)行老本行,來完成信息輸出的工作;而將USART2和USART3組成一個(gè)模擬雙方收發(fā)數(shù)據(jù)的終端。 那完成這一任務(wù)都需要哪些器件呢? 一條杜邦線,一個(gè)USB轉(zhuǎn)TTL通信串口模塊及導(dǎo)線,具體的連接形式如圖2所示。 杜邦線的作用是將USART2的TX連接到USART3的RX,這樣就用一條杜邦線連接起了模擬通信的收發(fā)雙發(fā)。 USB轉(zhuǎn)TTL通信串口模塊大的作用,則是將USART1的輸出信息傳輸?shù)诫娔X,并通過串口助手等工具軟件來顯示信息。 當(dāng)然了,如果你要想令USART2和USART3的地位平等,那也很容易,無非是再添加一條杜邦線,將空置的2個(gè)通訊引腳連接起來便是了!
2.jpg (32.09 KB, 下載次數(shù): 78)
下載附件
2020-11-19 21:27 上傳
圖2 多串口通信線路連接
那在程序設(shè)計(jì)上該如何設(shè)計(jì)呢? 為了便于測試,這里將待發(fā)送的信息存入數(shù)組中: u8 TxBuffer[] = "Buffer Send fromUSART2 to USART3 by polling!"; 然后通過USARTx_CFG函數(shù)對(duì)USART2和USART3進(jìn)行初始化,其內(nèi)容如下: - 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);
- }
復(fù)制代碼
實(shí)現(xiàn)多串口通信測試的主程序如下: - 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);
- }
復(fù)制代碼
其中關(guān)鍵的程序段是: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)); } 它通過定義循環(huán)的次數(shù),由USART2把信息發(fā)送出去,而與此同時(shí)又通過USART3將信息接收回來,最終由。 USART1把接收的信息原樣顯示出來以供比對(duì)判別。 經(jīng)編譯下載后,其運(yùn)行效果如圖3所示,說明多串口通信是正確的。 若感興趣的話,在此基礎(chǔ)上可以拓展出許多有應(yīng)用價(jià)值的功能。
3.jpg (41.54 KB, 下載次數(shù): 91)
下載附件
2020-11-19 21:27 上傳
圖3 多串口通信測試
|