请选择 进入手机版 | 继续访问电脑版

普中开源电子分享网

 找回密码
 立即注册
搜索
查看: 3121|回复: 0

外部中断结束后主函数无法正确运行

[复制链接]

1

主题

1

帖子

7

积分

新手上路

Rank: 1

积分
7
发表于 2020-4-21 21:30:08 | 显示全部楼层 |阅读模式
1金钱
根据教程我写了一下代码:
#include "exti.h"
#include "Systick.h"
#include "key.h"
#include "led.h"
#include "beep.h"

void My_EXTI_Init(void)
{
        NVIC_InitTypeDef NVIC_InitStruct;
        EXTI_InitTypeDef EXTI_InitStruct;
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
       
        SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0);
        SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource2);
        SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource3);
        SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE,EXTI_PinSource4);
        //EXTI0 NVIC
        NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStruct);
  //EXTI2 NVIC
        NVIC_InitStruct.NVIC_IRQChannel=EXTI2_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStruct);
        //EXTI3 NVIC       
        NVIC_InitStruct.NVIC_IRQChannel=EXTI3_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStruct);
        //EXTI4 NVIC       
        NVIC_InitStruct.NVIC_IRQChannel=EXTI4_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStruct);
       
        EXTI_InitStruct.EXTI_Line=EXTI_Line0;
        EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
        EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
        EXTI_InitStruct.EXTI_LineCmd=ENABLE;
        EXTI_Init(&EXTI_InitStruct);
       
        EXTI_InitStruct.EXTI_Line=EXTI_Line2|EXTI_Line3|EXTI_Line4;
        EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
        EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
        EXTI_InitStruct.EXTI_LineCmd=ENABLE;
        EXTI_Init(&EXTI_InitStruct);
       
}

void EXTI0_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line0)==1)
        {
                delay_nms(10);
                if(KEY_UP==1)
                {
                        led2=0;
                }
        }
        EXTI_ClearITPendingBit(EXTI_Line0);
}

void EXTI2_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line2)==1)
        {
                delay_nms(10);
                if(KEY_RIGHT==0)
                {
                        beep=1;
                }
        }
        EXTI_ClearITPendingBit(EXTI_Line2);
}

void EXTI3_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line3)==1)
        {
                delay_nms(10);
                if(KEY_DOWN==0)
                {
                        led2=1;
                }
        }
        EXTI_ClearITPendingBit(EXTI_Line3);
}

void EXTI4_IRQHandler(void)
{
        if(EXTI_GetITStatus(EXTI_Line4)==1)
        {
                delay_nms(10);
                if(KEY_LEFT==0)
                {
                        beep=0;
                }
        }
        EXTI_ClearITPendingBit(EXTI_Line4);
}


主函数为:
#include "stm32f4xx.h"
#include "led.h"
#include "bit_operation.h"
#include "Systick.h"
#include "beep.h"
#include "key.h"
#include "exti.h"

int main()
{
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        My_EXTI_Init();
        BEEP_Init();
        KEY_Init();
        SysTick_Init(168);  //系统时钟为168M
        RCC_HSE_Config(8,336,2,7);
        LED_Init();
        while(1)
        {
         delay_nms(100);
         led1=!led1;
        }
}1

根据我的设想,主程序中始终是led1进行闪烁,但在板子上运行时,一开始led1是闪烁的,但一按下按键进入中断后led1就熄灭了,不知道究竟是因为什么原因,求指教
我知道答案 本帖寻求最佳答案回答被采纳后将获得系统奖励10 金钱 , 目前已有0人回答

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Archiver|手机版|小黑屋|普中开源电子分享网 粤ICP备16123577号-2

GMT+8, 2024-3-29 22:37 , Processed in 0.083653 second(s), 29 queries .

Powered by 论坛搭建 X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表