Jump to content
43oh

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!

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.

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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...