发送中断是可以进去的。
想了很久没想到原因在哪,求帮助。
代码如下:
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Uart2_Init(u32 nBaud)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//
USART_DeInit(USART2);
//
RCC_AHB1PeriphClockCmd(UART2_TX_GPIO_CLK|UART2_RX_GPIO_CLK,ENABLE);
RCC_APB1PeriphClockCmd(UART2_CLK,ENABLE);
//
GPIO_PinAFConfig(UART2_TX_GPIO_PORT,UART2_TX_PIN,GPIO_AF_USART2);
GPIO_PinAFConfig(UART2_RX_GPIO_PORT,UART2_RX_PIN,GPIO_AF_USART2);
//TX
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = UART2_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(UART2_TX_GPIO_PORT,&GPIO_InitStructure);
//RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = UART2_RX_PIN;
GPIO_Init(UART2_RX_GPIO_PORT,&GPIO_InitStructure);
//
USART_InitStructure.USART_BaudRate = nBaud;
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_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_Cmd(USART2, ENABLE);
//·ÀÖ¹µÚÒ»¸öÊý¾ÝÎÞ·¨·¢ËÍÎÊÌâ¡£
//USART_GetITStatus(USART2, USART_FLAG_TC);
}
void USART2_IRQHandler(void)
{
//
if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET)
{
USART_ReceiveData(USART2);
}
//
if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
{
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
//USART_ClearITPendingBit(UART4,USART_IT_TXE);
}
//
if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET)
{
ElemType eDat;
g_TestInfo.nUart2RcvCnt++;
eDat = USART_ReceiveData(USART2) & 0xFF;
QueueIn(Get_Uart2_Rx_Fifo(),eDat);
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
void USART2_IRQHandler( void )
{
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE)!= RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
USART_ReceiveData(USART2);
}
}
可以试试先把发送中断关了,从最简单的开始调试
先把发送中断关了,在中断函数的入口断点看看能不能进入,如果不能那确定就是配置的问题了,找 st 的 demo看看
在TX和RX短接的瞬间会进入到接收中断中去,但是接收的数据与发送的不一致也不固定。(终端1S发送一个0XF0数据)。这么看来发送中断和接收中断是正常的。。
我以为硬件问题,我拿了另外一个板子还是这样。
复用有的,代码是这个:
GPIO_PinAFConfig(UART2_TX_GPIO_PORT,UART2_TX_PIN,GPIO_AF_USART2);
GPIO_PinAFConfig(UART2_RX_GPIO_PORT,UART2_RX_PIN,GPIO_AF_USART2);
问题应该就是这里UART2_TX_PIN,UART2_RX_PIN,复用设置时应该用GPIO_PinSource2,GPIO_PinSource3。
刚才也遇到这个问题,发现是这里错了,给以后的朋友们提个醒。