1 Placement interrupted 1. Allocate interrupt vector table: /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 2. Set interrupt priority: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //Set interrupt priority 3. Initialize external interrupt: /*Enable EXTI4 interrupt*/ NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; //Interrupt channel NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue; //Enforce priority NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Sub-priority NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Channel interrupt enable NVIC_Init(&NVIC_InitStructure); //Initialize interrupt Note: If the external pin we configure is PA4, or PB4, or PC4, PD4, etc., then the external interrupt used must also be EXTI4. Similarly, if the external interrupt pin is PA1, PB1, PC1, PD1, then the interrupt must use EXTI1, and so on.
2Configure the GPIO pin as a trigger event for an external interrupt 1. Select IO pins GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; Note that if the pin is pin 4 of the port, the configured interrupt must be EXTI4 2. Configure the pin as input GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 3. Initialize the pins GPIO_Init(GPIOD,&GPIO_InitStructure);
3. Configure the EXTI line to connect the interrupt line and IO pin line. 1. Connect the EXTI line to the IO port Connect EXTI line 4 to pin 4 of port GPIOD GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4); Note: If the configured pin is number 4, then the parameter must be GPIO_PinSource4 If the configured pin is number 3, then the parameter must be GPIO_PinSource3 2. Configure interrupt edge /*Configure EXTI line 0 to generate an interrupt when a falling edge occurs*/ EXTI_InitStructure.EXTI_Line = EXTI_Line4; Note: If pin 4 is configured, then EXTI_Line4 is required EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //Falling edge trigger EXTI_InitStructure.EXTI_LineCmd = ENABLE; //Interrupt line enable EXTI_Init(&EXTI_InitStructure); //Initialize interrupt EXTI_GenerateSWInterrupt(EXTI_Line4); //EXTI_Line4 interrupt is enabled
Now the interrupt configuration is complete and you can write the interrupt handling function. Example: Configuration Function /************************************************************************* * Function name NVIC_Configration * Description Configure each interrupt register * Input None * Output None * Return value None ****************************************************************************/ void NVIC_Configration(void) { NVIC_InitTypeDef NVIC_InitStructure; //#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //#else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ //NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //#endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //Set interrupt priority /*Enable EXTI4 interrupt*/ NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PreemptionPriorityValue; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*Enable EXTI9 interrupt*/ NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /*Configure SysTick processing priority: priority and sub-priority*/
}
/************************************************************************ * Function name: GPIO_Configuration(void) * Description: Configure TIM2 pins * Input: None * Output: None * Return: None ************************************************************************/ void GPIO_Configuration(void){ /* GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOC,&GPIO_InitStructure);
/*Configure the first pin of GPIOD as floating input*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD,&GPIO_InitStructure);
/*Configure the 9th pin of GPIOB as floating input*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB,&GPIO_InitStructure); }
/************************************************************** * Function SysTick_Configuration * Description Set SysTick * Input None * Output None * Return value None ***************************************************************/ void SysTick_Configuration(void) { /*Configure HCLK clock as SysTick clock source*/ SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //System clock divided by 8 72MHz
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 8,2); /*SysTick Interrupt each 1000Hz with HCLK equal to 72MHz*/ SysTick_SetReload(9000); //Interrupt cycle 1ms /*Enable the SysTick Interrupt */ SysTick_ITConfig(ENABLE); //Enable interrupt SysTick_CounterCmd(SysTick_Counter_Enable); SysTick_CounterCmd(SysTick_Counter_Clear); }
/****************************************************************************** * Function name EXTI_Configuration * Description Configure EXTI line * Input None * Output None * Return value None ******************************************************************************/ void EXTI_Configuration(void){
/* Connect EXTI line 0 to PA0 */ GPIO_EXTILineConfig(GPIO_PortSourceGPIOD,GPIO_PinSource4); /*Configure EXTI line 0 to generate an interrupt when a falling edge occurs*/ 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); EXTI_GenerateSWInterrupt(EXTI_Line4);
/*Connect EXTI line 9 to PB9*/ GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9); /*Generate an interrupt when a falling edge appears on EXTI line 9*/ EXTI_InitStructure.EXTI_Line = EXTI_Line9; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); EXTI_GenerateSWInterrupt(EXTI_Line9); }
Interrupt function: void EXTI4_IRQHandler(void) { if (EXTI_GetITStatus(EXTI_Line4)!= RESET){ EXTI_ClearITPendingBit(EXTI_Line4); if(Ledflag == 0){ Ledflag = 1; GPIOC->ODR |= 0X00000080; } else{ Ledflag = 0; GPIOC->ODR &= 0XFFFFFF7F; } } } |
Previous article:STM32 uses timer to accurately delay (non-SysTick)
Next article:STM32 uses systick to accurately delay nms
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- Switching Power Supply Reference Book, "Switching Power Converters: Principles, Simulation, and Design of Switching Power Supplies"
- Today's 10:00 AM broadcast | MPS inductor solutions help better switching power supply design
- Playing with Zynq Serial 17 - Creating a New Vivado Project
- Help analyze this MOS tube control circuit
- [Raspberry Pi Pico Review] 1. Develop Pico without MicroPython and C++
- McDonald's launches scented candles! But you may be afraid of this scent
- Beautiful ESP32 Pacman Clock
- RC Application Circuit Collection 2
- Segment code LCD segment code screen driving principle
- Single chip solution for 0-10V dimming driver