STM32串口1,2,3初始化函数代码(标准库版)
没有使用中断 cks32C8T6、stm32c8t6直接复制使用 其他芯片注意引脚,复用功能什么的
文章目录
STM32串口1,2,3初始化函数代码(标准库版)串口1初始化代码串口2初始化代码串口3初始化代码
串口1初始化代码
void Uart1_init(u32 bound
){
GPIO_InitTypeDef GPIO_InitStructure
;
USART_InitTypeDef USART_InitStructure
;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1
|RCC_APB2Periph_GPIOA
, ENABLE
);
GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_9
;
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_10
;
GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_IN_FLOATING
;
GPIO_Init(GPIOA
, &GPIO_InitStructure
);
USART_InitStructure
.USART_BaudRate
= bound
;
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
;
USART_Init(USART1
, &USART_InitStructure
);
USART_Cmd(USART1
, ENABLE
);
}
串口2初始化代码
void Usart2_Init(u32 bound
)
{
GPIO_InitTypeDef GPIO_InitStructure
;
USART_InitTypeDef USART_InitStructure
;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
|RCC_APB2Periph_AFIO
,ENABLE
);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2
,ENABLE
);
GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_2
;
GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF_PP
;
GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
;
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
);
USART_InitStructure
.USART_BaudRate
= bound
;
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
;
USART_Init(USART2
,&USART_InitStructure
);
USART_Cmd(USART2
,ENABLE
);
}
串口3初始化代码
void USART3_Init(u32 baud
)
{
USART_InitTypeDef USART_InitStructure
;
GPIO_InitTypeDef GPIO_InitStructure
;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB
, ENABLE
);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3
, ENABLE
);
GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_11
;
GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_IN_FLOATING
;
GPIO_Init(GPIOB
, &GPIO_InitStructure
);
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
);
USART_InitStructure
.USART_BaudRate
= baud
;
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
;
USART_Init(USART3
, &USART_InitStructure
);
USART_Cmd(USART3
, ENABLE
);
}