void usart2_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2|RCC_APB2Periph_GPIOA, 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 = 115200; /*éèÖÃ2¨ìØÂêÎa115200*/
USART_InitStructure.USART_WordLength = USART_WordLength_8b; /*éèÖÃêy¾YλÎa8λ*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*éèÖÃí£Ö1λÎa1λ*/
USART_InitStructure.USART_Parity = USART_Parity_No; /*ÎTÆæżD£Ñé*/
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; /*ûóDó2¼tá÷¿Ø*/
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*·¢Ëíóë½óêÕ*/
/*íê3é′®¿úCOM1μÄê±ÖóÅäÖá¢GPIOÅäÖ㬸ù¾YéÏêö2Îêy3õê¼»ˉ2¢ê1Äü*/
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_RXNE);
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
此帖出自
小平头技术问答
一周热门 更多>