w453509596 发表于 2016-11-25 09:23:47

stm32f091cB串口接收不到数据

小弟最近在使用stm32f091cb单片机,在做产品时需要使用串口,我使用时发现串口接收不到数据,而且而且在串口使能的时候, RXNE志位会被置1,我用了串口接收中断,这样在开中断的时候,程序就会第一次误进入中断,我串使能后,加一条清中断标志的程序,发现这个中断在单步调试时可以清除,但是在全速运行时就清除不了了,但是后通过串口调试助手发送擞数据,就不再会进入串口接收中断了。以下是我的初始化代码,求大神帮我看看。

static void MCU_enableClock(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, ENABLE);
}


static void MCU_debugUsartIOInit(void)
{
        GPIO_InitTypeDefGPIO_InitStructure;
       
        GPIO_PinAFConfig(DEBUGCOM_TX_PORT, DEBUGCOM_TX_PinSource, DEBUGCOM_TX_GPIO_AF);
        GPIO_PinAFConfig(DEBUGCOM_RX_PORT, DEBUGCOM_RX_PinSource, DEBUGCOM_RX_GPIO_AF);
       
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_Pin = DEBUGCOM_TX_Pin;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;
        GPIO_Init(DEBUGCOM_TX_PORT, &GPIO_InitStructure);

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_Pin = DEBUGCOM_RX_Pin;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_2;
        GPIO_Init(DEBUGCOM_RX_PORT, &GPIO_InitStructure);
}



static void HAL_debugUsartInit(void)
{
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDefNVIC_InitStructure;
        USART_DeInit(DEBUG_COM);
       
        USART_InitStructure.USART_BaudRate = 9600;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_Init(DEBUG_COM, &USART_InitStructure);       
        USART_ITConfig(DEBUG_COM, USART_IT_RXNE, ENABLE);       
        USART_Cmd(DEBUG_COM, ENABLE);               
USART_ClearITPendingBit(DEBUG_COM, USART_IT_RXNE);       
       
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
        NVIC_Init(&NVIC_InitStructure);       
}

发表于 2016-11-25 10:00:27

楼主,测试一下接收的IO口是否有干扰。

anobodykey 发表于 2016-11-25 10:21:00

帮顶,帮顶

xhzheng 发表于 2016-11-25 11:03:57

使用设备测量下端口,是否有波形,对波形进行分析看看;

Paderboy 发表于 2016-11-30 11:58:14

直接想用cube配置下串口。。。应该很容易实现

w453509596 发表于 2017-1-5 08:48:51

我自己解决了,谢谢大家,我找到原因了,用STM32的串口需要注意两点:
1.在打开串口后,需要等待串口先发送一帧空闲帧,然后再进行后续的中断配置
2. 如果在程序中,用了写FLASH的代码,或者部分中断的代码执行时间长,最好需要关掉串口的数据溢出检测

aaronhu-172089 发表于 2019-8-2 11:31:15

好奇怪的UART!
页: [1]
查看完整版本: stm32f091cB串口接收不到数据