EXTI interrupt switch lights up LED source code

On the basis of KEY lighting the LED source code

Create a new EXIT folder under USER, create new bsp_exit.c and bsp_exit.h, and add them to the project (magic wand adds the folder where the header file is located)

bsp_exit.h content

#ifndef BSP_EIXT_H_
#define BSP_EIXT_H_

#include "stm32f10x.h"



void GPIOA_EXTI_Config(void);
void EXTI_NVIC_config(void);
#endif

bsp_exit.c content

#include "bsp_exit.h"

void GPIOA_EXTI_Config(){

	 GPIO_InitTypeDef GPIO_InitStruct;
	 EXTI_InitTypeDef EXTI_InitStruct; //Variable declarations are not allowed after statement execution

	 EXTI_NVIC_config();
	 //Initialize GPIOC
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	 GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
	 GPIO_Init(GPIOA, &GPIO_InitStruct);

	 //Initialize GPIOC_13 as the input line of EXTI
	 GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

	 EXTI_InitStruct.EXTI_Line=EXTI_Line0;
	 EXTI_InitStruct.EXTI_LineCmd=ENABLE;
	 EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
	 EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
   EXTI_Init(&EXTI_InitStruct);
}

 static  void EXTI_NVIC_config(){   //Can only be called in this article
  NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1);

	NVIC_InitStruct.NVIC_IRQChannel= EXTI0_IRQn; //Set the interrupt source attention (that is, the EXTI input line) 
	NVIC_InitStruct.NVIC_IRQChannelCmd= ENABLE;   //Open
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority= 1 ;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority= 1  ;

	NVIC_Init(&NVIC_InitStruct);
}


sstm32f10x_it.h (introduce the corresponding header files for LED and EXTI)


 void EXTI0_IRQHandler( void ){
	  if (EXTI_GetITStatus(EXTI_Line0)!=RESET){ //Determine whether the interrupt source is the one that lights up the LED
	  LED_TOGGLE;
	 }
	 EXTI_ClearITPendingBit(EXTI_Line0); //Clear the interrupt

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771118&siteId=291194637