stm32f103rct6初始化3个串口的代码
时间: 2023-09-16 18:06:34 浏览: 84
好的,以下是初始化3个串口的代码:
首先需要在代码中引入相关头文件:
```
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
```
然后定义各个串口的相关参数:
```
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
#define USART1_RX_GPIO GPIO_Pin_10
#define USART1_TX_GPIO GPIO_Pin_9
#define USART1_GPIO GPIOA
#define USART1 USART1
#define USART1_CLK RCC_APB2Periph_USART1
#define USART1_GPIO_CLK RCC_APB2Periph_GPIOA
#define USART2_RX_GPIO GPIO_Pin_3
#define USART2_TX_GPIO GPIO_Pin_2
#define USART2_GPIO GPIOA
#define USART2 USART2
#define USART2_CLK RCC_APB1Periph_USART2
#define USART2_GPIO_CLK RCC_APB2Periph_GPIOA
#define USART3_RX_GPIO GPIO_Pin_11
#define USART3_TX_GPIO GPIO_Pin_10
#define USART3_GPIO GPIOB
#define USART3 USART3
#define USART3_CLK RCC_APB1Periph_USART3
#define USART3_GPIO_CLK RCC_APB2Periph_GPIOB
```
然后进行初始化:
```
void USARTInit(void){
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = USART1_RX_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART1_TX_GPIO;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
/* USART1 configuration ------------------------------------------------------*/
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_Rx | USART_Mode_Tx;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
/* Configure USART2 Rx (PA3) as input floating */
GPIO_InitStructure.GPIO_Pin = USART2_RX_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);
/* Configure USART2 Tx (PA2) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART2_TX_GPIO;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART2_GPIO, &GPIO_InitStructure);
/* USART2 configuration ------------------------------------------------------*/
USART_InitStructure.USART_BaudRate = 9600;
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_Rx | USART_Mode_Tx;
/* Configure USART2 */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART2 Receive and Transmit interrupts */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* Enable the USART2 */
USART_Cmd(USART2, ENABLE);
/* Configure USART3 Tx (PB10) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART3_TX_GPIO;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART3_GPIO, &GPIO_InitStructure);
/* Configure USART3 Rx (PB11) as input floating */
GPIO_InitStructure.GPIO_Pin = USART3_RX_GPIO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART3_GPIO, &GPIO_InitStructure);
/* USART3 configuration ------------------------------------------------------*/
USART_InitStructure.USART_BaudRate = 4800;
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_Rx | USART_Mode_Tx;
/* Configure USART3 */
USART_Init(USART3, &USART_InitStructure);
/* Enable USART3 Receive and Transmit interrupts */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
/* Enable the USART3 */
USART_Cmd(USART3, ENABLE);
}
```
这样就初始化了3个串口。
阅读全文