Jump to content
Sign in to follow this  
kkumar326

MSP430G2553 - Please let me know why my code is not working.

Recommended Posts

I'm trying to create a toggle button but the code is not working. The code is as shown below:

#include <msp430g2553.h>
int main(void)
{
    WDTCTL = WDTPW + WDTHOLD;    // stop watchdog timer
    P1DIR &= ~BIT3; //P1.3 i/p
    P1REN |=  BIT3; //P1.3 enable pullup resistor
    P1IES |=  BIT3; //P1.3 high to low transition
    P1IFG &= ~BIT3; //P1.3 clear interrupt flag
    P1IE  |=  BIT3; //enable P1.3 interrupt
    P1DIR |=  BIT0; //P1.0 o/p
    P1OUT &= ~BIT0; //clear P1.0

    _BIS_SR(LPM0_bits + GIE); //enter LPM0 with interrupts enabled
}

#pragma vector = PORT1_VECTOR
__interrupt void Port1(void)
{
  P1IFG &= ~BIT3; //clear P1IFG
  P1OUT ^= BIT0;  //toggle LED at P1.0
}

Please let me know why it is not working. I am using MS430G2553 launchpad kit.

Thanks!

Share this post


Link to post
Share on other sites

Did you debounce the switch signal with some kind of circuit first? If not I believe you need add a debouncer, possibly in code. MAX6816 is an easy to use chip but adds parts and cost to the design, doing it by code only needs some programming effort to make it work.

Share this post


Link to post
Share on other sites

I agree with @terjeio it looks like it's a debounce issue.  I assume by "not working" you mean that the LED state toggles inconsistently? I ran the code and it "works" in that it turns the LED on or off, but it's inconsistent.  Time to do a little reading on how to debounce switches. It can be in software, hardware or a combination of the two.  One "sort of classic" is to respond to the changed button state, and then in a few milliseconds check the button state again to see if it's still down, then switch the LED.

Share this post


Link to post
Share on other sites

Another thing that might make this code unreliable is that you aren't setting P1OUT bit 3 high. Setting a bit in PxREN connects the pull resistor to the corresponding pin, but the pull direction is given by the value of the relevant PxOUT bit.

The value of PxOUT is unspecified on startup and power-on clear, so you might be getting a pulldown resistor instead of pullup.

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  

×