当前位置:网站首页>STM32 external interrupt (register version)

STM32 external interrupt (register version)

2022-07-24 06:58:00 The road is difficult at night

1) initialization IO The mouth is the input .

Initialize your key input port PA0,PA15,PC5.

2) Turn on IO Port multiplex clock , Set up IO Mapping relationship between port and interrupt line

eg.PA0-15 by EXTI0 External interrupt input source

3) Open with this IO The line opposite the port is interrupted / event , Set trigger conditions .

because IO When setting the port PC5 and PA15 Set to pull up input ,PA0 Enter for drop-down , So the trigger condition is PC5 and PA15 When the potential changes from high to low ( Falling edge trigger ) Trigger interrupt ,PA0 On the contrary .

Ex_NVIC_Config(u8 GPIOx,u8 BITx,u8 TRIM) The three parameters are :GPIOA-G, Bits that need to be enabled , The trigger condition

RTIR: Rising edge trigger    FTIR: Falling edge trigger

secondly , You need to set the problem of interrupt grouping

Preemption priority and response priority :

1. The smaller the serial number , The higher the priority .

2. Interrupt groups with high preemption priority can interrupt interrupt interrupt groups with low preemption priority , But when two preemption priorities arrive at the same time , The interrupt group with high response priority will be executed first , But when they don't arrive at the same time , Even later, when the high response priority arrives , You cannot interrupt the currently executing lower interrupt Group , Preemption priority and response priority can be divided into five groups , In each group 16 Distribution method in . For example, group 2, Preemption attribute and response attribute ,, Preemption and response are 11:11, So there are four options for preemption and response (0-3).

 Insert picture description here

 MY_NVIC_Init(u8 NVIC_PreemptionPriority,u8 NVIC_SubPriority,u8 NVIC_Channel,u8 NVIC_Group)

Respectively means : Value of preemption priority , Value of response priority ,io Interrupt service function corresponding to port , Group .

 // Set trigger conditions 
	Ex_NVIC_Config(GPIO_A,0,RTIR); 		// Rising edge trigger   wake_up  The potential triggers the output change from low to high    It was originally a pull-down resistor 
	Ex_NVIC_Config(GPIO_C,5,FTIR);		// Falling edge trigger   key0     The potential triggers the output change from high to low    It was originally a pull-up resistor 
	Ex_NVIC_Config(GPIO_A,15,FTIR);		// Falling edge trigger   key1	   The potential triggers the output change from high to low 	  It was originally a pull-up resistor 
	// Set interrupt grouping 
	MY_NVIC_Init(2,2,EXTI0_IRQn,2);    	// preemption 2, Sub priority 2, Group 2
	MY_NVIC_Init(2,1,EXTI9_5_IRQn,2);  	// preemption 2, Sub priority 1, Group 2
	MY_NVIC_Init(2,0,EXTI15_10_IRQn,2);	// preemption 2, Sub priority 0, Group 2 

exti.c( After the interrupt is used, the interrupt flag bit needs to be cleared )

// External interrupt 0 Service program 
void EXTI0_IRQHandler(void)
{
	delay_ms(10);	// Desquamation 
	if(WK_UP==1)	//WK_UP Key  
	{
		LED0=!LED0;
		LED1=!LED1;	
	}		 
	EXTI->PR=1<<0;  // eliminate LINE0 Interrupt flag bit on   
}
// External interrupt 9~5 Service program 
void EXTI9_5_IRQHandler(void)
{			
	delay_ms(10);   // Desquamation 			 
    if(KEY0==0)		// Key 0
	{
		LED0=!LED0;
	}
 	EXTI->PR=1<<5;     // eliminate LINE5 Interrupt flag bit on   
}
// External interrupt 15~10 Service program 
void EXTI15_10_IRQHandler(void)
{			
	delay_ms(10);   // Desquamation 			 
    if(KEY1==0)		// Key 1
	{
		LED1=!LED1;
	}
 	EXTI->PR=1<<15; // eliminate LINE15 Interrupt flag bit on   
}
// External interrupt initializer 
// initialization PA0,PC5,PA15 Enter... For the interrupt .
void EXTI_Init(void)
{
	KEY_Init();
	Ex_NVIC_Config(GPIO_A,0,RTIR); 		// Rising edge trigger   wake_up  The potential triggers the output change from low to high    It was originally a pull-down resistor 
	Ex_NVIC_Config(GPIO_C,5,FTIR);		// Falling edge trigger   key0     The potential triggers the output change from high to low    It was originally a pull-up resistor 
	Ex_NVIC_Config(GPIO_A,15,FTIR);		// Falling edge trigger   key1	   The potential triggers the output change from high to low 	  It was originally a pull-up resistor 

	MY_NVIC_Init(2,2,EXTI0_IRQn,2);    	// preemption 2, Sub priority 2, Group 2
	MY_NVIC_Init(2,1,EXTI9_5_IRQn,2);  	// preemption 2, Sub priority 1, Group 2
	MY_NVIC_Init(2,0,EXTI15_10_IRQn,2);	// preemption 2, Sub priority 0, Group 2	   
}

int main()

int main(void)
{			
	Stm32_Clock_Init(9);// System clock settings 
	delay_init(72);	    // Delay initialization 
	uart_init(72,9600); // Serial initialization  
	LED_Init();		  	// Initialization and LED Connected hardware interface 
	EXTI_Init();		// External interrupt initialization 
	while(1)
	{	    
		printf("lalala\r\n");	
		delay_ms(1000);	  
	} 
}

原网站

版权声明
本文为[The road is difficult at night]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/205/202207240549451496.html