Stm32 External Interrupt

Source: Internet
Author: User
Each Io of stm32 can be used as the interrupt input port for external interruptions, which is also powerful for stm32. The Interrupt Controller of stm32f103 supports 19 External Interrupt/Event requests. Each interrupt has a status bit, and each interrupt/event has its own trigger and shield settings. Stm32f103
19 external interruptions:
Line 0 ~ 15: the input of the corresponding external IO port is interrupted.
Line 16: connect to the PCIe output.
Line 17: connect to the RTC alarm event.
Line 18: connect to the USB wake-up event.
We can see from the above that there are only 16 disconnection ports for the I/O port of stm32, but there are far more than 16 I/O Ports of stm32, so how does stm32 match 16 Central disconnection and I/O Ports one by one? So stm32 is like this.
Design, discipline gpiox.0 ~ Gpiox.15 (x = A, B, C, D, E, F, G) correspond to 0 ~ 15. In this way
Disconnection corresponds to a maximum of seven Io ports. For example, wire 0 corresponds to gpioa.0, gpiob.0, gpioc.0, gpiod.0,
Gpioe.0, gpiof.0, and gpiog.0. However, the central disconnection can only be connected to one IO port at a time, so you need to configure

Determines the gpio to which the disconnection is configured. Next we will look at the gpiing relationship between gpio and disconnection:


In the library function, configure the gpiing between gpio and the disconnection function gpio_extilineconfig:
Void gpio_extilineconfig (uint8_t gpio_portsource, uint8_t gpio_pinsource)
This function maps the gpio port to the disconnection. The example is:
Gpio_extilineconfig (gpio_portsourcegpioe, gpio_pinsource2 );
Ing the middle line 2 and gpioe, it is obvious that the gpioe.2 is connected to the exti2. Set the interrupt
After line ing, how is the interrupt from this IO port triggered? Next we need to set
The initialization parameter of the online interrupt of the interrupt.
The initialization of an online interrupt is implemented through the exti_init () function. The exti_init () function is defined as follows:
Void exti_init (exti_inittypedef * exti_initstruct );
The following uses an example to describe how to use this function:
Exti_inittypedef exti_initstructure;
Exti_initstructure.exti_line = exti_line4;
Exti_initstructure.exti_mode = exti_mode_interrupt;
Exti_initstructure.exti_trigger = exti_trigger_falling;
Exti_initstructure.exti_linecmd = Enable;
Exti_init (& exti_initstructure); // according to
// The parameter initializes the peripheral exti register.
In the preceding example, the interrupt on disconnection 4 is triggered as the descent edge. The initialization of stm32 peripherals is based on struct.
Set the initial value. Here we will not go into the initialization process of the struct. Let's take a look at the result of the struct exti_inittypedef
Member variable: typedef struct

{
Uint32_t exti_line;
Extimode_typedef exti_mode;
Extitrigger_typedef exti_trigger;
Functionalstate exti_linecmd;
} Exti_inittypedef;

From the definition, we can see that four parameters need to be set. The first parameter is the line break label and the value range is
Exti_line0 ~ Exti_line15. The concept of disconnection has been discussed above. That is to say, this function is configured
The interrupt parameter of an interrupt line. The second parameter is the interrupt mode. The optional value is the interrupt exti_mode_interrupt and
Exti_mode_event. The third parameter is the trigger method. It can be the descent edge that triggers the exti_trigger_falling.
The rising edge triggers exti_trigger_rising, or any level (rising edge and falling edge ).
Exti_trigger_rising_falling, I believe it is not hard to understand this. The last parameter is enable disconnection.
.
We have set the ing between the disconnection and gpio, and set initialization parameters such as the interrupt trigger mode. Both
However, it is an external interrupt. We also need to set the priority of Objective C interrupt when it comes to interruptions. This has been explained earlier.
In the above example, we set the interrupt priority of the disconnection 2.
Nvic_inittypedef nvic_initstructure;
Nvic_initstructure.nvic_irqchannel = exti2_irqn; // enable the External Interrupt channel by pressing the key
Nvic_initstructure.nvic_irqchannelpreemptionpriority = 0x02; // preemptible Priority 2,
Nvic_initstructure.nvic_irqchannelsubpriority = 0x02;
// Subpriority 2
Nvic_initstructure.nvic_irqchannelcmd = Enable;
// Enable the external interruption Channel

There are only six external interrupt functions for the I/O port of stm32,
They are:
Export exti0_irqhandler
Export extistmirqhandler
Export exti2_irqhandler
Export exti3_irqhandler
Export exti4_irqhandler
Export exti9_5_irqhandler
Export exti15_10_irqhandler
Disconnection 0-4 each disconnection corresponds to an interrupt function. Disconnection 5-9 shares the interrupt function exti9_5_irqhandler
Disconnection 10-15: The shared interrupt function exti15_10_irqhandler. When writing interrupt service functions, we often use two
Function. The first function is to determine whether an interruption occurs on a certain interrupt line (whether the flag is set ):
Itstatus exti_getitstatus (uint32_t exti_line );
This function is generally used to determine whether an interrupt occurs at the beginning of the interrupt service function. Another function is to clear an interrupted line.
The identifier of the interruption:
Void exti_clearitpendingbit (uint32_t exti_line );
This function is usually used to clear the interrupt flag before the interrupt service function ends.
The format of commonly used interrupt service functions is:
Void exti2_irqhandler (void)
{
If (exti_getitstatus (exti_line3 )! = Reset) // determines whether an online interruption has occurred.
{
Interrupt logic...
Exti_clearitpendingbit (exti_line3); // clear the interrupt flag on line
}
}

The Firmware Library also provides two functions to determine the External Interrupt status and clear the external status.
The functions of the flag are exti_getflagstatus and exti_clearflag. Their functions are similar to those of the previous two functions.
Only in the exti_getitstatus function will judge whether the interrupt is enabled before judging the interrupt flag bit.
Exti_getflagstatus is directly used to determine the status flag.

To sum up the general steps of using the I/O port for external interruption:
1) initialize the IO port as input.
2) Enable the IO port to reuse the clock and set the ing between the IO port and the disconnection.
3) initialize online interruptions and set triggering conditions.
4) configure the interrupt group (Objective C) and enable interruption.
5) Write interrupt service functions.
By setting the above steps, we can use external interruptions normally.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.