串口配置程序
我使用的是串口5,UART5,TX为PC12,RX为PD2
- void Usart5_Init(u32 bound)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //1.GPIO口初始化
- USART_InitTypeDef USART_InitStructure; //串口初始化
- NVIC_InitTypeDef NVIC_InitStructure; //中断初始化
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD,ENABLE); //使能GPIOC时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);//使能USART5时钟
-
- //开启串口5对应IO引脚复用映射
- GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
- GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
-
- //IO口初始化配置
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF ;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
- GPIO_Init(GPIOC, &GPIO_InitStructure);
-
- //IO口初始化配置
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF ;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- //GPIO // RS485EN5,此处代码与RS485有关
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;//
- GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIOD
-
- //串口初始化配置
- 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_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
- USART_Init(UART5,&USART_InitStructure);
-
- USART_Cmd(UART5, ENABLE); //使能串口5
-
- USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//开启相关中断
-
- // USART5 NVIC初始化
- NVIC_InitStructure.NVIC_IRQChannel= UART5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 3;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority= 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
-
- NVIC_Init(&NVIC_InitStructure);
-
-
- }
|