IC解密H32V103配置有3个串口,适用于需要同时使用多个串口工作的环境,例如用在一个采用串口屏来显示工作界面并绘制数据波形曲线、一个串口用来控制数据记录仪来存储原始数据、一个串口来控制MP3音频播放模块来播报数据或发出语音提示等。
那么这3个串行通讯口都使用哪些引脚呢?
其使用的引脚情况如表1所示:
其中,USART1主要供打印输出之用,其接口电路如图1所示。
图1 串口1接口电路
- 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);
- }