Search code examples
cembeddedmsp430

MSP430 issue with button not switching off


I'm making a program for the msp430.

The incrementation runs away on first button click. It doesn't stop when the button is released.

How can incrementation be limited to one incrementation for each button click?

#include <msp430.h>

int main(void)
{
   int i; //delay variable
   int dimeRead=0;
   int desired=1000;
   volatile int total=0;

   P1OUT=0;                  //Supposed to get rid of it hanging at the top
   WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

   while(total<desired)
   {
      if((P1IN&0x16)!=0x16) // check if switch is pressed or not
      {
         dimeRead=dimeRead+1;
         total=total + 10;
      }

      //Goal is to flip an out put on to turn on light when desired number is hit.
   }

   return 0;  
}

Solution

  • At first write your button pins by mask like this #define MASK PIN1 | PIN2 (1 and 2 change to your pins) it's better for visual error control. At second statement for check all pressed buttons if ((P1IN&MASK)==MASK).

    Now your statement if((P1IN&0x16)!=0x16) check that 3 pins (PIN1, PIN2, PIN4) are in Hi state and when it's false make code

    {
             dimeRead=dimeRead+1;
             total=total + 10;
    }
    

    If you want increment when one or two buttons are pushed statement must be like this if((P1IN&MASK)!=0)

    All this is true for buttons that pushed up (HI state) when pressed, for pulled down (LOW state) is if((P1IN&MASK)!=MASK).

    Add some delay after increment for debounse button. If your buttons are connected by PIN and ground, you must enable pull-up for this pins/