【读书笔记】EXTI外部中断 实例

本次使用开发板:    

STM32f2XX系列

本次勾选的驱动列表如下:

  • Core
  • Startup        
  • Framework   ---包含msci,h、stm32f2xx_conf.h,不需要额外copy文件
  • RCC           ---包含GPIO时钟和 SYSCFG时钟,SYSCFG时钟是因为配置中断源需要
  • GPIO            ----指示灯、按键
  • EXTI             ---外部中断,本次主角
  • SYSCFG      --主要配置GPIOX引脚作为中断输入源
stm32f2xx_exti.c中记载了EXTI驱动的使用方法:
【读书笔记】EXTI外部中断 实例_第1张图片

main文件如下:
#include "stm32f2xx_conf.h"
//按键对应引脚是PD7
//LED灯对应引脚是PC4


void BSP_LED_Init()
{
	GPIO_InitTypeDef GPIOInitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC ,ENABLE);
	
	GPIOInitStructure.GPIO_Mode = GPIO_Mode_OUT;
	GPIOInitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIOInitStructure.GPIO_OType = GPIO_OType_PP;
	
	GPIOInitStructure.GPIO_Pin = GPIO_Pin_4;
	GPIO_Init(GPIOC,&GPIOInitStructure);

}


void BSP_EXTI_Init()
{
   EXTI_InitTypeDef EXTIInitStructure;
   GPIO_InitTypeDef GPIOInitStructure;
   
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD ,ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
   
   GPIOInitStructure.GPIO_Mode = GPIO_Mode_IN;
   GPIOInitStructure.GPIO_PuPd = GPIO_PuPd_UP;
   
   GPIOInitStructure.GPIO_Pin = GPIO_Pin_7;
   GPIO_Init(GPIOD,&GPIOInitStructure);
   
   //配置PD7为中断输入源,需启用SYSCFG时钟
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOD,EXTI_PinSource7);

  EXTIInitStructure.EXTI_Line = EXTI_Line7;
  EXTIInitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTIInitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTIInitStructure.EXTI_LineCmd = ENABLE;
  
  EXTI_Init(&EXTIInitStructure);
}
void EXTI9_5_IRQHandler(void)
{
  if(RESET != EXTI_GetFlagStatus(EXTI_Line7) )
  {
    EXTI_ClearITPendingBit(EXTI_Line7);
    GPIO_ToggleBits(GPIOC,GPIO_Pin_4);
  }
}
void BSP_NVIC_Init()
{
  NVIC_InitTypeDef NVICInitStructure;
  NVICInitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
  NVICInitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVICInitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVICInitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_Init(&NVICInitStructure);
}

int main(void)
{


  BSP_LED_Init();
  BSP_EXTI_Init();
  BSP_NVIC_Init();

  GPIO_SetBits(GPIOC,GPIO_Pin_4);
  while(1)
  {

  }
}	




你可能感兴趣的:(STM32)