其他分享
首页 > 其他分享> > 按键外中断控制

按键外中断控制

作者:互联网

Main.c:

#include "stm32f10x.h"
#include "delay.h"
#include "key.h"
#include "led.h"
#include "sys.h"

int main()
{
	NVIC_Configuration();
	Led_Init();
	Key_Exti_Init();
	while(1)
	{
		LED1=0;LED2=LED3=LED4=1;delay(400);
		LED2=0;LED1=LED3=LED4=1;delay(400);
		LED3=0;LED2=LED1=LED4=1;delay(400);
		LED4=0;LED2=LED3=LED1=1;delay(400);
	}
}


key.h:

#ifndef __KEY_H
#define __KEY_H

#include "sys.h"


void Key_Exti_Init(void);
void EXTI0_IRQHandler(void);
void EXTI1_IRQHandler(void);
void EXTI2_IRQHandler(void);
void EXTI3_IRQHandler(void);

#endif

key.c:

#include "key.h"
#include "stm32f10x.h"
#include "delay.h"
#include "led.h"
#include "sys.h"

void Key_Exti_Init(void)
{
	//启用GPIO复用
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); 

	
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1 |GPIO_Pin_2 |GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA, &GPIO_InitStructure); 
	
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); 	//没有下划线 把哪些引脚配置为外中断引脚
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource3);
	
	//配置0-3中断线
	EXTI_InitTypeDef EXTI_InitStructure;
	EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1 | EXTI_Line2 | EXTI_Line3;
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //中断模式
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
	EXTI_Init(&EXTI_InitStructure); 
	
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;	//抢占优先级 分组2中为0-3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;	//响应优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure); 
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;	//抢占优先级 分组2中为0-3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;	//响应优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure); 
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;	//抢占优先级 分组2中为0-3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;	//响应优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure); 
	
	NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;	//抢占优先级 分组2中为0-3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;	//响应优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure); 
}

void EXTI0_IRQHandler()
{
	u8 i;
	for(i=0;i<20;i++)
	{
		LED1=~LED1;
		delay(300);
	}
}

void EXTI1_IRQHandler()
{
	u8 i;
	for(i=0;i<20;i++)
	{
		LED2=~LED2;
		delay(300);
	}
}

void EXTI2_IRQHandler()
{
	u8 i;
	for(i=0;i<20;i++)
	{
		LED3=~LED3;
		delay(300);
	}
}

void EXTI3_IRQHandler()
{
	u8 i;
	for(i=0;i<20;i++)
	{
		LED4=~LED4;
		delay(300);
	}
}

led.h

#ifndef _LED_H
#define _LED_H

#include "sys.h"

#define LED1 PBout(8)
#define LED2 PBout(9)
#define LED3 PBout(10)
#define LED4 PBout(11)



void Led_Init(void);

#endif

led.c

#include "led.h"
#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"

void Led_Init(void)
{
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); 

	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9 |GPIO_Pin_10 |GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB, &GPIO_InitStructure); 
	
	LED1=LED2=LED3=LED4=1;
	

}

标签:控制,中断,void,NVIC,InitStructure,按键,GPIO,include,EXTI
来源: https://blog.csdn.net/weixin_45705007/article/details/118526664