.RS485——串口2初始化
- void RS485_USART2_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStruct;
- NVIC_InitTypeDef NVIC_InitStruct;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOG,ENABLE);
-
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
- //GPIOA2,A3初始化设置
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用模式
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
- GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//复用模式
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
- GPIO_Init(GPIOG, &GPIO_InitStructure);//初始化
-
-
-
-
- //串口初始化配置
- USART_InitStruct.USART_BaudRate=9600;
- USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
- USART_InitStruct.USART_Parity=USART_Parity_No;
- USART_InitStruct.USART_StopBits=USART_StopBits_1;
- USART_InitStruct.USART_WordLength=USART_WordLength_8b;
- USART_Init(USART2,&USART_InitStruct);
- USART_Cmd(USART2,ENABLE);
-
- USART_ClearFlag(USART2, USART_FLAG_TC);
- USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
-
-
-
- NVIC_InitStruct.NVIC_IRQChannel=USART2_IRQn;
- NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
- NVIC_Init(&NVIC_InitStruct);
- GPIO_ResetBits(GPIOG,GPIO_Pin_8);//初始化为接收状态
- }
|