串口中断卡住了

[复制链接]
1148|5
 楼主| 大白农 发表于 2015-8-23 09:14 | 显示全部楼层 |阅读模式
执行到USART_ITConfig(USART1, USART_IT_TC, ENABLE);这一步时,程序莫名其妙地卡住了,问一下是什么原因?
  1. #include "stm32f10x.h"

  2. int global = 0;

  3. void uart_init(int bound)
  4. {
  5.         GPIO_InitTypeDef GPIO_InitStructure;
  6.         USART_InitTypeDef USART_InitStructure;
  7.         NVIC_InitTypeDef NVIC_InitStructure;
  8.        
  9.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  10.        
  11.         USART_DeInit(USART1);
  12.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  13.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  15.        
  16.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  17.        
  18.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  19.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  20.        
  21.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  22.        
  23.         USART_InitStructure.USART_BaudRate = bound;
  24.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  25.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  26.         USART_InitStructure.USART_Parity = USART_Parity_No;
  27.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  28.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;       
  29.        
  30.         USART_Init(USART1, &USART_InitStructure);
  31.        
  32.         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;  
  33.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  
  34.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  35.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  36.         NVIC_Init(&NVIC_InitStructure);  
  37.           
  38.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  39.         USART_ITConfig(USART1, USART_IT_TXE, ENABLE);  
  40.                
  41.         USART_Cmd(USART1, ENABLE);
  42. }

  43. void NVIC_Configuration()
  44. {
  45.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  46. }

  47. int main()
  48. {
  49.         NVIC_Configuration();
  50.         uart_init(9600);

  51.         USART_SendData(USART1, global);
  52.         while (1)
  53.         {
  54.                 if (USART_GetFlagStatus(USART1, USART_FLAG_TC) == SET)
  55.                 {
  56.                 USART_SendData(USART1, global);
  57.                 }
  58.         }
  59.        
  60.     return 0;
  61. }

  62. void USART1_IRQHandler()
  63. {
  64.         global++;
  65. }


deliciouscook 发表于 2015-8-23 16:25 | 显示全部楼层
USART_FLAG_TC需要软件清除,你的里面好像没有清除,没有清除就没有等待,发完一个就直接下一个
justperky 发表于 2015-8-24 12:22 | 显示全部楼层
最好还是用USART_FLAG_TXE
低八度的声线 发表于 2015-8-24 17:16 | 显示全部楼层
USART_ClearITPendingBit(USART1, USART_IT_TC);                //清除完成标志
Desire1 发表于 2015-8-24 18:45 | 显示全部楼层
Bit 7 TXE: Transmit data register empty
This bit is set by hardware when the content of the USARTx_TDR register has been
transferred into the shift register. It is cleared by a write to the USARTx_TDR register.
An interrupt is generated if the TXEIE bit =1 in the USARTx_CR1 register.

Bit 6 TC: Transmission complete
This bit is set by hardware if the transmission of a frame containing data is complete and if
TXE is set. An interrupt is generated if TCIE=1 in the USARTx_CR1 register. It is cleared by
software, writing 1 to the TCCF in the USARTx_ICR register or by a write to the
USARTx_TDR register.
An interrupt is generated if TCIE=1 in the USARTx_CR1 register.

好好看看Reference manual吧,只要发送寄存器空,发送完了,这两个标志都是置位的,刚初始化完就是这个状态
北方小榕树 发表于 2015-8-26 22:00 | 显示全部楼层
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);/*等待发送完成*/
USART_FLAG_TC需要软件清除,你的里面好像没有清除,没有清除就没有等待,发完一个就直接下一个。
最好还是用USART_FLAG_TXE
您需要登录后才可以回帖 登录 | 注册

本版积分规则

15

主题

139

帖子

0

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