OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3280|回复: 2

用STM32精英板写外部中断的时候KEY_UP按钮不灵敏

[复制链接]

2

主题

3

帖子

0

精华

新手入门

积分
19
金钱
19
注册时间
2020-12-12
在线时间
3 小时
发表于 2020-12-15 20:58:16 | 显示全部楼层 |阅读模式
1金钱
有时候要按几下才会有反应,KEY0,KEY1按一下就行

最佳答案

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

22

主题

2251

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
4477
金钱
4477
注册时间
2013-4-22
在线时间
336 小时
发表于 2020-12-15 20:58:17 | 显示全部楼层
回复

使用道具 举报

2

主题

3

帖子

0

精华

新手入门

积分
19
金钱
19
注册时间
2020-12-12
在线时间
3 小时
 楼主| 发表于 2020-12-16 17:03:46 | 显示全部楼层
  1. //主函数
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include "led.h"
  5. #include "key.h"
  6. #include "beep.h"
  7. #include "exti.h"
  8. #include "sys.h"


  9. int main(void)
  10. {
  11.         delay_init();                                //延时函数初始化
  12.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);                                //设置NVIC中断分组2?2位抢占优先级,2位响应优先级
  13.         uart_init(115200);                        //设置波特率
  14.         Key_Init();                                //按钮初始化
  15.         Beep_Init();                        //蜂鸣器初始化
  16.         Led_Init();                                //LED灯初始化
  17.         Exti_Init();                        //外部中断初始化
  18.         LED0 = 0;                                //点亮LED0
  19.         while(1)
  20.         {
  21.                 printf("OK\r\n");
  22.                 delay_ms(1000);
  23.         }
  24. }
  25. //中断函数
  26. #include "exti.h"

  27. void Exti_Init(void )
  28. {
  29.         EXTI_InitTypeDef EXTI_InitStruct;
  30.         NVIC_InitTypeDef NVIC_InitStruct;
  31.        
  32.         Key_Init();
  33.        
  34.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
  35.        
  36.         //PE.4(KEY0)
  37.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource4);
  38.         EXTI_InitStruct.EXTI_Line = EXTI_Line4;                        //选择中断线4
  39.         EXTI_InitStruct.EXTI_LineCmd = ENABLE;                        //使能
  40.         EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;                        //中断还是事件
  41.         EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;                //上升沿还是下降沿或者双边沿
  42.         EXTI_Init(&EXTI_InitStruct);
  43.        
  44.         //PE.3(KEY1)
  45.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
  46.         EXTI_InitStruct.EXTI_Line = EXTI_Line3;
  47.         EXTI_Init(&EXTI_InitStruct);
  48.        
  49.         //PA.0(KEY_UP)
  50.         GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
  51.         EXTI_InitStruct.EXTI_Line = EXTI_Line0;                        //选择中断线
  52.         EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;                //上升沿还是下降沿或者双边沿
  53.         EXTI_Init(&EXTI_InitStruct);
  54.        
  55.         NVIC_InitStruct.NVIC_IRQChannel = EXTI4_IRQn;                                //开启EXTI4_IRQn通道
  56.         NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  57.         NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;                //抢占优先级2
  58.         NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;                                //响应优先级1
  59.         NVIC_Init(&NVIC_InitStruct);
  60.        
  61.         NVIC_InitStruct.NVIC_IRQChannel = EXTI3_IRQn;                        //开启EXTI3_IRQn通道
  62.         NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;                //抢占优先级2
  63.         NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;                                //响应优先级1
  64.         NVIC_Init(&NVIC_InitStruct);
  65.        
  66.         NVIC_InitStruct.NVIC_IRQChannel = EXTI0_IRQn;                        //开启EXTI0_IRQn通道
  67.         NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;                //抢占优先级2
  68.         NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;                        //响应优先级2
  69.         NVIC_Init(&NVIC_InitStruct);

  70. }


  71. void EXTI4_IRQHandler(void )
  72. {
  73.         delay_ms(10);                        //防抖
  74.         if(KEY0 == 0){
  75.                 LED0 = !LED0;
  76.                 LED1 = !LED1;
  77.         }
  78.         EXTI_ClearITPendingBit(EXTI_Line4);                                //标志位清零
  79. }

  80. void EXTI3_IRQHandler(void )
  81. {
  82.         delay_ms(10);                        //防抖
  83.         if(KEY1 == 0){
  84.                 LED0 = !LED0;
  85.         }
  86.         EXTI_ClearITPendingBit(EXTI_Line3);
  87. }
  88. void EXTI0_IRQHandler(void )
  89. {
  90.         delay_ms(10);                        //防抖
  91.         if(KEY2 == 0){
  92.                 BEEP = !BEEP;
  93.         }
  94.         EXTI_ClearITPendingBit(EXTI_Line0);
  95. }

复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-14 18:54

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表