[PIC®/AVR®/dsPIC®产品] 中断双边沿检测按键动作

[复制链接]
1481|18
 楼主| xuanhuanzi 发表于 2023-9-14 16:33 | 显示全部楼层 |阅读模式
https://bbs.21ic.com/icview-3325360-1-1.html
参靠了上面的神贴思路,尝试了一下啊。
  1. #include "mcc_generated_files/system/system.h"

  2. bool SW_flag = 0; //记录是否发生了下降沿中断
  3. bool SW_Button_active_flag =0; //有效按键激活标记


  4. void SW_Button_Interrupt_Handle(void)
  5. {
  6.     if(SW_GetValue()==LOW)
  7.     {
  8.         SW_flag = 1;
  9.     }
  10.     else if( ( SW_GetValue()==HIGH ) && (SW_flag == 1) )
  11.     {
  12.         SW_Button_active_flag =1;
  13.         SW_flag = 0;
  14.     }
  15. }


  16. /*
  17.     Main application
  18. */


  19. int main(void)
  20. {
  21.     SYSTEM_Initialize();

  22.     // If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
  23.     // If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global Interrupts
  24.     // Use the following macros to:

  25.     // Enable the Global Interrupts
  26.     INTERRUPT_GlobalInterruptEnable();

  27.     // Disable the Global Interrupts
  28.     //INTERRUPT_GlobalInterruptDisable();

  29.     SW_SetInterruptHandler(SW_Button_Interrupt_Handle);

  30.     while(1)
  31.     {
  32.         if(SW_Button_active_flag == 1)
  33.         {
  34.             SW_Button_active_flag = 0;
  35.             LED_Toggle();
  36.         }
  37.     }   
  38. }
试了一下,效果很好
 楼主| xuanhuanzi 发表于 2023-9-14 16:33 | 显示全部楼层
根据帖子中的说明,如果按键使用一个104电容效果会更好更加稳定。

评论

@forgot :手头不是都有单片机开发板吗,你试试看。  发表于 2023-9-16 21:39
@forgot :不需要滤波啊,我没滤波,用的内置上拉电阻,发现很OK。  发表于 2023-9-16 21:38
滤波抗干扰吗  发表于 2023-9-15 09:13
 楼主| xuanhuanzi 发表于 2023-9-16 21:40 | 显示全部楼层
这个方法是很不错的,不过部分按键可能机械问题会导致按了没反应,那是按压力度不够。这个无妨。主要避免按一次触发多次的情况。
tpgf 发表于 2023-10-12 16:29 | 显示全部楼层
这样的操作无形中就减少了误操作的几率
paotangsan 发表于 2023-10-12 16:46 | 显示全部楼层
xuanhuanzi 发表于 2023-9-14 16:33
根据帖子中的说明,如果按键使用一个104电容效果会更好更加稳定。

这种软件方式已经在一定程度上起到了滤波的作用了
wakayi 发表于 2023-10-12 17:03 | 显示全部楼层
频繁按压也不会频繁的相应了是吧
xiaoqizi 发表于 2023-10-12 18:33 | 显示全部楼层
采用这种方式的话  就不需要做延时防抖了
wowu 发表于 2023-10-12 19:07 | 显示全部楼层
这种方式充分利用了引脚的自我保护机制
renzheshengui 发表于 2023-10-12 19:41 | 显示全部楼层
xuanhuanzi 发表于 2023-9-16 21:40
这个方法是很不错的,不过部分按键可能机械问题会导致按了没反应,那是按压力度不够。这个无妨。主要避免按 ...

只要不是频繁按压 基本不会出现无法识别的现象
Bowclad 发表于 2023-11-23 16:22 来自手机 | 显示全部楼层
加电容是为了消抖吧
 楼主| xuanhuanzi 发表于 2023-11-27 14:27 | 显示全部楼层
Bowclad 发表于 2023-11-23 16:22
加电容是为了消抖吧

是的,推荐加104电容,也就是0.1UF,效果嘎嘎好使。
Jacquetry 发表于 2023-11-27 14:28 来自手机 | 显示全部楼层
自我保护机制是什么啊
AloneKaven 发表于 2023-11-28 10:45 来自手机 | 显示全部楼层
加电容是不是容易坏啊
Stahan 发表于 2023-11-30 23:22 | 显示全部楼层
元件多了自然容易坏
星辰大海不退缩 发表于 2023-12-1 08:28 | 显示全部楼层
这样的操作减少了误操作的几率
Henryko 发表于 2023-12-4 11:30 来自手机 | 显示全部楼层
硬件消抖比软件的好多了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部