Sign in to follow this  
Followers 0
morelius21

Debounce switch routine watchdog not work

3 posts in this topic

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

main.c

Share this post


Link to post
Share on other sites

Because you are reenabling the P1 Interrupt inside of the P1 Interrupt, making the Watchdog pointless.

 

Remove "P1IE |= buttonTotal;    //activ interrupt" from the interrupt. Now the watchdog should enable the interrupt after a single 32mS interval triggers the watchdog interrupt.

morelius21 likes this

Share this post


Link to post
Share on other sites
#2. Thanks for the reply.

 

I understand the problem but when i make this modification don't work properly yet. I try to put the reads in P1IN inside whatchdog ISR and now it  works. I wasn't understanding the function of the watchdog. I post the code. is possible that something can be better.

 



#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



}


#pragma vector=WDT_VECTOR
__interrupt void WDT_ISR(void)
{
IE1 &= ~WDTIE; // disable WDT interrupt
IFG1 &= ~WDTIFG; // clear flag
WDTCTL = WDTPW + WDTHOLD; // hold WDT

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

}



 

 

Thanks for all people

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0