打印
[应用相关]

STM32外部中断以及中断嵌套

[复制链接]
303|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tpgf|  楼主 | 2024-5-30 17:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x.h"   // Device header

int i,a,b;          //定义三个变量,应用于三个外部中断函数

GPIO_InitTypeDef GPIO_InitStructure;         //GPIO结构体
EXTI_InitTypeDef EXTI_InitStruct;            //外部中断结构体
NVIC_InitTypeDef NVIC_InitStruct;           //中断向量结构体  

int main()
{
       
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);          //开启GPIOA时钟
       
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;   
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_2|GPIO_Pin_4; //按键  PA0  PA2 PA4
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;    //LED  PA5 PA6 PA7
        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5 |GPIO_Pin_6|GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz ;
        GPIO_Init(GPIOA,&GPIO_InitStructure);
       
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0|GPIO_PinSource1|GPIO_PinSource6);   
       
       
        //2.中断配置
        EXTI_InitStruct.EXTI_Line= EXTI_Line0|EXTI_Line2|EXTI_Line4;  //  EXTI_Linex
        EXTI_InitStruct.EXTI_LineCmd = ENABLE;
        EXTI_InitStruct.EXTI_Mode= EXTI_Mode_Interrupt;
        EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;

        EXTI_Init( &EXTI_InitStruct);
       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

        NVIC_InitStruct.NVIC_IRQChannel =  EXTI0_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd  =  ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority= 0; //0-3
        NVIC_InitStruct.NVIC_IRQChannelSubPriority  = 0;    // 0-3
        NVIC_Init(&NVIC_InitStruct);
       
        NVIC_InitStruct.NVIC_IRQChannel =  EXTI2_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd  =  ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority= 1; //0-3
        NVIC_InitStruct.NVIC_IRQChannelSubPriority  = 0;    // 0-3
        NVIC_Init(&NVIC_InitStruct);
       
        NVIC_InitStruct.NVIC_IRQChannel =  EXTI4_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd  =  ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority= 2; //0-3
        NVIC_InitStruct.NVIC_IRQChannelSubPriority  = 0;    // 0-3
        NVIC_Init(&NVIC_InitStruct);
       
        while(1)
        {
                GPIO_ResetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
        }
}

void Delay(u16 time)
{   
   u16 i=0;  
   while(time--)
   {
      i=12000;  //自己定义
      while(i--) ;   
   }
}

void EXTI0_IRQHandler()
{
        if(EXTI_GetITStatus(EXTI_Line0)==SET)   //判断是否是外部中断5
                {               
                                for(i=0;i<1000;i++)   // i=100
                        {
                                Delay(2);
                                GPIO_SetBits(GPIOA,GPIO_Pin_5);
                                GPIO_ResetBits(GPIOA,GPIO_Pin_6|GPIO_Pin_7);
                        }
                                EXTI_ClearITPendingBit(EXTI_Line0) ; //清楚外部中断0的标准
                }
               
               
}

void EXTI2_IRQHandler()
{
        if(EXTI_GetITStatus(EXTI_Line2)==SET)   //判断是否是外部中断5
                {               
                                for(a=0;a<1000;a++)   // i=100
                        {
                                Delay(2);
                                GPIO_SetBits(GPIOA,GPIO_Pin_6);
                                GPIO_ResetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_7);
                        }
                        EXTI_ClearITPendingBit(EXTI_Line2) ; //清楚外部中断0的标准,
                }
                       
               
}

void EXTI4_IRQHandler()
{
        if(EXTI_GetITStatus(EXTI_Line4)==SET)   //判断是否是外部中断5
                {               
                                for(b=0;b<1000;b++)   // i=100
                        {
                                Delay(2);
                                GPIO_SetBits(GPIOA,GPIO_Pin_7);
                                GPIO_ResetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6);
                        }
                        EXTI_ClearITPendingBit(EXTI_Line4) ; //清楚外部中断6的标准
                }
                       
               
}



————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/m0_64386340/article/details/133976741


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1502

主题

14431

帖子

9

粉丝