stm32f10x external interrupt

Overview

Each IO of STM32 can be used as external interrupt input.
The interrupt controller of STM32 supports 19 external interrupt/event requests:
Line 0~15: Input interrupt corresponding to external IO port.
Line 16: Connect to PVD output.
Line 17: Connect to the RTC alarm event.
Line 18: Connect to USB wake event.

Each external interrupt line can be independently configured trigger mode (rising edge, falling edge or double edge trigger), trigger/mask, dedicated status bits.

As can be seen from the above, there are only 16 interrupt lines for IO in STM32, but there are hundreds of IO ports in the STM32F10x series, STM32F103ZET6(112),

Mapping relations

STM32F103RCT6(51), how does the interrupt line correspond to the I/O port?
The mapping relationship is used,
GPIOx.0 is mapped to EXTI0 ,
GPIOx.1 is mapped to EXTI1
...
GPIOx.15 is mapped to EXTI15
, that is, a fixed I/O of each group of I/O ports is mapped to the corresponding line. Each group of I/Os is 16 in total from 0 to 15, which is exactly the same as the number of external interrupt lines.

For each interrupt line, we can set the corresponding trigger mode (rising edge trigger (EXTI_Trigger_Rising), falling edge trigger (EXTI_Trigger_Falling), edge trigger (EXTI_Trigger_Rising_Falling)) and enable.

text

In the 51 MCU, each interrupt corresponds to an interrupt service function, but not in stm32. There are only seven interrupt service functions for external interrupts. Each external interrupt of interrupt 0~4 corresponds to an interrupt service function. Interrupt 5~9 Corresponds to one (that is, interrupts 5~9 enter the same interrupt service function), and interrupts 10~15 correspond to one. Seven in total.

List of interrupt service functions:

    EXTI0_IRQHandler           
    EXTI1_IRQHandler
    EXTI2_IRQHandler           
    EXTI3_IRQHandler           
    EXTI4_IRQHandler           
    EXTI9_5_IRQHandler         
    EXTI15_10_IRQHandler

General configuration steps for external interrupts:

1. Initialize the IO port as input.
  GPIO_Init();
2, enable IO port multiplexing clock.
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
3. Set the mapping relationship between the IO port and the interrupt line.
    void GPIO_EXTILineConfig();
4. Initialize online interrupts, set trigger conditions, etc.
   EXTI_Init();
5. Configure interrupt grouping (NVIC) and enable interrupts.
   NVIC_Init();
6, write interrupt service function.
   EXTIx_IRQHandler();
7, clear the interrupt flag
   EXTI_ClearITPendingBit();
Note: Since NVIC is used, it needs to be grouped first:
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set the group to 2

Several function explanations:

1,初始化线上中断: `void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)  // 位于stm32f10x_exti.c 文件中`
    该函数的参数为一个结构体指针
typedef struct
        {
          uint32_t EXTI_Line;              // 中断线

          EXTIMode_TypeDef EXTI_Mode;      // 方式 :中断 或者 事件

          EXTITrigger_TypeDef EXTI_Trigger;     // 中断触发方式(下降沿,上升沿,或者 双边沿) 前面有对应的值

          FunctionalState EXTI_LineCmd;         // 中断使能(ENABLE or DISABLE)

        }EXTI_InitTypeDef;

2, void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)// 位于 misc.c 中
the parameter is also a structure pointer

typedef struct
        {
          uint8_t NVIC_IRQChannel;               // 外部中断几, 取值:EXTI0_IRQn、EXTI1_IRQn、EXTI2_IRQn、EXTI3_IRQn、EXTI9_5_IRQn
                                                // 这个参数的取值位于:stm32f10x.h 的 186 行,以及后面

          uint8_t NVIC_IRQChannelPreemptionPriority;  // 抢占优先级等级,范围与分组有关

          uint8_t NVIC_IRQChannelSubPriority;         // 响应优先级等级,范围与分组有关

          FunctionalState NVIC_IRQChannelCmd;         // 使能, 取值(ENABLE 或者 DISABLE)

        } NVIC_InitTypeDef;

3. Flag bit clearing function:void EXTI_ClearITPendingBit(uint32_t EXTI_Line)// 位于stm32f10x_exti.c 文件中

This function is used to clear the interrupt flag bit. If an interrupt occurs, the hardware will automatically set the flag bit corresponding to the interrupt to 1. It needs to be cleared manually, usually in the interrupt.

Guess you like

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