Normally keypresses are not direct change from one state to other , instead its a change with bit noise in between where the state seems undefined. To avoid the situation the common method is to recheck after certain delay
if(PINState==Pressed)
{
delay(few_microsec);
if(PINState==Pressed)
{
ActionKeyPress();
}
}But if we are dealing with interrupt
PIN_ISR()
{
setkeypressflag();
}
main()
{
if (keypressflagset)
if(PINState==Pressed)
{
DoKeyPressAction();
}
}Now the most advanced one with timer or WatchdogTimer
#pragma vector=PORT1_VECTOR
__interrupt void P1_Function()
{
count=0; //Reset count
TACTL|=TASSEL_2+MC_1+TAIE; //Start Timer0 with SMCLK clock source, UP mode and enable overflow interrupt
state=(P1IN&BIT3)>>3; //Save the state of the switch
P1IE&=~BIT3; //Disable interrupt on P1.3, now the Timer will take care of Debouncing
P1IFG&=~BIT3; // Reset Port1 interrupt flag
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TMR0()
{
if(TAIV==TA0IV_TAIFG) //Check if Timer overflow caused the interrupt
//This would be required in projects where multiple interrupts have
//the same interrupt vector. Here it is only optional.
{
if(state==((P1IN&BIT3)>>3)) //If the state of the LED is the same
count++; //Increment the counter variable
else
{
count=0; //If not same, reset the counter variable
state=((P1IN&BIT3)>>3); //And save the present state of the switch
}
if(count==10) //If the state has been consistently the same
{
if(state==0) //If the switch was pressed
P1OUT^=BIT0; //Toggle the LED
P1IE|=BIT3; //We have handled the debouncing, now we again enable interrupt on P1.3, for it to again detect switch bounce
TACTL=0; //Stop the Timer
TACTL|=TACLR; //Clear the Timer counter
}
TACTL&=~(TAIFG); //Reset the interrupt flag
}
}
Comments
Post a Comment