引用了你的代码,我想usart1输入中断后 发送到usart2,然后usart2通过串口助手发送数据给usart1,; usart1的中断程序参照了你的方法,我在usart2那里可以收到;但是usart2发送数据,usart1就没反应。我的usart2中断程序不想要判断什么帧头 帧尾的,想直接收数据。你看看哪里错了啊int main(void) { SystemInit(); Rcc_Configuration(); ////系统时钟配置 NVIC_Configuration(); //中断向量配置 UsartGPIO_Configuration(); USART_Configuration();
while(1) { // USART_SendData(USART1, i); }
} void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; //定义数据结构体 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);//将中断矢量放到Flash的0地址
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//设置优先级配置的模式,详情请阅读原材料中的文章
//使能串口中断,并设置优先级 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //将结构体丢到配置函数,即写入到对应寄存器中
//这个参数设置的是,STM32 MCU中43个中断通道的先占优先级 /* Enable the USART2 Interrupt 串口2 */ NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
} void Rcc_Configuration(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); } 中断: void USART1_IRQHandler(void) { char i=0,j,t; //处理接收到的数据 if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) { /* Clear the USART1 Receive interrupt */ /* USART_ClearITPendingBit(USART1, USART_IT_RXNE);
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); { com1_buffer[i++]=USART_ReceiveData(USART1); } for(j=0;j<=i;j++) USART1_Putc(com1_buffer[j]); */ while(USART1_ReceiveChar()!=0xaa);//寻找帧头 t=USART1_ReceiveChar(); while(t!=0xbb)//若是没到帧尾就继续接收 {com1_buffer[i++]=t; t=USART1_ReceiveChar();} for(j=0;j<(i-1);j++) USART2_Putc(com1_buffer[j]);//把接收到的数据发送出 USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清中断标识
} if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET) //溢出-如果发生溢出需要先读SR,再读DR寄存器 则可清除不断入中断的问题 { USART_ClearFlag(USART1,USART_FLAG_ORE); //读SR USART_ReceiveData(USART1); //读DR }
} 关键是下面的usart2中断,看有问题么 void USART2_IRQHandler(void) { char i=0,j,t; //处理接收到的数据 if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { /* Clear the USART1 Receive interrupt */
t=USART2_ReceiveChar(); while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != SET)//若是没到帧尾就继续接收 {com2_buffer[i++]=t; t=USART2_ReceiveChar();} for(j=0;j<(i-1);j++) USART1_Putc(com2_buffer[j]);//把接收到的数据发送出 USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清中断标识
}
//溢出-如果发生溢出需要先读SR,再读DR寄存器 则可清除不断入中断的问题 if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)==SET) { USART_ClearFlag(USART2,USART_FLAG_ORE); //读SR USART_ReceiveData(USART2); //读DR }
}
请高手指教啊 |