void USART3_Init(u32 baud)
{
GPIO_InitTypeDef GPIO_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
USART_InitTypeDef USART_InitStruct;
//GPIO端口设置
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //使能USART3,GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
//USART3_TX GPIOA.9
//串口使用的GPIO口配置
// Configure USART3 Rx (PB.11) as input floating
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;//GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// Configure USART3 Tx (PB.10) as alternate function push-pull
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStruct);
#ifdef USART3_RS485
//USART3_RS485
GPIO_InitStruct.GPIO_Pin = USART3_RS485_TX_Pin;
GPIO_InitStruct.GPIO_Mode = USART3_RS485_TX_Mode;
GPIO_Init(USART3_RS485_TX_Port, &GPIO_InitStruct);
USART3_RS485_TX_Disable;
#endif
//Usart3 NVIC 配置
NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级3
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; //子优先级3
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStruct); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStruct.USART_BaudRate = baud;//串口波特率
USART_InitStruct.USART_WordLength = USART_WordLength_9b;//字长为8位数据格式
USART_InitStruct.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStruct.USART_Parity = USART_Parity_Even;//无奇偶校验位
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART3, &USART_InitStruct); //初始化串口3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启串口接受中断
USART_Cmd(USART3, ENABLE); //使能串口3
}
2021-10-09
最新推荐文章于 2024-08-10 22:20:04 发布