串口收发程序,只能发送数据不能接收数据,求指导。。。

2019-07-20 13:19发布

#include <stm32f10x.h>


void RCC_Config(void);
void GPIO_Config(void);
void USART_Config(void);
void NVIC_Config(void);

int main(void)
{   

    RCC_Init();
    GPIO_Config();
    NVIC_Config();
    USART_Config();

     while(1);
}
void RCC_Init()
{   

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
}
void GPIO_Config(void)
{
    //configure USARTx_Tx as alternate function push-pull//
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);



    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}
   void NVIC_Config(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}
void USART_Config (void)
{
  USART_InitTypeDef USART_InitStructure;
  USART_InitStructure.USART_BaudRate = 9600;
  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_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能串口的接收中断
  USART_Cmd(USART1,ENABLE);
  USART_Init(USART1, &USART_InitStructure);

}
    串口中断服务程序
void USART1_IRQHandler(void)
{
   if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//判断串口是否产生接收中断
   {


     USART_SendData(USART1,USART_ReceiveData(USART1));

     while (USART_GetFlagStatus (USART1,USART_FLAG_TXE)!=RESET);//判断是否发送成
     USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除中断标志位
   }


}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。