初学STM32,最近在编一个使用USART2的小程序,
问题出在接收数据的地方,目前不使用中断,循环检测RXNE标志然后读数据,数据都能收到,正常。
希望数据处理速度更合理一些,所以希望改成中断方式,但是发现代码进不了接收中断函数,在中断函数里改变一个值,在主函数循环里打印,这个值一直没变。也用JTAG看过,一直没进中断服务函数。
相关代码如下,请高手看看漏了什么,打开中断只调用了一行USART_ITConfig,后面设置优先级调与不调结果都一样
初始化部分:
void uart2_init()
{
USART_InitStructure.USART_BaudRate = 57600;;
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(EVAL_COM2, USART_IT_RXNE, ENABLE);//enable int
STM_EVAL_COMInit(COM2, &USART_InitStructure);//init USART2
NVIC_SetPriority(USART2_IRQn, 0x0);
Uart2_Rx = 0;//debug value, init 0
}
stm32f2xx_it.c......................................
void USART2_IRQHandler()
{ //printf("u2int\r\n");
#if 1
if(USART_GetITStatus(EVAL_COM2, USART_IT_RXNE) != RESET) //?D??2¨²¨¦¨²
{
Uart2_Buffer[Uart2_Rx] = USART_ReceiveData(EVAL_COM2);
Uart2_Rx++;
Uart2_Rx &= 0x3F; //?D??¨º?¡¤???¨ºy¦Ì?¡Á?¡ä¨®
USART_ClearITPendingBit(EVAL_COM2,USART_IT_RXNE); //??3y?D??¡À¨º??.
}
#else
if (USART_GetFlagStatus(EVAL_COM2, USART_FLAG_ORE) != RESET)//¡Á¡é¨°a¡ê?2??¨¹¨º1¨®?if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)¨¤¡ä?D??
{
USART_ReceiveData(EVAL_COM2);
}
if(USART_GetITStatus(EVAL_COM2, USART_IT_RXNE) != RESET)
{
Uart2_Buffer[Uart2_Rx] = USART_ReceiveData(EVAL_COM2);
Uart2_Rx++;
Uart2_Rx &= 0x3F; //?D??¨º?¡¤???¨ºy¦Ì?¡Á?¡ä¨®
// Clear the USART2 Receive interrupt
USART_ClearITPendingBit(EVAL_COM2, USART_IT_RXNE);
}
#endif
}
系统运行起来后,向stm32发送12个字符。没有反应。
|