小弟最近在使用
STM32f091cb
单片机,在做产品时需要使用串口,我使用时发现串口接收不到数据,而且而且在串口使能的时候, RXNE志位会被置1,我用了串口接收中断,这样在开中断的时候,程序就会第一次误进入中断,我串使能后,加一条清中断标志的程序,发现这个中断在单步调试时可以清除,但是在全速运行时就清除不了了,但是后通过串口调试助手发送擞数据,就不再会进入串口接收中断了。以下是我的初始化代码,求大神帮我看看。
sta
tic 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_InitTypeDef GPIO_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_InitTypeDef NVIC_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);
}
1. 在打开串口后,需要等待串口先发送一帧空闲帧,然后再进行后续的中断配置
2. 如果在程序中,用了写FLASH的代码,或者部分中断的代码执行时间长,最好需要关掉串口的数据溢出检测
一周热门 更多>