当前位置:网站首页>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
边栏推荐
- Management and use of DokuWiki (supplementary)
- Anonymous structure in C language
- Hardware 3 -- function of voltage follower
- MySQL之MHA高可用集群
- Detailed explanation of pragma usage
- [trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
- Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
- Consul installation
- Simple design description of MIC circuit of ECM mobile phone
- Semiconductor devices (III) FET
猜你喜欢

Process communication mode between different hosts -- socket

Compilation warning solution sorting in Quartus II

MHA High available Cluster for MySQL

Simple design description of MIC circuit of ECM mobile phone

How to copy formatted notepad++ text?

Network communication process

Talk about the circuit use of TVs tube

Installation and use of libjpeg and ligpng

Wifi-802.11 negotiation rate table

Consul installation
随机推荐
My-basic application 2: my-basic installation and operation
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
MHA High available Cluster for MySQL
Reasons for rapid wear of conductive slip rings
Shape template matching based on Halcon learning [viii] PM_ multiple_ models. Hdev routine
Some tips for using source insight (solve the problem of selecting all)
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Bluetooth hc-05 pairing process and precautions
C WinForm [exit application] - practice 3
Relationship between line voltage and phase voltage, line current and phase current
Naming rules for FreeRTOS
Zero length array in GNU C
Create inf module in AMI code
What are the test items of power battery ul2580
Sql Server的存儲過程詳解
C WinForm [display real-time time in the status bar] - practical exercise 1
Baiwen 7-day smart home learning experience of Internet of things
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
Hardware and software solution of FPGA key chattering elimination
General makefile (I) single C language compilation template