4
\$\begingroup\$

I am using the MSP-EXP430F5438 evaluation board, which has pushbuttons connected to P2.6 and P2.7 and LEDs connected to P1.0 and P1.1.

I want to make it such that P2.6 sets the LEDs on and P2.7 resets them. To do that, I wrote the following code:

#include  "msp430x54x.h"

int main(void)
{
          WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer
          P1DIR |= BIT0 + BIT1;                 // Set P1.0 and P1.1 to output direction
          P2DIR = 0x00;                         // Set the P2 to input direction

          P1OUT = 0x00; // Blank all LEDs

          while(1)
         {
            if(P2IN & 0x40) // When P2.6 is pushed
              P1OUT = 0x03;
            if(P2IN & 0x80) // When P2.7 is pushed
              P1OUT = 0x00;
            __delay_cycles(100000);
         }
}

But it doesn't work properly; sometimes it works erratically (the LEDs turn on when P2.6 is pressed but they don't turn off when P2.7 is pressed), sometimes it doesn't work at all.

Any ideas?

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Check for the switch bounce. \$\endgroup\$ Commented Oct 5, 2012 at 14:33
  • \$\begingroup\$ @mazurnification you have a good point; checking the board's schematic, it doesn't provide debouncing for those switches. \$\endgroup\$
    – Renan
    Commented Oct 5, 2012 at 14:40
  • \$\begingroup\$ Debouncing is unlikely to be the main problem. Try this: remove the call to delay_cycles() and make sure that the watchdog is not enabled. Then report what you find. \$\endgroup\$
    – user3624
    Commented Oct 5, 2012 at 15:22

2 Answers 2

12
\$\begingroup\$

There are no pullups on P2.6 and P2.7.

enter image description here

The MSP has internal pullup / pulldowns but you have to set them up yourself.

So as part of your initialization, add this

P2REN |= (1<<6) | (1<<7);  // turn on pullups
P2OUT |= (1<<6) | (1<<7);  // set them to pull up

Then the inputs won't float randomly when the switches aren't pressed.

\$\endgroup\$
2
  • \$\begingroup\$ I can't believe I didn't see that those pins didn't have pullups... that was it! Now it works correctly. Thanks! \$\endgroup\$
    – Renan
    Commented Oct 5, 2012 at 16:11
  • 1
    \$\begingroup\$ super helpful answer -- I always assume that I have to use my own external pullups and rarely check the datasheet to see if there's something already available that precludes the need for external components. Awesome! \$\endgroup\$
    – Dave
    Commented Oct 5, 2012 at 20:35
4
\$\begingroup\$

Your code polls the switch pins but then delays. If the switch is pressed and released during the delay time it won't be seen.

Try removing the delay, or using interrupts to catch edges on the switch pins.

\$\endgroup\$
2
  • \$\begingroup\$ Agreed. Or just hold the button down longer, but offhand I don't know how many seconds a 100000 cycle delay equates to. \$\endgroup\$
    – Dave
    Commented Oct 5, 2012 at 15:25
  • \$\begingroup\$ Try toggling an LED with a 100000 cycle delay. If it's visibly flashing, that would be bad \$\endgroup\$ Commented Oct 5, 2012 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.