STM32---External interrupt

table of Contents

 

1. External interrupt description

2. External interrupt block diagram

Summary: After analyzing the block diagram, software interrupts and event interrupts can be generated. The purpose of software interrupts is to enter the interrupt service function. Event interrupt is to generate a pulse signal to the on-chip peripherals, which belongs to the hardware level.

3. Port correspondence

4. Programming ideas

EXIT

NVIC

EXTI1_IRQHandler  EXTI9_5_IRQHandler   EXTI15_10_IRQHandler

5. Code


1. External interrupt description

Acting on the GPIO port, referencing the chip external interrupt as the interrupt source.

General products have 19 external interrupts, and interconnected products have 20 external interrupts. There are only 16 external interrupts (0-15) in the true sense.

The remaining four:

EXTI line 16 is connected to the PVD output

EXTI line 17 is connected to the RTC alarm event

EXTI line 18 is connected to the USB wake-up event

EXTI line 19 is connected to the Ethernet wake-up event (only applicable to interconnected products)

Note: Each interrupt source of EXIT0-EXIT4 corresponds to an interrupt entry address. EXIT5-EXIT9 share an interrupt entry address. . EXIT10-EXIT15 share an interrupt entry address

2. External interrupt block diagram

work process:

1. The input signal from the external GPio pin passes through the edge detection circuit, and when a specific edge is detected, it outputs signal 1.

2. The software sets whether to generate an event interrupt (hardware interrupt) when the line passes through the OR gate .

             Software interrupt event register (EXTI_SWIER) function: each 1 bit controls 1 external interrupt line, this register is valid by writing "1", and writing "0" is invalid. Manually generate an external interrupt/event request

Note: This register cannot be cleared directly (the corresponding bit of the register cannot be cleared directly), it can only be cleared through the corresponding bit of the "suspend register suspend register (EXTI_PR) " to clear the corresponding bit of the register.

3. If you need to generate an event interrupt, you need to write "1" to the corresponding bit in the event mask register (EXTI_EMR) , and a pulse signal will be output at this time.

4. Write "1" to the corresponding bit of the interrupt mask register (EXTI_IMR) to enable the interrupt enable of the corresponding interrupt line, and write "0" to turn off the interrupt.

5. When the edge detection circuit detects the expected edge signal, the corresponding bit of the suspend register will be automatically set by the hardware to generate a flag for applying for an interrupt.

Note: This register can only clear the corresponding flag bit by writing "1" to the corresponding bit (write 1 to clear), or by changing the edge detection condition of the interrupt line to clear the flag bit.

Summary: After analyzing the block diagram, software interrupts and event interrupts can be generated. The purpose of software interrupts is to enter the interrupt service function. Event interrupt is to generate a pulse signal to the on-chip peripherals, which belongs to the hardware level.

3. Port correspondence

External interrupt 0~15 are mapped to any group of GPIO ports with the same name (external interrupt 0, mapped to the GPIO port with port number 0), and an external interrupt entry can only be mapped to one GPIO port

4. Programming ideas

  1. Define the structure NVIC and EXIT
  2.  Turn on the multiplexed clock----RCC_APB2PeriphClockCmd

EXIT

  1. Configure the external interrupt line corresponding to the GPIO port. GPIO_EXTILineConfig (GPIO_PortSourceGPIOA, GPIO_PinSource0)
  2. Select the external interrupt line corresponding to GPIO. 0-15 
  3. Select external interrupt or event interrupt
  4. Select rising edge or falling edge trigger
  5. Enable interrupt line
  6. Initialize the external interrupt structure

NVIC

  1. Configure NVIC interrupt source
  2. Set preemption priority and response priority
  3. Interrupt channel enable
  4. Initialize the interrupt structure

EXTI1_IRQHandler  EXTI9_5_IRQHandler   EXTI15_10_IRQHandler

  1. Write interrupt service function

5. Code

void KEY_Exit_Init(void)
{
	NVIC_InitTypeDef  NVIC_InitStructure;
	EXTI_InitTypeDef  EXTI_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line3;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;  //软件中断
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿触发---key默认高电平
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	EXTI_InitStructure.EXTI_Line = EXTI_Line4;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;  //软件中断
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //key2
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;//key1
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
}

void EXTI3_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line3) == SET) //挂起寄存器
	{
		EXTI_ClearITPendingBit(EXTI_Line3);//
		//
		
	}
}

 

 

Guess you like

Origin blog.csdn.net/qq_45604814/article/details/114017832