STM32F302RC用PC4\PC5做串口1,只能发送数据到上位机,无法收到上位机发送的数据。请问这是什么问题?

上位机能收到MCU送上来的数据,MCU无法收到上位机发送的数据,但是PC5脚位上都测试到了上位机送下来的数据信息,只是没有触发串口中断。有没有人遇到过这种问题?哪位大神帮忙指导一下,谢谢。
代码:
void My_USART1_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType =GPIO_OType_PP ;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_7);
GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PA.10 //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//?? //
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//??
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_7);
GPIO_Init(GPIOC, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 115200;
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(USART1, &USART_InitStructure);

USART_Cmd(USART1,ENABLE);//ʹÄÜ´®¿Ú1

USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//¿ªÆô½ÓÊÕÖжÏ

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

}
void USART1_IRQHandler(void)
{
u8 res;

if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
res= USART_ReceiveData(USART1);
USART_SendData(USART1,res);
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
My_USART1_Init();
LED_Init();
delay_init();
while(1)
{
LED_G1(ON);
//USART_SendData(USART1,0x55);
delay_ms(1000);
LED_R1(OFF);
delay_ms(1000);
}
}

第1个回答  2020-04-15
是不是因为没有清除中断标志。追问

清除中断语句是要加在中断处理函数里面吗?之前我有加在中断函数中加过,感觉没有效果。

追答

清除中断语句一般是放在中断处理函数里面。
USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
没遇到 到这种问题,不知道行不行,以前遇到过用串口发送丢第一个数据。

相似回答