如何检查按钮是否在 500 毫秒内按下两次(PIC16)?

发布于 2025-01-20 10:14:56 字数 819 浏览 3 评论 0原文

我有一个连接到RB1的开关按钮。此按钮可作为重置,但需要将其按两次才能正常工作。如果第二压在500毫秒后发生(相对于初始按下),则不会做任何事情。

我认为我应该利用计时器或CCP模块,但我不确定如何实施这些模块。你建议什么?

编辑(我的ISR):

void interrupt ISR(void){
    GIE = 0;
    if(INTF){
        INTF = 0;
        if(reset_press == 0){
            TMR1 = 0x0BDC;              // counter starts counting at 0x0BDC (3036)
            TMR1ON = 1;                 // Turns on Timer1 (T1CON reg)
        }
        reset_press++;
    }
    else if(TMR1IF==1){                 // checks Timer1 interrupt flag 
        TMR1IF = 0;                     // clears interrupt flag
        if(reset_press == 2){
            reset_count();              // reset function
        }
        TMR1ON = 0;                     // Turns off Timer1 (T1CON reg)
        reset_press = 0; 
    }
    GIE = 1;
}

I have a switch button that is connected to RB1. This button acts as a reset but it needs to be pressed twice for it to work. If the second press happens after 500 ms (relative to the initial press), it will do nothing.

I think I should make use of timers or ccp module but I am not sure how to implement those. What do you suggest?

Edit (My ISR):

void interrupt ISR(void){
    GIE = 0;
    if(INTF){
        INTF = 0;
        if(reset_press == 0){
            TMR1 = 0x0BDC;              // counter starts counting at 0x0BDC (3036)
            TMR1ON = 1;                 // Turns on Timer1 (T1CON reg)
        }
        reset_press++;
    }
    else if(TMR1IF==1){                 // checks Timer1 interrupt flag 
        TMR1IF = 0;                     // clears interrupt flag
        if(reset_press == 2){
            reset_count();              // reset function
        }
        TMR1ON = 0;                     // Turns off Timer1 (T1CON reg)
        reset_press = 0; 
    }
    GIE = 1;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡墨 2025-01-27 10:14:56
  • 在初始化时将定时器或 CCP 设置为 500ms,但不要触发它。 (您可能需要使用额外的字节来保存与 500ms 相对应的值,具体取决于振荡器频率)。
  • 进入main的超级循环后,检测按钮按下;使用标志或计数器来计算按钮按下次数。一旦检测到第一次按下,就启动 500 毫秒计时器。
  • 如果500ms的时间又过去了(你可以在中断中检测到它),这意味着你将什么也不做,所以重置一切;按钮按下计数器、触发计时器等。
  • 同时在500ms之前没有重复,如果检测到第二次按下;也就是说,按下计数器的值必须为 2,然后您执行您想要的操作,然后重置所有内容以进行接下来的两次按钮按下检测。
  • Setup the timer or CCP for 500ms once in initialisation, but don't fire it. (You may have to use an additional byte to achive to hold a value that corresponds to 500ms depending on the osc freq).
  • After you enter main's superloop, detect the button press; either use a flag or a counter to count the button press. As soon as you detect the first press,fire the 500ms timer.
  • If the 500ms time has relapsed (you can detect it in the interrupts), that means that you will do nothing, so reset everything; button press counter, fired timer etc.
  • Meanwhile before the 500ms has not been relapsed, if you detect a second press; that is the press counter value must be 2, then you execute what you wish and then reset everything for the next twice button press detection.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文