Hi,
I'm a newbie and I have a problem with debounce of 3 switches in an interrupt. Each switch toggles a led with an interrupt in P1. this software works when i press the switch slowly but when i press faster it doesn't work correctly (goes on an off not according to the button pressed). I use the example of RobG that he explains in this topic using the watchdog (Thanks RobG):
I post the code, maybe you can help me resolve the problem?
#include <msp430.h>
/*
* main.c
*/
#define button1 BIT5
#define button2 BIT4
#define button3 BIT3
#define led1 BIT0
#define led2 BIT1
#define led3 BIT2
#define ledTotal (led1 | led2| led3)
#define buttonTotal (button1 | button2| button3)
void main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
BCSCTL1 = CALBC1_1MHZ;// Use 1Mhz cal data for DCO
DCOCTL = CALDCO_1MHZ;// Use 1Mhz cal data for DCO
P1DIR |= ledTotal;
P1OUT |= buttonTotal; //on pullups
P1OUT &=~ ledTotal;
P1REN |= buttonTotal; // up pullups
P1IES |= buttonTotal; //interrupT hig to low edge
P1IFG &=~ buttonTotal; //Flag to 0
P1IE |= buttonTotal; //Activ interrupt button
__enable_interrupt();
while(1){
}
}
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void) {
P1IE &=~ buttonTotal; //disable interrupt
WDTCTL = WDT_MDLY_32; // start WDT, interrupt after 32ms (depending on your clock)
IFG1 &= ~WDTIFG; // clear WDT flag
IE1 |= WDTIE; // enable WDT interrupt
if((P1IN & button1)==0x00){
P1OUT ^= led1;
}
if((P1IN & button2)==0x00){
P1OUT ^= led2;
}
if((P1IN & button3)==0x00){
P1OUT ^= led3;
}
P1IE |= buttonTotal; //activ interrupt
P1IFG &=~ buttonTotal; //clean flag
}
#pragma vector=WDT_VECTOR
__interrupt void WDT_ISR(void)
{
IE1 &= ~WDTIE; // disable WDT interrupt
IFG1 &= ~WDTIFG; // clear flag
WDTCTL = WDTPW + WDTHOLD; // hold WDT
P1IE |= buttonTotal; // enable interrupts on S2
}
thanks for your time
