STM32 Basic Experiment 4 (Key Interrupt)

Publisher:静默思考Latest update time:2018-12-03 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Key interruption experiment, experiment 2 is key query


1. Experimental Principle


1. Use the buttons in query and interrupt mode 


For buttons, use query and interrupt modes. Query mode is to put a piece of code in a loop, for example, in the main function, you can query in real time and process it when you find a change, while interrupt mode uses hardware to query the change status and then respond. The difference between the two is that one needs to write the query software code by yourself, and the other is to configure the interrupt mode during initialization, and then directly write your processing code in the interrupt. 


2. Implementation steps 


① Initialize the IO port as input (in key.c!)


void KEY_Init(void) //IO initialization

    GPIO_InitTypeDef GPIO_InitStructure;


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE,ENABLE); //Enable PORTA, PORTE clock


    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_4|GPIO_Pin_3;//KEY0-KEY1

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Set to pull-up input

    GPIO_Init(GPIOE, &GPIO_InitStructure); //Initialize GPIOE4,3


    //Initialize WK_UP-->GPIOA.0 pull-down input

    GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 is set to input, pull-down by default     

    GPIO_Init(GPIOA, &GPIO_InitStructure); //Initialize GPIOA.0


}


② Turn on AFIO clock 

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); 

③Set the mapping relationship between IO port and interrupt line. 

void GPIO_EXTILineConfig(); 

④Initialize online interrupts, set trigger conditions, etc. 

EXIT_Init(); 

⑤Configure the interrupt group NVIC and enable interrupt. 

NVIC_Init(); 

⑥Write interrupt service function. 

EXTIx_IRQHandler(); 

⑦ Clear the interrupt flag 

EXTI_ClearITPendingBit();


2. Experimental Code


**exit.c**

#include "exti.h"

#include "led.h"

#include "key.h"

#include "delay.h"

#include "usart.h"

#include "beep.h"


void EXTIX_Init(void)

{


    EXTI_InitTypeDef EXTI_InitStructure;

    NVIC_InitTypeDef NVIC_InitStructure;


    KEY_Init(); //Key port initialization


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //Enable multiplexing function clock




   //GPIOE.3 interrupt line and interrupt initialization configuration falling edge trigger //KEY1

    GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);

    EXTI_InitStructure.EXTI_Line=EXTI_Line3;

    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 

    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;

    EXTI_Init(&EXTI_InitStructure); //Initialize the peripheral EXTI registers according to the parameters specified in EXTI_InitStruct


   //GPIOE.4 interrupt line and interrupt initialization configuration falling edge trigger //KEY0

    GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);

    EXTI_InitStructure.EXTI_Line=EXTI_Line4;

    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 

    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;

    EXTI_Init(&EXTI_InitStructure); //Initialize the peripheral EXTI registers according to the parameters specified in EXTI_InitStruct



   //GPIOA.0 interrupt line and interrupt initialization configuration rising edge trigger PA0 WK_UP

    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0); 

    EXTI_InitStructure.EXTI_Line=EXTI_Line0;

    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;

    EXTI_Init(&EXTI_InitStructure); //Initialize the peripheral EXTI registers according to the parameters specified in EXTI_InitStruct



    NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // Enable the external interrupt channel where the WK_UP button is located

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //Preemption priority 2, 

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03; //Subpriority 3

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable external interrupt channel

    NVIC_Init(&NVIC_InitStructure); 


    NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; // Enable the external interrupt channel where the key KEY1 is located

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //Preemption priority 2 

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01; //Subpriority 1 

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable external interrupt channel

    NVIC_Init(&NVIC_InitStructure); //Initialize peripheral NVIC registers according to the parameters specified in NVIC_InitStruct


    NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn; // Enable the external interrupt channel where the key KEY0 is located

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02; //Preemption priority 2 

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //Subpriority 0 

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable external interrupt channel

    NVIC_Init(&NVIC_InitStructure); //Initialize peripheral NVIC registers according to the parameters specified in NVIC_InitStruct


}


//External interrupt 0 service routine 

void EXTI0_IRQHandler(void)

{

    delay_ms(10); //debounce

    if(WK_UP==1) //WK_UP button

    {                

        BEEP=!BEEP; 

    }

    EXTI_ClearITPendingBit(EXTI_Line0); //Clear the interrupt flag on LINE0  

}



//External interrupt 3 service routine

void EXTI3_IRQHandler(void)

{

    delay_ms(10); //debounce

    if(KEY1==0) //key KEY1

    {                

        LED1=!LED1;

    }        

    EXTI_ClearITPendingBit(EXTI_Line3); //Clear the interrupt flag on LINE3  

}


void EXTI4_IRQHandler(void)

{

    delay_ms(10); //debounce

    if(KEY0==0) //key KEY0

    {

        LED0=!LED0;

        LED1=!LED1; 

    }        

    EXTI_ClearITPendingBit(EXTI_Line4); //Clear the interrupt flag on LINE4  

}


Keywords:STM32 Reference address:STM32 Basic Experiment 4 (Key Interrupt)

Previous article:STM32 serial communication uses interrupt mode to send and receive
Next article:STM32 Basic Experiment 3 (Serial Communication)

Recommended ReadingLatest update time:2024-11-23 07:57

STM32 output comparison mode outputs pulses with fixed frequency
//The following is the TIM configuration program   void TIM1_PWM_INIT(void) //TIM1 configures PWM output parameters {     u32 temp_fre,temp_duty;     TempC = 60; //Set the initial frequency value     TempD = 4; //Set the initial duty cycle 1 / 2     temp_fre = (1000000 / TempC) - 1;     temp_duty = ((temp_
[Microcontroller]
Use of assert_param() in STM32
In the STM32 firmware library and the provided routines, assert_param() can be seen everywhere. If you open the stm32f10x_conf.h file in any routine, you can see that assert_param is actually a macro definition; in the firmware library, its function is to detect whether the parameters passed to the function are valid
[Microcontroller]
STM32 Notes 8: Say hello to the PC, basic serial communication
a)               Purpose: Based on the success of the basic experiment, practice the serial port debugging method. After the hardware code is successfully completed, debug the printf redefinition needed for future debugging and fix it in your own library function. b)               Initialization function definition:
[Microcontroller]
The serial port and clock configurations that occur during STM32 library programming are different from the actual ones
A few days ago, I downloaded the STM32 library file from the Internet and built a project myself. Although the project was built successfully, the following problems occurred when debugging the timer and serial port: 1. When adjusting i, I set the timer to 1s, and let the LED turn on and off alternately every second.
[Microcontroller]
How to find the HardFault_Handler problem in STM32
There are two main reasons why HardFault_Handler failure occurs in STM32: 1. Memory overflow or out-of-bounds access. This requires standardizing the code when writing the program yourself, and you need to slowly troubleshoot when you encounter it. 2. Stack overflow. Increase the size of the stack.   How to troublesho
[Microcontroller]
How to find the HardFault_Handler problem in STM32
STM32 timer interrupt setting method
Today I debugged the timer function of STM32. There are many STM32 timers, but the debugging is the same, and the registers are one-to-one. Take TIM2 as an example. I searched a lot about timer settings on the Internet, but most of them are the same version, and they are all for library function operations, which make
[Microcontroller]
Detailed explanation of STM32_NVIC registers
In MDK, the registers related to NVIC are defined as follows:   typedef struct   {         vu32 ISER ; //2 32-bit interrupt enable registers correspond to 60 maskable interrupts       u32 RESERVED0 ;               vu32 ICER ; //2 32-bit interrupt disable registers correspond to 60 maskable interrupts       u32 RSERVED
[Microcontroller]
Detailed explanation of STM32_NVIC registers
IO port status in stm32 low power mode
1. Sleep mode (Cortex-M3 core stops, peripherals run)            At this time, if the IO is not locked, the IO level with external trigger will change. 2. Stop mode (all clocks stop)            At this time, the peripherals have stopped working and keep the original level, whether locked or not 3. Standby mode (1.8
[Microcontroller]
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号