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

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);//清除中断标志位
   }


}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
12条回答
Stephanie
1楼-- · 2019-07-21 05:25
 精彩回答 2  元偷偷看……
Stephanie
2楼-- · 2019-07-21 06:16
 精彩回答 2  元偷偷看……
Stephanie
3楼-- · 2019-07-21 11:42
Stephanie 发表于 2016-8-1 12:07
真的吗??我换个调试助手试试。。。谢谢!操作不当,指的是什么呢??

已经解决了,谢谢了
xinghongyc
4楼-- · 2019-07-21 12:41
Stephanie 发表于 2016-8-1 14:38
已经解决了,谢谢了

解决方法呢
Stephanie
5楼-- · 2019-07-21 18:31
 精彩回答 2  元偷偷看……
whj467467274672
6楼-- · 2019-07-21 23:06
 精彩回答 2  元偷偷看……

一周热门 更多>