新手入门
- 积分
- 6
- 金钱
- 6
- 注册时间
- 2023-6-2
- 在线时间
- 0 小时
|
战舰进行外部中断实验,设置了4个按键的外部中断,KEY2按键按一次有时会出现多次中断,做了10ms的防抖,其余按键无此情况。
#include <exit.h>
#include <sys.h>
#include "usart.h"
#include "led.h"
#include "key.h"
#include <delay.h>
#include <usart.h>
void Exit_Init(void)
{
EXTI_InitTypeDef EXTI_InitStructEXIT;
NVIC_InitTypeDef NVIC_InitType;
KEY_Init();
//ʹÄܸ´Óù¦ÄÜʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//GPIOE.3 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢ //KEY1
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
EXTI_InitStructEXIT.EXTI_Line=EXTI_Line3;
EXTI_InitStructEXIT.EXTI_LineCmd=ENABLE;
EXTI_InitStructEXIT.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructEXIT.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_Init(&EXTI_InitStructEXIT);
//GPIOE.2 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢ //KEY2
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);
EXTI_InitStructEXIT.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructEXIT.EXTI_LineCmd=ENABLE;
EXTI_InitStructEXIT.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructEXIT.EXTI_Line=EXTI_Line2;
EXTI_Init(&EXTI_InitStructEXIT);
//GPIOE.4 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢ //KEY0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
EXTI_InitStructEXIT.EXTI_Line=EXTI_Line4;
EXTI_Init(&EXTI_InitStructEXIT);
//GPIOA.0 ÖжÏÏßÒÔ¼°Öжϳõʼ»¯ÅäÖà ϽµÑØ´¥·¢ //WK_UP
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
EXTI_InitStructEXIT.EXTI_Line=EXTI_Line0;
EXTI_InitStructEXIT.EXTI_Trigger=EXTI_Trigger_Rising;
EXTI_Init(&EXTI_InitStructEXIT);
//key0 ÖÐ¶Ï 2£¬0
NVIC_InitType.NVIC_IRQChannel=EXTI4_IRQn;
NVIC_InitType.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitType.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitType.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitType);
//key1 ÖÐ¶Ï 2£¬1
NVIC_InitType.NVIC_IRQChannel=EXTI3_IRQn;
NVIC_InitType.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitType.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitType.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitType);
//key2 ÖÐ¶Ï 2£¬2
NVIC_InitType.NVIC_IRQChannel=EXTI2_IRQn;
NVIC_InitType.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitType.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitType.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitType);
//wk_up ÖÐ¶Ï 3£¬0
NVIC_InitType.NVIC_IRQChannel=EXTI0_IRQn;
NVIC_InitType.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitType.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitType.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitType);
}
void EXTI3_IRQHandler(void)
{
delay_ms(10);
if(KEY1==0)
{
LED1=!LED1;
printf("key1\r\n");
}
EXTI_ClearITPendingBit(EXTI_Line3);
}
void EXTI2_IRQHandler(void)
{
delay_ms(10);
if(KEY2==0)
{
LED0=!LED0;
printf("key2\r\n");
}
EXTI_ClearITPendingBit(EXTI_Line2);
}
void EXTI4_IRQHandler(void)
{
delay_ms(10);
if(KEY0==0)
{
LED0=!LED0;
LED1=!LED1;
printf("key0\r\n");
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
void EXTI0_IRQHandler(void)
{
delay_ms(10);
if(WK_UP==1)
{
LED0=!LED0;
LED1=!LED1;
printf("WK_UP\r\n");
}
EXTI_ClearITPendingBit(EXTI_Line0);
}
|
|