最近编写了STM32F407 USART1的串口,在进入USART1接收中断的时候没有反应。后用keil调试的时候发现,卡在了 B 这个位置,入下图所示:
串口1的配置代码如下:
void uart1_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure_USART1;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//ê1ÄüUSART1ê±Öó
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9¸′óÃÎaUSART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10¸′óÃÎaUSART1
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9óëGPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA9£¬
A10
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = 115200;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú1
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != SET)
;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure_USART1.NVIC_IRQChannel = USART1_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure_USART1.NVIC_IRQChannelPreemptionPriority=0x03;//ÇàÕ¼óÅÏ輶2
NVIC_InitStructure_USART1.NVIC_IRQChannelSubPriority =0x02; //×óóÅÏ輶3
NVIC_InitStructure_USART1.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure_USART1); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}
void USART1_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
USART_SendData(USART1,Res);
}
}
除了串口1之外,我也配置了串口2和串口3,代码如下:
void
USART2_Gpio_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // tx
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // rx
}
void
USART3_Gpio_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); // tx
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); // rx
}
/*
* USART2 is used for receiving commands from PC and
* printing debug information to PC
*/
void
USART2_Config(void)
{
USART2_Gpio_Config();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitTypeDef USART_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(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) != SET)
;
}
/*
* USART3 is used for communicating with the DJI flight controller
* The Baud rate needs to match the Baud rate used by the flight controller
*/
void
USART3_Config(void)
{
USART3_Gpio_Config();
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
USART_InitStructure.USART_BaudRate = 230400;
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(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) != SET)
;
}
void
USARTxNVIC_Config()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure_USART3;
NVIC_InitStructure_USART3.NVIC_IRQChannelPreemptionPriority = 0x04;
NVIC_InitStructure_USART3.NVIC_IRQChannelSubPriority = 0x03;
NVIC_InitStructure_USART3.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure_USART3.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure_USART3);
NVIC_InitTypeDef NVIC_InitStructure_USART2;
NVIC_InitStructure_USART2.NVIC_IRQChannelPreemptionPriority = 0x03;
NVIC_InitStructure_USART2.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure_USART2.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure_USART2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure_USART2);
//Usart1 NVIC ÅäÖÃ
NVIC_InitTypeDef NVIC_InitStructure_USART1;
NVIC_InitStructure_USART1.NVIC_IRQChannel = USART1_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure_USART1.NVIC_IRQChannelPreemptionPriority=0x03;//ÇàÕ¼óÅÏ輶2
NVIC_InitStructure_USART1.NVIC_IRQChannelSubPriority =0x02; //×óóÅÏ輶3
NVIC_InitStructure_USART1.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure_USART1); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}
每次执行代码都无法进入串口1的接收中断,希望有大神可以帮我解答一下!!!
十分感谢!!!
一周热门 更多>