各位大侠,最近我在尝试着编写STM32F103C8T6的中断和串口的程序,两个外部中断用来接受外部的信息,也就是用外部中断来采集序列号,而一个串口1(通过IO映射到PB6--PB7)通过发送字符命令到PC,然后PC返回一个固定的字符串序列号 (串口1通过中断接受序列号),但是当其中一个外部中断有中断时,偶尔会出现串口接受数据错误的现象,但是另外一个中断却不会影响串口接受的数据,这是为什么?函数介绍:
中断初始化以及优先级函数定义:
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //in group_2 style,Preemption and Sub no larger than 3
//
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
中断线定义:
static void EXTILine_Configulation(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_EXTILineConfig(WG1D0_PortSource, WG1D0_PinSource);
EXTI_ClearITPendingBit(EXTI_WG1D0);
EXTI_InitStructure.EXTI_Line = EXTI_WG1D0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
//EXTI_GenerateSWInterrupt(EXTI_WG1D0);
GPIO_EXTILineConfig(WG1D1_PortSource, WG1D1_PinSource);
EXTI_ClearITPendingBit(EXTI_WG1D1);
EXTI_InitStructure.EXTI_Line = EXTI_WG1D1;
EXTI_Init(&EXTI_InitStructure);
//EXTI_GenerateSWInterrupt(EXTI_WG1D1);
GPIO_EXTILineConfig(WG2D0_PortSource, WG2D0_PinSource);
EXTI_ClearITPendingBit(EXTI_WG2D0);
EXTI_InitStructure.EXTI_Line = EXTI_WG2D0;
EXTI_Init(&EXTI_InitStructure);
//EXTI_GenerateSWInterrupt(EXTI_WG2D0);
GPIO_EXTILineConfig(WG2D1_PortSource, WG2D1_PinSource);
EXTI_ClearITPendingBit(EXTI_WG2D1);
EXTI_InitStructure.EXTI_Line = EXTI_WG2D1;
EXTI_Init(&EXTI_InitStructure);
//EXTI_GenerateSWInterrupt(EXTI_WG2D1);
}
串口函数定义:
void COM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(COM1M_GPIO_RCC | COM2S_GPIO_RCC | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(COM1M_RCC, ENABLE);
RCC_APB1PeriphClockCmd(COM2S_RCC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// -通讯端口- //
// 内部通讯端口1(母版和子板通讯端口)
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); // USART1 重映射
GPIO_InitStructure.GPIO_Pin = COM1M_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(COM1M_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = COM1M_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(COM1M_PORT, &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(COM1M, &USART_InitStructure);
// Enable the USART1 Receive Interrupt
USART_ITConfig(COM1M, USART_IT_RXNE, ENABLE);
USART_Cmd(COM1M, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = COM1M_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
串口1中断接受函数:
void COM1M_IRQHandler(void)
{
if(USART_GetITStatus(COM1M, USART_IT_RXNE) != RESET)
{
if(Com1mRxIdx == 0)
{
ComStatus |= 0x01;
ComStatus &= 0xFD;
// TIM_ClearFlag(TIM2, TIM_FLAG_Update);
// TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //启动COM1M超时定时器TIM2
}
// TIM_SetCounter(TIM2, 0);// 重置定时器初值
Com1mRxBuf[Com1mRxIdx++] = USART_ReceiveData(COM1M);
if(Com1mRxIdx>=50)
{
Com1mRxIdx = 0;
}
COM1M_TimeCnt = 0;
Flag=0x00;
/*if(Com1mRxIdx==Com1mRxBuf[2]+5)
{
Flag=0x00;
}*/
}
}
外部中断1接受函数
void EXTI15_10_IRQHandler(void)
{
if(WG1RxIdx == 0)
{
WeiGenFlag |= 0x01;
WeiGenFlag &= 0xFD;
}
if(EXTI_GetITStatus(EXTI_Line10) != RESET )
{
WeiGen1Buf[WG1RxIdx] = 0x00;
WG1RxIdx++;
EXTI_ClearITPendingBit(EXTI_Line10); //清除中断标志
}
if(EXTI_GetITStatus(EXTI_Line11) != RESET )
{
WeiGen1Buf[WG1RxIdx] = 0x01;
WG1RxIdx++;
EXTI_ClearITPendingBit(EXTI_Line11); //清楚中断标志
}
WG1_TimeCnt = 0;
}
外部中断2接受函数
void EXTI9_5_IRQHandler(void)
{
if(WG2RxIdx == 0)
{
WeiGenFlag |= 0x04;
WeiGenFlag &= 0xF7;
}
if(EXTI_GetITStatus(EXTI_Line8) != RESET)
{
WeiGen2Buf[WG2RxIdx] = 0x00;
WG2RxIdx++;
EXTI_ClearITPendingBit(EXTI_Line8); //清除中断标志
}
if(EXTI_GetITStatus(EXTI_Line9) != RESET)
{
WeiGen2Buf[WG2RxIdx] = 0x01;
WG2RxIdx++;
EXTI_ClearITPendingBit(EXTI_Line9); //清除中断标志
}
WG2_TimeCnt = 0;
}
当然还有其他的定时器定义,这里没给出
请各位大侠解答,谢谢大家!!!!!
|