mcu为stm32f407,使用串口2接收不到数据(不用中断),求各路大神指导下新手,搞了好久也不知道哪出错,万分感谢

2019-07-20 23:48发布

void Usart_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);                                //
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);                                //
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);                        //        
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);                        //
        
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3;                                         //
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF ;                                                                        //
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;                                                        //
        GPIO_InitStructure.GPIO_OType=  GPIO_OType_PP ;                                                        //
        GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;                                                                        /
        GPIO_Init(GPIOA,&GPIO_InitStructure);                                                                                                //
        
        USART_InitStructure.USART_BaudRate=115200;                                                                        //
        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(USART2,&USART_InitStructure);                                                                                //
                                                                                                               
        USART_Cmd(USART2,ENABLE);                                                                                                                                                //
}
int main(void)
{
        u8 res;
        Usart_Init();
        while(1)
        {
                if(USART_GetFlagStatus(USART2,USART_FLAG_RXNE))
                {
                                res=USART_ReceiveData(USART2);
                                USART_SendData(USART2,res);
                }
        }
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。