遇到一个很奇怪的问题,STM32F030和传感器通信使用modbus协议,我用串口1中断接收数据,MCU数据发送正常,接收方也有返回数据,但是串口中断进去一次中断之后就不再进去了,求助各位!!!下面是我的代码
void USART_Configuration(void)//串口初始化函数
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
/*
* USART1_TX -> PA9 , USART1_RX -> PA10
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
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_HardwareFlowControl = USART_HardwareFlowControl_None;//设置流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置工作模式
USART_Init(USART1, &USART_InitStructure); //配置入结构体
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1, ENABLE);//使能串口1
}
void USART1_IRQHandler(void) //
{
uint8_t res = 0;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
if(g_ucUSART1RxTOCnt == 0) //接收超时
{
g_ucUSART1RxIdx = 0;
}
g_ucUSART1RxTOCnt = 35; //接收超时: 35ms, 实际介于34~35ms
res = (uint8_t)(USART_ReceiveData(USART1));
g_ucUSART1RxBuf[g_ucUSART1RxIdx] = res;
if(g_ucUSART1RxIdx < (sizeof(g_ucUSART1RxBuf)-1))
{
g_ucUSART1RxIdx++;
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
}
定时器中断是正常的。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>