Experiment 2 interrupt experiment

1. Purpose of the experiment

  1. Master the STM32 interrupt programming process.
  2. Familiar with the basic use of STM32 firmware library.

2. Experimental equipment

Hardware: one PC

       STM32 development board set

Software: MDK V4.0 set

       Windows 7 set

  • Experimental principle

1. STM32 external interrupt :

   STM32 has 20 external interrupt sources, as follows:

16 GPIO input interrupts ;

PVD output;

RTC alarm event;

USB wakeup event;

Ethernet wake-up event (applicable to interconnected products only).

   Although the GPIO input interrupt has 16 input channels, it only occupies 7 interrupt vectors. EXTI0~EXTI4 each occupy one interrupt vector, EXTI5~9 share one, and EXTI10~15 share one. So when programming, EXTI5~9 will share an interrupt function, and EXTI10~15 will share an interrupt function.

The mapping diagram of these 16 external interrupts and GPIO is as follows.

 

As can be seen from the figure above, the 16 interrupt input pins can be redefined by the user. Of course, there are restrictions, for example: EXTI0 channel can only choose one of the 7 pins PA0, PB0, PC0, PD0, PE0, PF0, PG0; and EXTI1 can only choose one of the 7 pins named Px1 (x can be one of A~G); and so on, EXTI2 can only choose Px2 pin;  …. The specific selection of the pin can be set by the corresponding bit of the register AFIO_EXTICRx (x can be 1~4).

2. Related internal registers

 

The above figure is a block diagram of the external interrupt/event controller. The block diagram involves 6 registers, namely:

Falling edge trigger selection register (EXTI_FTSR);

Rising edge trigger selection register (EXTI_RTSR);

Software interrupt event register (EXTI_SWIER);

pending register (EXTI_PR);

Interrupt mask register (EXTI_IMR);

Event Mask Register (EXTI_EMR).

3. NVIC---Nested Vectored Interrupt Controller

STM32 has only 68 interrupt channels at most, so all the above registers are not used. The priority setting of each interrupt is done by writing these register groups. The STM32 interrupt priority register only uses the upper 4 bits of the 8-bit width, and the meaning of these 4 bits is determined by the settings of the 3 bits [10:8] of the register AIRCR.

 

4. STM32 external interrupt process

1 ) Initialize the IO port as an input.

2 ) Turn on the multiplexing clock of the IO port, and set the mapping relationship between the IO port and the interrupt line.

3 ) Initialize online interrupts, set trigger conditions, etc.

4 ) Configure the interrupt grouping ( NVIC ) and enable the interrupt.

5 ) Write the interrupt service function.

4. Experimental content

Schematic diagram of LED and button circuit

  1. Program to achieve the following:

 When the button is not pressed, the state of the main program is as follows: LED1, LED2, and LED3 flash alternately, and the interval between each LED flashing is 0.2S (once on and off for a total of 0.2S), lasting for 1S.

‚If the keyboard K2 is pressed, the LED1, LED2, and LED3 will realize the function of LED1, LED2, and LED3 forward flowing lights (that is, light up in sequence, from top to bottom), and the LED interval time is 0.2S. (Using to judge whether the button is pressed or not for programming, non-interruption )

ƒ Press the K2 button to enter the interrupt state: LED1, LED2, LED3 are forward-flowing lights (that is, light up in sequence, from top to bottom), and the LED interval time is 0.2S; after 8 cycles, the interruption ends. (Interrupt mode programming)

Continue programming on the basis of ƒ. If the keyboard K3 is pressed, it will also enter the interrupt state: LED1, LED2, LED3 reverse running water light function (that is, light up sequentially, from bottom to top), LED interval time 0.2S; after 3 cycles, the interrupt will end. Requirement: The interrupt level of keyboard K3 is higher than that of keyboard K2.

Submit the content requirements of the experiment report:

  1. Purpose.

(1) Master the STM32 interrupt programming process.
(2), familiar with the basic use of STM32 firmware library.
(3) Master the STM32 timer design process.

2 . experiment content . Question 1 Programming .

First of all, the main function initializes each module, and the while loop makes the 3 lights cycle.

(1) Write key initialization program

(2) Write the led initialization program

 

(3) The service function of the external interrupt corresponding to KEY2:

 

(4) The service function of the external interrupt corresponding to KEY3:

 (5) Write the code of the main main function:

 

3 . Experiment summary.

Through this experiment, I learned what an interrupt is. The interrupt source initiates an interrupt request to the CPU. If the priority level is high, the CPU can interrupt the running of the current program if certain conditions are met. Then according to the interrupt source, find the entry address of the interrupt service subroutine, and go to execute the new program segment. In this experiment, I also mastered the STM32 interrupt programming process, which is of great help to better write programs in the future.

 

 

Guess you like

Origin blog.csdn.net/weixin_45784275/article/details/125425343