按键触发外部中断
1.1EXTI简介
STM32F10x外部中断/事件控制器(EXTI)包含多达20个用于产生事件/中断请求的边沿检测器。EXTI的每根输入线都可单独进行配置,以选择类型(中断或事件)和相应的触发事件(上升沿触发、下降沿触发或边沿触发),还可以独立地被屏蔽。
3.或门电路,只要有一个高电平就为高电平
4.与门电路,都是1才为1,1&1=1,取决于3输出,对应的~请求挂起寄存器
脉冲信号~定时器
APB2总线上,外设时钟
通道名
k_up是上升沿
k1,k2,k3是下降沿,加上拉电阻
以上是程序步骤
EXTI.C
#include "exti.h"
#include "SysTick.h"
#include "led.h"
#include "key.h"
void My_EXTI_Init()
{
NVIC_InitTypeDef NVIC_InitStructure;//结构体初始化
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//开启AFIO
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);//IO口与中断线的映射
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;//中断NVIC
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占式优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3; //响应优先级,0到3选择
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占式优先级
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;//抢占式优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占式优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //响应优先级,数字越小,越高
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
EXTI_InitStructure.EXTI_Line=EXTI_Line0;//外部中断
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line=EXTI_Line2|EXTI_Line3|EXTI_Line4;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void EXTI0_IRQHandler(void)//外部中断
{
if(EXTI_GetITStatus(EXTI_Line0)==1)//获取中断标志
{
delay_ms(10);//消抖
if(K_UP==1) //判断
{
led2=0; //亮
}
}
EXTI_ClearITPendingBit(EXTI_Line0);//清除标志
}
void EXTI2_IRQHandler(void)//外部中断
{
if(EXTI_GetITStatus(EXTI_Line2)==1)//获取中断标志
{
delay_ms(10);//消抖
if(K_LEFT==0)//连接地线
{
led3=1; //灭
}
}
EXTI_ClearITPendingBit(EXTI_Line2);//清除标志
}
void EXTI3_IRQHandler(void)//外部中断
{
if(EXTI_GetITStatus(EXTI_Line3)==1)//获取中断标志
{
delay_ms(10);//消抖
if(K_DOWN==0)//连接地线
{
led2=1; //灭
}
}
EXTI_ClearITPendingBit(EXTI_Line3);//清除标志
}
void EXTI4_IRQHandler(void)//外部中断
{
if(EXTI_GetITStatus(EXTI_Line4)==1)//获取中断标志
{
delay_ms(10);//消抖
if(K_RIGHT==0)//连接地线
{
led3=0; //亮
}
}
EXTI_ClearITPendingBit(EXTI_Line4);//清除标志
}
exti.h
#ifndef _exti_H
#define _exti_H
#include "system.h"
void My_EXTI_Init(void);
#endif
main.c
#include "system.h"
#include "led.h"
#include "SysTick.h"
#include "key.h"
#include "exti.h"
int main()
{
u8 i=0;
SysTick_Init(72);//72M
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();//初始化
KEY_Init();
My_EXTI_Init();
while(1)
{
i++;
if(i%20==0)
{
led1=!led1;
}
delay_ms(10);
}
}
详细数据手册