【Experiment 3】External interrupt experiment

Publisher:SereneMeadow7Latest update time:2017-09-26 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Experimental Purpose


1) Understand the knowledge related to STM32 external interrupts;

2) Use STM32 external interrupt to turn on and off two LEDs;


2. Introduction to STM32 external interrupts

1) The STM32F103 series has 60 maskable interrupts and supports 19 external interrupt/event requests. Each interrupt has a status bit, and each interrupt/event has independent trigger and mask settings. 

The 19 external interrupts of STM32F103 are:


Line 0~15: corresponds to the input interrupt of the external IO port.

Line 16: Connect to PVD output.

Line 17: Connected to the RTC alarm event.

Line 18: Connected to USB wakeup event.

The interrupt line corresponds to:


2) External interrupt function:

EXPORT EXTI0_IRQHandler
EXPORT EXTI2_IRQHandler
EXPORT EXTI4_IRQHandler
EXPORT EXTI15_10_IRQHandler
EXPORT EXTI9_5_IRQHandler
EXPORT EXTI1_IRQHandler
EXPORT EXTI3_IRQHandler

Interrupt lines 0-4 each correspond to an interrupt function, interrupt lines 5-9 share the interrupt function EXTI9_5_IRQHandler, and interrupt lines 10-15 share the interrupt function EXTI15_10_IRQHandler. 

When writing interrupt service functions, two functions are often used. The first function is to determine whether an interrupt occurs on a certain interrupt line (whether the flag bit is set):

ITStatus EXTI_GetITStatus(uint32_t EXTI_Line);

This function is generally used at the beginning of the interrupt service function to determine whether an interrupt has occurred. Another function is to clear the interrupt flag on a certain interrupt line:

void EXTI_ClearITPendingBit(uint32_t EXTI_Line);

This function is generally used to clear the interrupt flag before the end of the interrupt service function.

3) General steps in IO port external

1) Initialize the IO port as input.

2) Enable the IO port multiplexing clock and set the mapping relationship between the IO port and the interrupt line.

3) Initialize online interrupts, set trigger conditions, etc.

4) Configure the interrupt group (NVIC) and enable interrupts.


5) Write the interrupt service function.


3. Hardware Design

LED lights, buttons.


4. Software Design

exti.c is mainly involved in the implementation of initialization and interrupt functions


  1. //External interrupt 0 service routine  

  2. void EXTIX_Init(void)  

  3. {  

  4.    

  5.     EXTI_InitTypeDef EXTI_InitStructure;  

  6.     NVIC_InitTypeDef NVIC_InitStructure;  

  7.   

  8.         KEY_Init(); //Key port initialization  

  9.   

  10.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //Enable multiplexing function clock  

  11.   

  12.    //GPIOE.2 interrupt line and interrupt initialization configuration falling edge trigger  

  13.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);  

  14.   

  15.     EXTI_InitStructure.EXTI_Line=EXTI_Line2; //KEY2  

  16.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;   

  17.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  

  18.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;  

  19.     EXTI_Init(&EXTI_InitStructure); //Initialize the peripheral EXTI register according to the parameters specified in EXTI_InitStruct   


  1.     NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // Enable the external interrupt channel where the WK_UP button is located  

  2.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //Preemption priority 2,   

  3.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //Subpriority 3  

  4.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable external interrupt channel  

  5.     NVIC_Init(&NVIC_InitStructure); //Enable external interrupt channel  

  6.     NVIC_Init(&NVIC_InitStructure); //Initialize peripheral NVIC registers according to the parameters specified in NVIC_InitStruct  

  7.    

  8. }  

Interrupt function implementation: The implementation of functions is also mainly implemented in the interrupt function



  1. void EXTI0_IRQHandler(void)  

  2. {  

  3.     delay_ms(10); //debounce  

  4.     if(KEY3==1) //key KEY3  

  5.     {  

  6.         LED0=!LED0;  

  7.     }          

  8.     EXTI_ClearITPendingBit(EXTI_Line0); //Clear the interrupt flag on LINE0    

  9. }  


V. Experimental Results


success!


Reference address:【Experiment 3】External interrupt experiment

Previous article:[Experiment 4] Independent watchdog experiment
Next article:【Experiment 2】Serial port experiment

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号