• Hi! Why stm32F103ZET use serial port can't receive and send data?
    The system has been in smooth operation when configuration with USART1 program. The data's receiving and sending are very good. But it went wrong when I replaced usart1 instead of usart2.
    Serial port is not working properly. I don't know what's wrong. Please give me some suggestions.Thank you.

    u8 data;
    int main()
    {

                USART_InitTypeDef USART_InitStruct;
                GPIO_InitTypeDef GPIO_InitStruct;
                 NVIC_InitTypeDef NVIC_InitStruct;
    
          NVIC_PriorityGroupConfig(NVIC_PriorityGr­oup_1);
    
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GP­IOA, ENABLE);
                RCC_APB1PeriphClockCmd(RCC_APB1Periph_US­ART2, ENABLE);
                USART_DeInit(USART2);
    
                USART_InitStruct.USART_BaudRate = 115200;
                USART_InitStruct.USART_HardwareFlowContr­ol = 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);
    
    
    
                GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
                GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOA, &GPIO_InitStruct);
    
                GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
                GPIO_Init(GPIOA, &GPIO_InitStruct);
    
                NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
                NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
                NVIC_InitStruct.NVIC_IRQChannelPreemptio­nPriority = 0;
                NVIC_InitStruct.NVIC_IRQChannelSubPriori­ty = 0;
                NVIC_Init(&NVIC_InitStruct);
    
                 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
                USART_Cmd(USART2, ENABLE);
    
                while(1);
    

    }

    void USART2_IRQHandler(void)
    {

                if(USART_GetFlagStatus(USART2, USART_IT_RXNE) != RESET)
                {
                                USART_ClearFlag(USART2, USART_FLAG_RXNE);
                                data = USART_ReceiveData(USART2);
                                USART_SendData(USART2, data);
                                while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) != SET);
                }
    

    }

    Best regards,

About

Avatar for user67013 @user67013 started