STM32F205 串口无法接收数据

2019-03-23 19:12发布

现象如题.
发送中断是可以进去的。
想了很久没想到原因在哪,求帮助。
代码如下:
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);
//·àÖ1μúò»¸öêy¾YÎT·¨·¢ËíÎêìa¡£
//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);
}

} 此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
further_away
1楼-- · 2019-03-24 01:36
/ 求帮助!!!!
zxh623
2楼-- · 2019-03-24 07:08
 精彩回答 2  元偷偷看……

一周热门 更多>