STM32使用串口二遇到的问题

2019-07-15 12:31发布

uart.c里面
void USART2_Init( u32 bound )
{
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /* Enable the USART2 Pins Software Remapping */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
        USART_DeInit(USART2);


    /* Configure USART2 Rx (PA.03) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;   
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Configure USART2 Tx (PA.02) as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* Enable the USART2 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);   

    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_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;        

    USART_Init(USART2, &USART_InitStructure);
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
    //USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
    /* Enable USART2 */
    USART_Cmd(USART2, ENABLE);
}

void USART2_IRQHandler(void)   
{  
        u8 Res;
        if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
        {
        Res =USART_ReceiveData(USART2);//(USART1->DR);        //读取接收到的数据        
        if((USART_RX_STA&0x8000)==0)//接收未完成
                {
                if(USART_RX_STA&0x4000)//接收到了0x0d
                        {
                        if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
                        else USART_RX_STA|=0x8000;        //接收完成了
                        }
                else //还没收到0X0D
                        {        
                        if(Res==0x0d)USART_RX_STA|=0x4000;
                        else
                                {
                                USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
                                USART_RX_STA++;
                                if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收         
                                }                 
                        }         
                }  
                        
    }
}


通过USART_SendData(USART2,0x16);发送数据  串口没收到
而用串口1就可以USART_SendData(USART1,0x16);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。