yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
STM32F103RCT6 Core board📖
Basic introduction of core board
  • Basic introduction
Experimental documents
  • 1.Turn on RGB light
  • 2.Drive LCD
  • 3.Detect external Flash
  • 4.Read IMU raw data
  • 5. Key control
  • 6. External interruption experiment
  • 7. ADC acquisition
  • 8. PWM Breathing Lamp Experiment
  • 9. Serial communication
Download
  • Source code
  • Environment building tools
  • Hardware data
Download ZIP

Welcome to STM32F103RCT6 Core board repository

6. External interruption experiment

External interruption experiment

1、Learning objectives

1.Learn about STM32 external interrupts.

2.Use the key module to control the switch of the on-board LED lamp in the form of an external terminal.

Introduction to External Interruptions:

An interrupt is when the CPU is executing a program and there are other events that need to occupy the CPU. Upon receiving this interrupt request, the CPU pauses the execution of the original program and executes the interrupt function. After the interrupt function is completed, it continues to return to the original program and continue executing.

The STM32F1x External Interrupt/Event Controller (EXTI) contains up to 20 edge detectors for generating event/interrupt requests. Each input line of EXTI can be individually configured to select the type (interrupt or event) and corresponding trigger event (rising edge trigger, falling edge trigger, or edge trigger), and can also be independently shielded.

The peripheral devices connected to the 20 interrupt/event lines are described in the following table:

EXTI LINEExplain
EXTI(0-15)Input interrupt corresponding to external 10 ports
EXTI(16)Connect to PVD output
EXTI(17)Connect to RTC alarm events
EXTI(18)Connect to USB OTG FS Wakeup Event
EXTI(19)Connect to Ethernet Wakeup Event

STM32f103RCT6 Each GPIO can generate interrupts, with 16 interrupt lines and a maximum of 6 interrupt service functions.

There are 19 interrupt detection edges, and 16 interrupt lines correspond to a maximum of 16 interrupts. However, the CPU can only handle one event at a time, so it is necessary to prioritize these interrupts.

Each interrupt line 0-4 corresponds to an interrupt function, and interrupt lines 5-9 share the interrupt function EXTI9_ 5_ IRQHandler, interrupt lines 10-15 share interrupt function EXTI15_ 10_ IRQHandler.

General steps for using external interrupts on the IO port:

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 interrupt grouping (NVIC) and enable interrupts.

5)Write an interrupt service function.

2、Hardware construction

The keystroke module and DuPont cable used in the external interrupt experiment need to be purchased separately, otherwise they lack important accessories and cannot complete this course.

image-2023022700002 Module Wiring Diagram

3、 Program analysis

Interrupt initialization:

void My_EXTI_Init(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    EXTI_InitTypeDef  EXTI_InitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
    
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
    
    
    //EXTI0 NVIC 
    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;       
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;         
    NVIC_Init(&NVIC_InitStructure); 
    
    EXTI_InitStructure.EXTI_Line=EXTI_Line0;
    EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd=ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    
}

To use an external interrupt, we must first configure it as shown in the figure above.

EXTI interrupt function:

xxxxxxxxxx
void EXTI0_IRQHandler(void)
{
    if(EXTI_GetITStatus(EXTI_Line0)==1)
    {
        //Delay_ms(10);
        SysTick_Delay_Ms(10);
        if(K_UP==0)
        {
            led1=!led1;
        }   
    }
    EXTI_ClearITPendingBit(EXTI_Line0);
}

After initializing EXTI, the interrupt is already enabled. When any key is pressed, an interrupt will be triggered, and the program will enter the interrupt service function for execution.

Main function code:

xxxxxxxxxx
int main(void)
{   
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  
    bsp_init();
    LED_Init();
    KEY_Init();
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
    My_EXTI_Init();  
     while(1)
    {   
     //while Prevent program from flying            
    } 
                        
}

The main function implements a simple function. First, group NVICs. Here, we call NVICs_ The PriorityGroupConfig (NVIC_PriorityGroup_2) function divides NVICs into two groups, that is, preemption priority and response priority occupy two bits. Then initialize the used hardware port clock and IO port, and call the EXTI initialization function we wrote earlier.

We're at My_ EXTI_ The Init() function already maps the key pin to the interrupt line and configures the corresponding trigger method. When a key is pressed, it will enter the corresponding interrupt service function to execute the corresponding function program, and LED control is completed within the interrupt function.

4、Experimental Phenomena

After the program is downloaded, you can control the LED light to turn on and off by pressing the button. Press the button once, the LED light will turn on and off by pressing it again. image-2023022700003

Key control
ADC acquisition

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文