STM32--外部中断与NVIC

under 单片机  嵌入式开发  tag     Published on January 1st , 2021 at 05:47 am

代码

void EXTIX_Init(void)
{
 
    /*--------------------------申明结构体---------------------*/
       EXTI_InitTypeDef EXTI_InitStructure;
       NVIC_InitTypeDef NVIC_InitStructure;

    
    /*--------------------------外部中断需要使能AFIO时钟---------------------*/
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);    
    
    
    /*--------------------------配置使用的端口---------------------*/

   //GPIOA.1      中断线以及中断初始化配置 上升沿触发 //KEY0
      GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);
      EXTI_InitStructure.EXTI_Line=EXTI_Line1;
      EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;    
      EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
      EXTI_Init(&EXTI_InitStructure);          

   //GPIOA.2      中断线以及中断初始化配置  上升沿触发    //KEY1
      GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource2);
      EXTI_InitStructure.EXTI_Line=EXTI_Line2;
      EXTI_Init(&EXTI_InitStructure);          
 
    /*--------------------------配置NVIC---------------------*/
    
    
        //GPIOA.1    
      NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;                            //使能按键KEY0所在的外部中断通道
    
    
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x03;    //抢占优先级2 
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;                    //子优先级1 
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                //使能外部中断通道
      NVIC_Init(&NVIC_InitStructure);                                                            //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

        //GPIOA.2
      NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;                            //使能按键KEY1所在的外部中断通道
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;    //抢占优先级2 
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;                    //子优先级0 
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                                //使能外部中断通道
      NVIC_Init(&NVIC_InitStructure);                                                            //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
}



void EXTI1_IRQHandler(void)
{
    delay_ms(10);
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_1))//PB4中断是否发生
    {
        canopy_auto=!canopy_auto;                        //1:开启自动canopy   0:关闭自动canopy   canopy初始状态 0 
    }
    EXTI_ClearITPendingBit(EXTI_Line1); //清除 LINE4 上的中断标志位
}


void EXTI2_IRQHandler(void)
{
    delay_ms(10);
    if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2))//PB6中断是否发生
   {         
            if(!canopy_auto)
            {
                flag=!flag;   //
                if(flag)
                {Canopy_UP();}
                if(!flag)
                {Canopy_DOWN();}
            }
     } 
     EXTI_ClearITPendingBit(EXTI_Line2); //清除 LINE6 上的中断标志位
 }


本文由simyng创作, 采用知识共享署名4.0 国际许可协议进行许可,转载前请务必署名
  文章最后更新时间为:December 31st , 2020 at 09:47 pm