rickiestRick
rickiestRick

Reputation: 55

How to check if a button is pressed twice within 500 ms (PIC16)?

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;
}

Upvotes: 1

Views: 115

Answers (1)

Kozmotronik
Kozmotronik

Reputation: 2520

  • 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.

Upvotes: 1

Related Questions