程序的大概目的:通过外部中断,按键按下后,在外部中断中会使对应的LED亮,并且通过串口发送键值以下是存在问题的代码:通过调试后发现,程序会卡死在 USART_GetFlagStatus()中,但是程序根本就没完成串口初始化
int main()
{
KeyInit();
ExtiInit();
LED_Init();
NvicInit();
UsartInit();
while (1);
}
void EXTI0_IRQHandler()
{
if (EXTI_GetITStatus(EXTI_Line0))
{
RLED_Toggle;
printf("PA0 response
");
}
EXTI_ClearFlag(EXTI_Line0);
}
void EXTI15_10_IRQHandler()
{
if (EXTI_GetITStatus(EXTI_Line13))
{
BLED_Toggle;
printf("PC13 response
");
}
EXTI_ClearFlag(EXTI_Line13);
}
后来瞎鸡儿改代码,发现只是将 NvicInit(); 与UsartInit();交换了下位置程序就运行正常了
这两个函数代码如下
NvicInit(); 实现外部中断的配置
void NvicInit()
{
NVIC_InitTypeDef NvicStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NvicStructure.NVIC_IRQChannel = EXTI0_IRQn;
NvicStructure.NVIC_IRQChannelCmd = ENABLE;
NvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
NvicStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NvicStructure);
NvicStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
NvicStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NvicStructure);
}
UsartInit();实现串口中断和串口的配置
void UsartInit()
{
UsartGpioInit();
Init();
NvicConfig();
}
void Init()
{
USART_InitTypeDef UsartStructure;
RCC_APB2PeriphClockCmd(UsartClk, ENABLE);
USART_StructInit(&UsartStructure);
UsartStructure.USART_BaudRate = DeaultBaudRate;
UsartStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
UsartStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
UsartStructure.USART_Parity = USART_Parity_No;
UsartStructure.USART_StopBits = USART_StopBits_1;
UsartStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(DefaultUsart, &UsartStructure);
USART_Cmd(DefaultUsart, ENABLE);
USART_ITConfig(DefaultUsart, USART_IT_RXNE, ENABLE);
}
void NvicConfig()
{
NVIC_InitTypeDef NvicStructure;
NvicStructure.NVIC_IRQChannel = USART1_IRQn;
NvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
NvicStructure.NVIC_IRQChannelSubPriority = 6;
NvicStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NvicStructure);
}
求救啊!!!大佬们、原子哥,救救孩子吧
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>