当前位置:网站首页>STM32 --- configuration of external interrupt
STM32 --- configuration of external interrupt
2022-07-05 08:15:00 【chen_ bx】
STM32--- Configuration of external interrupts
Sample functions
Use external interrupt , When you press the key 0 Trigger interrupt on when pressed LED0, When you press the key 1 Trigger interrupt on when pressed LED1, When you press the key 3 Trigger interrupt when pressed , close LED0 and LED1.
Configure external interrupt steps
① Set interrupt grouping
stay main Set interrupt grouping in function
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
② Turn on AFIO The clock
stay Exti_Init Enable in function AFIO The clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
③ initialization GPIO
example :
void KEY_Init(){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
④ call Exti_Init Initialization function
EXTI_InitTypeDef The structure is as follows
typedef struct
{
uint32_t EXTI_Line; /*!< Specifies the EXTI lines to be enabled or disabled. This parameter can be any combination of @ref EXTI_Lines */
EXTIMode_TypeDef EXTI_Mode; /*!< Specifies the mode for the EXTI lines. This parameter can be a value of @ref EXTIMode_TypeDef */
EXTITrigger_TypeDef EXTI_Trigger; /*!< Specifies the trigger signal active edge for the EXTI lines. This parameter can be a value of @ref EXTIMode_TypeDef */
FunctionalState EXTI_LineCmd; /*!< Specifies the new state of the selected EXTI lines. This parameter can be set either to ENABLE or DISABLE */
}EXTI_InitTypeDef;
Yes EXTI To initialize
example :
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3); // Set up IO Mapping relationship between port and interrupt line
EXTI_InitStructure.EXTI_Line=EXTI_Line3; // Configure interrupt line
EXTI_InitStructure.EXTI_LineCmd=ENABLE; // Can make OR
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt; // Set the mode ; interrupt
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;// Set the interrupt trigger mode
EXTI_Init(&EXTI_InitStructure);
⑤ Configure interrupt grouping , And can interrupt
example :
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // Enable key WK_UP External interrupt channel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // preemption 2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; // Sub priority 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channels
NVIC_Init(&NVIC_InitStructure);
⑥ Write interrupt service function
example :
void EXTI0_IRQHandler(void){
delay(10);// Desquamation
if(KEY_UP==1) //WK_UP Key
{
LED0_OFF; // close LED0
LED1_OFF; // close LED1
}
EXTI_ClearITPendingBit(EXTI_Line0); // eliminate LINE0 Interrupt flag bit on
}
Complete code
main The function code is as follows :
#include "stm32f10x.h"
#include "led.h"
#include "sys.h"
#include "key.h"
#include "exti.h"
int main(void){
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();
Exti_Init();
while(1);
}
led.c The code is as follows :
#include "led.h"
#include "stm32f10x.h"
void LED_Init(){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
led.h The code is as follows :
#ifndef __LED_H
#define __LED_H
void LED_Init(void);
#define LED0_ON GPIO_ResetBits(GPIOB,GPIO_Pin_5);
#define LED0_OFF GPIO_SetBits(GPIOB,GPIO_Pin_5);
#define LED1_ON GPIO_ResetBits(GPIOE,GPIO_Pin_5);
#define LED1_OFF GPIO_SetBits(GPIOE,GPIO_Pin_5);
#endif
key.c The code is as follows :
#include "key.h"
#include "stm32f10x.h"
void KEY_Init(){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3|GPIO_Pin_4;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
key.h The code is as follows :
#ifndef __KEY_H
#define __KEY_H
#define KEY0 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3)
#define KEY1 GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4)
#define KEY_UP GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
void KEY_Init(void);
#endif
exti.c The code is as follows
#include "exti.h"
#include "key.h"
#include "led.h"
#include "stm32f10x.h"
void delay(int len)
{
volatile unsigned int i = 7200*len;
while(i--);
}
void Exti_Init(){
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); // Can make AFIO The clock
KEY_Init(); // Key initialization (GPIO initialization )
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3); // Set up IO Mapping relationship between port and interrupt line
EXTI_InitStructure.EXTI_Line=EXTI_Line3; // Configure interrupt line
EXTI_InitStructure.EXTI_LineCmd=ENABLE; // Can make OR
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt; // Set the mode ; interrupt
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;// Set the interrupt trigger mode
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line=EXTI_Line4;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // Enable key WK_UP External interrupt channel
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; // preemption 2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; // Sub priority 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable external interrupt channels
NVIC_Init(&NVIC_InitStructure);
}
void EXTI0_IRQHandler(void){
delay(10);// Desquamation
if(KEY_UP==1) //WK_UP Key
{
LED0_OFF; // close LED0
LED1_OFF; // close LED1
}
EXTI_ClearITPendingBit(EXTI_Line0); // eliminate LINE0 Interrupt flag bit on
}
void EXTI3_IRQHandler(void){
delay(10);// Desquamation
if(KEY0==0) //KEY0 Key
{
LED0_ON; // open LED0
}
EXTI_ClearITPendingBit(EXTI_Line3); // eliminate LINE3 Interrupt flag bit on
}
void EXTI4_IRQHandler(void){
delay(10);// Desquamation
if(KEY1==0) //KEY1 Key
{
LED1_ON; // open LED1
}
EXTI_ClearITPendingBit(EXTI_Line4); // eliminate LINE4 Interrupt flag bit on
}
exti.h The code is as follows :
#ifndef __EXTI_H
#define __EXTI_H
void Exti_Init(void);
#endif
边栏推荐
- Create inf module in AMI code
- Keil use details -- magic wand
- PMSM dead time compensation
- Take you to understand the working principle of lithium battery protection board
- UEFI development learning 6 - creation of protocol
- UEFI development learning series
- Naming rules for FreeRTOS
- [trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
- Installation and use of libjpeg and ligpng
- Correlation based template matching based on Halcon learning [II] find_ ncc_ model_ defocused_ precision. hdev
猜你喜欢
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Working principle and type selection of common mode inductor
UEFI development learning 5 - simple use of protocol
The firmware of the connected j-link does not support the following memory access
Create inf module in AMI code
Altium designer learning (I)
Halcon's practice based on shape template matching [2]
List of linked lists
Compilation warning solution sorting in Quartus II
Programming knowledge -- basis of C language
随机推荐
OLED 0.96 inch test
Development tools -- gcc compiler usage
C WinForm [display real-time time in the status bar] - practical exercise 1
PMSM dead time compensation
Communication standard -- communication protocol
Tailq of linked list
Explication de la procédure stockée pour SQL Server
2021-10-28
Soem EtherCAT source code analysis I (data type definition)
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,LU分解(LU Decomposition)源程序
Gradle复合构建
Why is 1900 not a leap year
C WinForm [change the position of the form after running] - Practical Exercise 4
Weidongshan Internet of things learning lesson 1
Reasons for rapid wear of conductive slip rings
Adaptive filter
List of linked lists
动力电池UL2580测试项目包括哪些
FIO测试硬盘性能参数和实例详细总结(附源码)