kkumar326 0 Posted June 6, 2018 Share Posted June 6, 2018 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! Quote Link to post Share on other sites
terjeio 134 Posted June 6, 2018 Share Posted June 6, 2018 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. NurseBob 1 Quote Link to post Share on other sites
NurseBob 111 Posted June 7, 2018 Share Posted June 7, 2018 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. Quote Link to post Share on other sites
tripwire 139 Posted June 9, 2018 Share Posted June 9, 2018 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. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.