串口1的中断问题
时间:10-02
整理:3721RD
点击:
串口1的配置与中断函数如下所示。但不知为什么串口的中断进不去。串口2也是这样配置的,但串口2能正常使用。也有可能是下面这个配置错了。弄了两天了,还不知错在哪。求路过的都来看看,帮忙指点一下,在这里先谢谢了
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;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
/* Enable the USART2 Pins Software Remapping */
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);//改变管脚的映射,为串口1
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1, &USART_InitStructure); //初始化外设
/* Enable USART */
USART_Cmd(USART1, ENABLE);//外设使能
USART_ITConfig(USART1,USART_IT_TXE,ENABLE); //发送中断使能
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收中断使能
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//选择串口中断通道1 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式优先级为3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式优先级为1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//--------------中断函数--------------------//
void USART1_IRQHandler(void){ USARTx=USART1; printf("USART1中......\r\n"); if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //检测接收中断是否发生 { /* received data */ USART1_GetInputString(); } /* If overrun condition occurs, clear the ORE flag and recover communication */ if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)//检测接收数据是否溢出 { (void)USART_ReceiveData(USART1); } if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) //检测发送中断是否发生 { /* Write one byte to the transmit data register */ USART1_SendBufferData(); } }
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;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
/* Enable the USART2 Pins Software Remapping */
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);//改变管脚的映射,为串口1
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART Rx as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART1, &USART_InitStructure); //初始化外设
/* Enable USART */
USART_Cmd(USART1, ENABLE);//外设使能
USART_ITConfig(USART1,USART_IT_TXE,ENABLE); //发送中断使能
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收中断使能
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//选择串口中断通道1 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式优先级为3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式优先级为1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//--------------中断函数--------------------//
void USART1_IRQHandler(void){ USARTx=USART1; printf("USART1中......\r\n"); if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //检测接收中断是否发生 { /* received data */ USART1_GetInputString(); } /* If overrun condition occurs, clear the ORE flag and recover communication */ if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)//检测接收数据是否溢出 { (void)USART_ReceiveData(USART1); } if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET) //检测发送中断是否发生 { /* Write one byte to the transmit data register */ USART1_SendBufferData(); } }
问题找出来了,是多开了AFIO的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
改为
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 , ENABLE);
就可以了
学习啦
不好意思,我必须说明一下,我的AFIO时钟在其它地方开了,所以可能是重复开启带来的问题
好好学习