STM32 notes - EXTI external interrupt

Table of contents

I. Overview

2. Introduction to main peripherals

2.1 AFIO multiplexed IO port

2.2 Introduction to EXTI

EXTI block diagram:

 2.3 NVIC interrupt priority grouping

3. The overall structure of EXTI external interrupt

4. Experimental procedures

4.1 Through-beam infrared sensor module

4.2 Rotary encoder module

5. Experimental phenomena


I. Overview

        Regarding the EXTI external interrupt, first, let’s briefly introduce the peripherals AFIO, EXTI, NVIC, etc. to be used; then, introduce the process from triggering the external interrupt to the CPU responding to the interrupt function; finally, it is a simple application of the external interrupt, including two Experiment: ① Through-beam infrared sensor counting; ② Rotary encoder counting.

2. Introduction to main peripherals

2.1 AFIO multiplexed IO port

  • The main function of AFIO here is interrupt pin selection. AFIO is composed of many data selectors, which can complete data selection, thus reducing the number of IO ports. For example: STM32F103C8T6 has two sets of GPIO, GPIOA and GPIOB, with a total of 32 pins, both of which can trigger interrupts, but the same pins cannot trigger interrupts at the same time. If there is one IO port and one interrupt channel, 32 channels are needed, which takes up too much IO resources. . As shown in the figure, select one bit from PA0 and PB0 to enter the EXTI0 channel; select one bit from PA1 and PB1 to enter the EXTI1 channel...so the same pin cannot trigger interrupts at the same time.

2.2 Introduction to EXTI

EXTI block diagram:

  • EXTI (Extern Interrupt) external interrupt
  • EXTI can monitor the level signal of the designated GPIO port. When the level of the designated GPIO port changes, EXTI will immediately send an interrupt request to NVIC. After the NVIC decision, the CPU main program can be interrupted, causing the CPU to execute the interrupt corresponding to EXTI. program
  • Supported trigger methods: rising edge/falling edge/double edge/software trigger
  • Supported GPIO ports: all GPIO ports, but the same Pin cannot trigger interrupts at the same time
  • Number of channels: 16 GPIO_Pin, plus PVD output, RTC alarm clock, USB wake-up, Ethernet wake-up
  • Trigger response mode: interrupt response/event response

 2.3 NVIC interrupt priority grouping

  • The interrupt priority of NVIC is determined by the 4 bits (0~15) of the priority register. These 4 bits can be divided into high n-bit preemption priority and low 4-n bit response priority.
  • Those with high preemption priority can interrupt nesting, those with high response priority can be queued first, and those with the same preemption priority and response priority can be queued according to the interrupt number.

Grouping method

Seize priority

response priority

Group 0

0 bit, the value is 0

4 bits, value is 0~15

Group 1

1 bit, value is 0~1

3 bits, the value is 0~7

Group 2

2 bits, the value is 0~3

2 bits, the value is 0~3

Group 3

3 bits, the value is 0~7

1 bit, value is 0~1

Group 4

4 bits, value is 0~15

0 bit, the value is 0

3. The overall structure of EXTI external interrupt

  1.  AFIO interrupt pin selection. Select the appropriate interrupt channel. PA0 and PB0 are EXTI0 channels, PA1 and PB1 are EXTI1 channels... In order to save channel resources, EXTI5-EXTI9 are combined into one channel EXTI9_5, and EXTI10-EXTI15 are combined into one channel EXTI15_10.
  2. EXTI edge detection and control. Set the trigger method: rising edge/falling edge/double edge/software trigger; set interrupt response or event response, etc.
  3. NVIC priority grouping. It is divided into high n-bit preemption priority and low 4-n bit response priority.

4. Experimental procedures

4.1 Through-beam infrared sensor module

  •  When there is no obstruction, D0 outputs low level, and when there is obstruction, it outputs high level.

The module program is as follows:

#include "stm32f10x.h"                  // Device header
uint16_t count;
/*
      *函数名      :CountSensor_Init(void)
      *函数功能    :红外传感器触发外部中断初始化
      *输入        :无
      *输出        :无
*/
void CountSensor_Init(void)
{	
	//GPIO初始化
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB时钟
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);//GPIOB初始化
	//AFIO初始化
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//使能AFIO时钟
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource14);//选择EXTI14线
	//EXIT初始化
	EXTI_InitTypeDef EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line=EXTI_Line14;
	EXTI_InitStruct.EXTI_LineCmd=ENABLE;
	EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_InitStruct);//EXIT初始化
	//NVIC初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择NVIC2组
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel=EXTI15_10_IRQn;//EXTI14线属于EXTI15_10通道
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStruct);	//NVIC初始化
}
/*
      *函数名      :Getcount(void)
      *函数功能    :返回计数数据
      *输入        :无
      *输出        :count:计数数据
*/
uint16_t Getcount(void)
{
	return count;
}
void EXTI15_10_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line14)==SET)//判断是否EXTI14线申请的中断
	{
		EXTI_ClearITPendingBit(EXTI_Line14);//软件清零中断标志位
		count++;
	}
}

4.2 Rotary encoder module

  •  As can be seen from the above figure, when not rotating, the initial levels of A and B are both high.
  • When rotating, the output signals are all square wave signals.
  • When rotating clockwise, phase A leads phase B by 90°.
  • When rotating counterclockwise, phase B leads phase A by 90°.

The module program is as follows:

  • Similar to the above, there is an additional GPIO port pin to trigger an interrupt.
#include "stm32f10x.h" 
int16_t count;
/*
      *函数名      :Encode_Init(void)
      *函数功能    :旋转编码器外部中断初始化
      *输入        :无
      *输出        :无
*/

void Encode_Init(void)
{
	//GPIO初始化
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	//AFIO初始化
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource0);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource1);

	//EXIT初始化
	EXTI_InitTypeDef EXTI_InitStruct;
	EXTI_InitStruct.EXTI_Line=EXTI_Line0|EXTI_Line1;
	EXTI_InitStruct.EXTI_LineCmd=ENABLE;
	EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_InitStruct);
	//NVIC初始化
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStruct);
	
	NVIC_InitStruct.NVIC_IRQChannel=EXTI1_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
	NVIC_Init(&NVIC_InitStruct);
}

int16_t Getencode(void)
{
	int16_t temp=0;
	temp=count;
	count=0;
	return temp;
}
void  EXTI0_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line0)==SET)
	{
		EXTI_ClearITPendingBit(EXTI_Line0);
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==1)
		{
			count--;
		}
	}
}
void  EXTI1_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line1)==SET)
	{
		EXTI_ClearITPendingBit(EXTI_Line1);
		if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==1)
		{
			count++;
		}
	}
}

5. Experimental phenomena

  • Cover the groove of the infrared sensor with a shielding plate. When the shielding plate is removed, the count increases by one.
  • When the rotary encoder is rotated clockwise, the count increases by one; when rotated counterclockwise, the count decreases.

Guess you like

Origin blog.csdn.net/ssssshhbh/article/details/129387192