[应用相关] stm32f103c8t6串口 usart1只能发送不能接受

[复制链接]
 楼主| 逢dududu必shu 发表于 2022-2-27 22:07 | 显示全部楼层 |阅读模式
这几天一直在搞毕设,需要通过串口用电脑控制单片机,用的是usart1,调了好几天,死活就是只能发送数据,不能接受数据,下面是我的配置函数和串口中断处理函数,求指导!!!

  1. <span style="color: rgb(34, 34, 38); font-family: -apple-system, " sf="" ui="" text",="" arial,="" "pingfang="" sc",="" "hiragino="" sans="" gb",="" "microsoft="" yahei",="" "wenquanyi="" micro="" hei",="" sans-serif,="" simhei,="" simsun;="" font-variant-ligatures:="" common-ligatures;"="">//串口引脚初始化</span>
  2. <span style="color: rgb(34, 34, 38); font-family: -apple-system, " sf="" ui="" text",="" arial,="" "pingfang="" sc",="" "hiragino="" sans="" gb",="" "microsoft="" yahei",="" "wenquanyi="" micro="" hei",="" sans-serif,="" simhei,="" simsun;="" font-variant-ligatures:="" common-ligatures;"="">void Init_Usart(void)</span>
  3. <span style="color: rgb(34, 34, 38); font-family: -apple-system, " sf="" ui="" text",="" arial,="" "pingfang="" sc",="" "hiragino="" sans="" gb",="" "microsoft="" yahei",="" "wenquanyi="" micro="" hei",="" sans-serif,="" simhei,="" simsun;="" font-variant-ligatures:="" common-ligatures;"="">{</span>
  4. <span style="color: rgb(34, 34, 38); font-family: -apple-system, " sf="" ui="" text",="" arial,="" "pingfang="" sc",="" "hiragino="" sans="" gb",="" "microsoft="" yahei",="" "wenquanyi="" micro="" hei",="" sans-serif,="" simhei,="" simsun;="" font-variant-ligatures:="" common-ligatures;"="">GPIO_InitTypeDef GPIO_InitStructure;</span>

  5. <span style="color: rgb(34, 34, 38); font-family: -apple-system, " sf="" ui="" text",="" arial,="" "pingfang="" sc",="" "hiragino="" sans="" gb",="" "microsoft="" yahei",="" "wenquanyi="" micro="" hei",="" sans-serif,="" simhei,="" simsun;="" font-variant-ligatures:="" common-ligatures;"="">RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);</span>
  1. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;               
  2. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         
  3. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
  4. GPIO_Init(GPIOA, &GPIO_InitStructure);                     

  5. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  6. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
  7. GPIO_Init(GPIOA, &GPIO_InitStructure);                  


 楼主| 逢dududu必shu 发表于 2022-2-27 22:08 | 显示全部楼层
  1. }

  2. //串口配置函数
  3. void Usart_Configuration(uint32_t BaudRate)
  4. {
  5. USART_InitTypeDef USART_InitStructure;

  6. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  7. USART_InitStructure.USART_BaudRate =BaudRate ;

  8. USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  9. USART_InitStructure.USART_StopBits = USART_StopBits_1;

  10. USART_InitStructure.USART_Parity = USART_Parity_No ;

  11. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  12. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  13. USART_Init(USART1, &USART_InitStructure);

  14. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

  15. USART_Cmd(USART1, ENABLE);

  16. }

  17. //串口中断处理函数
  18. void USART1_IRQHandler(void)
  19. {
  20. uint8_t ReceiveDate;

  21. if(!(USART_GetITStatus(USART1,USART_IT_RXNE)))
 楼主| 逢dududu必shu 发表于 2022-2-27 22:10 | 显示全部楼层
  1. {
  2.     USART_ClearITPendingBit(USART1,USART_IT_RXNE);  
  3.     ReceiveDate=USART_ReceiveData(USART1);         
  4.     printf("%d",(char *)ReceiveDate);               

  5.     printf("\n\r");                                 
  6. }  
 楼主| 逢dududu必shu 发表于 2022-2-27 22:12 | 显示全部楼层
  1. }
  2. //串口中断NVIC配置函数
  3. void Init_NVIC(void)
  4. {

  5. NVIC_InitTypeDef NVIC_InitStructure;

  6. #ifdef VECT_TAB_RAM
 楼主| 逢dududu必shu 发表于 2022-2-27 22:14 | 显示全部楼层
  1.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);   
  2. #else  

  3.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);                                                  
  4. #endif

  5. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

  6. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;           
  7. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;   
  8. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;         
  9. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;            
  10. NVIC_Init(&NVIC_InitStructure);                                 
  11. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
 楼主| 逢dududu必shu 发表于 2022-2-27 22:14 | 显示全部楼层
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}

调了好几天了,实在是高不出来了,求大神指导!!!!!
diweo 发表于 2022-2-28 10:28 | 显示全部楼层
F103参考手册V10上是这么写的:
RXNE:读数据寄存器非空 (Read data register not empty)
当RDR移位寄存器中的数据被转移到USART_DR寄存器中,该位被硬件置位。如果USART_CR1寄存器中的RXNEIE为1,则产生中断。对USART_DR的读操作可以将该位清零。RXNE位也可以通过写入0来清除,只有在多缓存通讯中才推荐这种清除程序。
0:数据没有收到;
1:收到数据,可以读出。

现在你配置了中断却收不到数据,那就先看RXNE这里有没有置位。
如果RXNE置位了但没中断,那么看看RXNEIE有没有置位。
如果RXNEIE也置位了但没中断,那就再看NVIC相关位有没有置位。


出现问题的时候,不要老盯着代码看,纠结于和例程写的是否一样,而是要学会看寄存器然后分析原因。
香水城 发表于 2022-2-28 10:52 | 显示全部楼层
先不管Printf,你看看你的ReceiveDate里到底有没有数据。

等到ReceiveDate里有数据了再来看看printf。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

69

主题

493

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部

69

主题

493

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部