// 1. 启用串口时钟和GPIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStruct);
// 2. 配置串口引脚
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_USART2); // TX
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_USART2); // RX

// 3. 初始化串口设置
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = BaudRate;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStruct);

// 4. 配置串口中断
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);

USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // 使能接收中断

// 5. 使能串口
USART_Cmd(USART2, ENABLE);
P_decimal = decimal_Firs * 10 + decimal_Sec;
                P_integer = hundreds * 100 + tens * 10 + units;
                P = P_integer + P_decimal / 100;
                flag_P = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[6]='0';
            }
            else if(pRxPacket == 6)
            {
                tens = Serial_RxPacket_USART2[0] - '0';
                units = Serial_RxPacket_USART2[1] - '0';
                
                decimal_Firs = Serial_RxPacket_USART2[3] - '0';
                decimal_Sec = Serial_RxPacket_USART2[4] - '0';
                P_integer = tens * 10 + units;
                P_decimal = decimal_Firs * 10 + decimal_Sec;
                P = P_integer + P_decimal / 100;
                flag_P = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[5]='0' ;
            }
            else if(pRxPacket == 5)
            {
                units = Serial_RxPacket_USART2[0] - '0';
                
                decimal_Firs = Serial_RxPacket_USART2[2] - '0';
                decimal_Sec = Serial_RxPacket_USART2[3] - '0';
                P_integer = units;
                P_decimal = decimal_Firs * 10 + decimal_Sec;
                P = P_integer + P_decimal / 100;
                flag_P = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[4]='0' ;
            }
        }
    }
            
            
    if(RxData == 73)            /*获取VOFA上位机I的值*/
    {
        flag_I = 1;
    }
    else if(flag_I == 1)
    {
        Serial_RxPacket_USART2[pRxPacket] = RxData;
        pRxPacket ++;
        if((Serial_RxPacket_USART2[6] == 64)||(Serial_RxPacket_USART2[5] == 64)||(Serial_RxPacket_USART2[4] == 64))
        {    
            if(pRxPacket == 7)
            {
                hundreds = Serial_RxPacket_USART2[0] - '0';
                tens = Serial_RxPacket_USART2[1] - '0';
                units = Serial_RxPacket_USART2[2] - '0';
                decimal_Firs = Serial_RxPacket_USART2[4] - '0';
                decimal_Sec = Serial_RxPacket_USART2[5] - '0';

                I_decimal = decimal_Firs * 10 + decimal_Sec;
                I_integer = hundreds * 100 + tens * 10 + units;
                I = I_integer + I_decimal / 100;
                flag_I = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[6]='0';
            }
            else if(pRxPacket == 6)
            {
                tens = Serial_RxPacket_USART2[0] - '0';
                units = Serial_RxPacket_USART2[1] - '0';
                
                decimal_Firs = Serial_RxPacket_USART2[3] - '0';
                decimal_Sec = Serial_RxPacket_USART2[4] - '0';
                I_integer = tens * 10 + units;
                I_decimal = decimal_Firs * 10 + decimal_Sec;
                I = I_integer + I_decimal / 100;
                flag_I = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[5]='0' ;
            }
            else if(pRxPacket == 5)
            {
                units = Serial_RxPacket_USART2[0] - '0';
                
                decimal_Firs = Serial_RxPacket_USART2[2] - '0';
                decimal_Sec = Serial_RxPacket_USART2[3] - '0';
                I_integer = units;
                I_decimal = decimal_Firs * 10 + decimal_Sec;
                I = I_integer + I_decimal / 100;
                flag_I = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[4]='0' ;
            }
        }
    }
            
    if(RxData == 68)            /*获取VOFA上位机D的值*/
    {
        flag_D = 1;
    }
    else if(flag_D == 1)
    {
        Serial_RxPacket_USART2[pRxPacket] = RxData;
        pRxPacket ++;
        if((Serial_RxPacket_USART2[6] == 64)||(Serial_RxPacket_USART2[5] == 64)||(Serial_RxPacket_USART2[4] == 64))
        {    
            if(pRxPacket == 7)
            {
                hundreds = Serial_RxPacket_USART2[0] - '0';
                tens = Serial_RxPacket_USART2[1] - '0';
                units = Serial_RxPacket_USART2[2] - '0';
                decimal_Firs = Serial_RxPacket_USART2[4] - '0';
                decimal_Sec = Serial_RxPacket_USART2[5] - '0';

                D_decimal = decimal_Firs * 10 + decimal_Sec;
                D_integer = hundreds * 100 + tens * 10 + units;
                D = D_integer + D_decimal / 100;
                flag_D = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[6]='0';
            }
            else if(pRxPacket == 6)
            {
                tens = Serial_RxPacket_USART2[0] - '0';
                units = Serial_RxPacket_USART2[1] - '0';
                
                decimal_Firs = Serial_RxPacket_USART2[3] - '0';
                decimal_Sec = Serial_RxPacket_USART2[4] - '0';
                D_integer = tens * 10 + units;
                D_decimal = decimal_Firs * 10 + decimal_Sec;
                D = D_integer + D_decimal / 100;
                flag_D = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[5]='0' ;
            }
            else if(pRxPacket == 5)
            {
                units = Serial_RxPacket_USART2[0] - '0';
                
                decimal_Firs = Serial_RxPacket_USART2[2] - '0';
                decimal_Sec = Serial_RxPacket_USART2[3] - '0';
                D_integer = units;
                D_decimal = decimal_Firs * 10 + decimal_Sec;
                D = D_integer + D_decimal / 100;
                flag_D = 0;
                pRxPacket=0;
                Serial_RxPacket_USART2[4]='0' ;
            }
        }
    }
    
            USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
OLED_ShowString(36, 17, "I:    .", 16, 1);
    OLED_ShowNum(49, 17, I, 3, 16, 1, 1);
    OLED_ShowNum(86, 17, I_decimal, 2, 16, 1, 1);
    
    OLED_ShowString(36, 33, "D:    .", 16, 1);
    OLED_ShowNum(49, 33, D, 3, 16, 1, 1);
    OLED_ShowNum(86, 33, D_decimal, 2, 16, 1, 1);
    OLED_Refresh();
    if( mpu_dmp_get_data(&pitch,&roll,&yaw) == 0 )
   
        Send_vofa(roll);            //发送翻滚角为例
    }
}

程序运行成功图片

vofa+上位机,无线调PID(附stm32F407标准库代码)_IT