炒冷饭系列-STM32F103之外部中断
作者:互联网
出现两个问题:1、WK_UP按键为什么是下拉输入(WK_UP按键外部接高电平的原因?)
2、 GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2|GPIO_PinSource3|GPIO_PinSource4);//不能合起来写
EXTI_InitStruct.EXTI_Line=EXTI_Line2|EXTI_Line3|EXTI_Line4;//但是这个可以合起来写。
#include "stm32f10x.h"
#include "BEEP.h"
#include "LED.h"
#include "delay.h"
#include "usart.h"
void myExitHandler();
int main(void)
{
//设置中断分组2
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();
BEEP_Init();
uart_init(115200);
myExitHandler();
while(1);
}
void myExitHandler()
{
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
//1、使能时钟 GPIO口时钟,外部中断时钟 GPIOE2,3,4 //KEY0-KEY2 WK_UP-->GPIOA.0
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO,ENABLE);
//2、初始化GPIO口,作为输入口
//WK_UP-->GPIOA.0
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPD;//下拉输入
// GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;//上拉输入
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
//KEY0-KEY2 ->GPIOE2,3,4
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;//上拉输入
// GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPD;//下拉输入
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOE,&GPIO_InitStruct);
//3、IO口映射到中断线上
//WK_UP-->GPIOA.0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
//4、初始化线上中断, 设置触发条件
EXTI_InitStruct.EXTI_Line=EXTI_Line0;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;//上升沿触发
EXTI_Init(&EXTI_InitStruct);
//KEY0-KEY2 ->GPIOE2,3,4
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
//4、初始化线上中断, 设置触发条件
EXTI_InitStruct.EXTI_Line=EXTI_Line2;
EXTI_InitStruct.EXTI_LineCmd=ENABLE;
EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling;//下升沿触发
EXTI_Init(&EXTI_InitStruct);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
EXTI_InitStruct.EXTI_Line=EXTI_Line3;
EXTI_Init(&EXTI_InitStruct);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
EXTI_InitStruct.EXTI_Line=EXTI_Line4;
EXTI_Init(&EXTI_InitStruct);
//5、配置中断参数
//WK_UP-->GPIOA.0
NVIC_InitStruct.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=3;
NVIC_Init(&NVIC_InitStruct);
//KEY0-->GPIOE2
NVIC_InitStruct.NVIC_IRQChannel=EXTI2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
//KEY1-->GPIOE3
NVIC_InitStruct.NVIC_IRQChannel=EXTI3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
//KEY2-->GPIOE4
NVIC_InitStruct.NVIC_IRQChannel=EXTI4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
}
//WK_UP GPIOA.0控制蜂鸣器,按一次叫,再按一次停;
void EXTI0_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line0))
{
BEEP=~BEEP;
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
//KEY0 GPIOE2 则同时控制 DS0 和 DS1,按一次,他们的状态就翻转一次
void EXTI2_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line2))
{
LED1=~LED1;
LED0=~LED0;
EXTI_ClearITPendingBit(EXTI_Line2);
}
}
//KEY1 GPIOE3 控制 DS1,按一次亮,再按一次灭;
void EXTI3_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line3))
{
LED1=~LED1;
EXTI_ClearITPendingBit(EXTI_Line3);
}
}
//KEY2 GPIOE4 控制 DS0,按一次亮,再按一次灭;
void EXTI4_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line4))
{
LED0=~LED0;
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
标签:STM32F103,中断,炒冷饭,NVIC,Init,Mode,InitStruct,GPIO,EXTI 来源: https://blog.csdn.net/chenyouledashen/article/details/120607816