金牌会员
 
- 积分
- 1180
- 金钱
- 1180
- 注册时间
- 2018-9-11
- 在线时间
- 132 小时
|
30金钱
我在STM32F407(探索者开发板)上同时使用串口2,串口3和串口5,配置好后运行代码发现无法进入串口3的接收中断,但是串口2可以进入中断,有没有大神可以帮我看看,我把配置串口的代码放在下面。
void USART2_init(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
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)
;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
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);
}
void USART3_init(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
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
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(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) != SET)
;
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);
}
void UART5_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure_UART5;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //ê1ÄüGPIOCê±Öó
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE); //ê1ÄüGPIODê±Öó
//UART5¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOC12
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(GPIOC,&GPIO_InitStructure); //3õê¼»ˉPC12
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //GPIOD12
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(GPIOD,&GPIO_InitStructure); //3õê¼»ˉPD2
GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5); //GPIOC12¸′óÃÎaUART5
GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_UART5); //GPIOD2¸′óÃÎaUART5
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);//ê1ÄüUART5ê±Öó
//UART5 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = 100000;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_9b;//×Ö3¤Îa9λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_2;//2¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_Even; //ż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(UART5, &USART_InitStructure); //3õê¼»ˉ′®¿ú5
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
USART_ITConfig(UART5, USART_IT_IDLE, ENABLE);//¿aÆô¿ÕÏDÖD¶Ï
USART_Cmd(UART5, ENABLE); //ê1Äü′®¿ú5
// while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != SET)
// ;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//Uart5 NVIC ÅäÖÃ
NVIC_InitStructure_UART5.NVIC_IRQChannel = UART5_IRQn;//′®¿ú5ÖD¶Ïí¨μà
NVIC_InitStructure_UART5.NVIC_IRQChannelPreemptionPriority=0x03;//ÇàÕ¼óÅÏ輶2
NVIC_InitStructure_UART5.NVIC_IRQChannelSubPriority =0x02; //×óóÅÏ輶3
NVIC_InitStructure_UART5.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure_UART5); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}
void
USART2_IRQHandler(void)
{
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{
uint8_t oneByte = USART_ReceiveData(USART2);
USART_SendData(USART2,oneByte);
}
}
void
USART3_IRQHandler(void)
{
if (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == SET)
{
uint8_t oneByte = USART_ReceiveData(USART3);
USART_SendData(USART2,oneByte);
}
}
谢谢各路大神!
|
最佳答案
查看完整内容[请看2#楼]
我换了一块开发板烧写进去同一个代码,然后就可以进入串口3的接收中断了,很奇怪!难道板子坏了还能只坏这一部分的吗?
|