Home > other >  Summarizes the STM32 external interrupt
Summarizes the STM32 external interrupt

Time:04-05


Experiment result: external interrupt, implementing PA1 up a key, select rising along the trigger interrupt, interrupt PB1 flicker on the LED,


Let's look at the configuration of the external interrupt the overall code, and then a function is a function of explain

[color=# 00 FFFF] # include "exti. H
"/*
Initializes the NVIC always interrupt
*/
The static void NVIC_Configur (void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_1);
NVIC_InitStructure. NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure. NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init (& amp; NVIC_InitStructure);
}
/* * * * * * * * * * * * * * * * * * * * * * * * */
/*
Initialize the GPIO port, GPIOA_PIN1
*/
Void GPIO_Configur (void)
{
GPIO_InitTypeDef GPIO_InitStructure;//named GPIO initialized structure
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);//can make the clock
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_1;//select PIN
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;//suspension type
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);//initialization function called
}
/* * * * * * * * * * * * * * * * * * * * * * * * * */
/*
Tell that GPIO pins as an external interrupt pin
*/
Void AFIO_Configur (void)
{
RCC_APB2PeriphClockCmd (RCC_APB2Periph_AFIO, ENABLE);//can make AFIO clock
GPIO_EXTILineConfig (GPIO_PortSourceGPIOA, GPIO_PinSource1);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
Configuration EXTI
*/
Void EXTI_Configur (void)
{
NVIC_Configur ();
GPIO_Configur ();
AFIO_Configur ();
EXTI_InitTypeDef EXTI_InitStructure;//named ETTI initialized structure
EXTI_InitStructure. EXTI_Line=EXTI_Line1;
EXTI_InitStructure. EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure. EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_InitStructure. EXTI_LineCmd=ENABLE;
EXTI_Init (& amp; EXTI_InitStructure);
[/color]}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Let's look at the first function
static void NVIC_Configur (void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_1);
NVIC_InitStructure. NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure. NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init (& amp; NVIC_InitStructure);
} [/align]

Why do you want to write the function of it?
Let's look at the full name of NVIC (Nested vectoredinterrupt controller) is nested vector interrupt controller
As a result of the STM32 interrupt system is very powerful, can each GPIO interrupt, so need a can manage these interrupt controller
Here is 32 interrupts and events, I only released part, there are a lot of can watch
on STM reference manual

The NVIC exactly is how to manage these disruptions, from the function one by one we see

NVIC_PriorityGroupConfig (NVIC_PriorityGroup_1);
32 the interrupt priority can be set, so we need to give him the configuration priority, so this is for his priority selection function, why choose set the table first

NVIC_IRQChannelSubPriority NVIC_IRQChannelPreemptionPriority on the left is the main priority, right is second priority
Each group has a corresponding choice scope of primary and secondary priority, 32 will first compare the main priority, if the main priority, priority will be more time, if both primary and secondary, with 32 own configuration priority,

NVIC_InitStructure. NVIC_IRQChannel=EXTI1_IRQn;
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure. NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

The four functions, start configuring NVIC structure members,

The first paragraph function, is to choose the you want to use the name of the interrupt, name in stm32f10x. H file to find (literally add, can function in the misc regarding the NVIC. H) were found in the
EXTI1 have 0 to 15, 16 correspond IO port, we need PA1, so choose EXTI1


Second, the three functions, is to choose the interrupt of the primary and secondary priority

The fourth section can make NVIC

Then initialized structure, named after initialization, the library function routine procedures can not explain the

Configure GPIO, anyone who wants to use GPIO configuration to it, must pay attention to the input of suspended type and configuration required frequency when
void GPIO_Configur (void)
{
GPIO_InitTypeDef GPIO_InitStructure;//named GPIO initialized structure
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);//can make the clock
GPIO_InitStructure. GPIO_Pin=GPIO_Pin_1;//select PIN
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;//suspension type
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);//initialization function called
}

/* * * * * * * * * * * * * * * * * * * * * * * * * */
/*
Tell that GPIO pins as an external interrupt pin
*/
void AFIO_Configur (void)
{
RCC_APB2PeriphClockCmd (RCC_APB2Periph_AFIO, ENABLE);//can make AFIO clock
GPIO_EXTILineConfig (GPIO_PortSourceGPIOA, GPIO_PinSource1);
}

PS: remember the clock can make AFIO

The final step configuration external interrupt EXTI
void EXTI_Configur (void)
{
NVIC_Configur ();
GPIO_Configur ();
AFIO_Configur ();
EXTI_InitTypeDef EXTI_InitStructure;//named ETTI initialized structure
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related