0
votes

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;  
}
3
Are you sure about 0x16....I guess you need 0x08 or something like that. I mean that 0x16 is binary 10110 and you probably want to test a single bit. With 0x08 you can check bit 3 only, for example - LPs
Tell us which MSP430 model you have, and how you have connected the button to it. - CL.
Where is the signal de-bouncing? Is it handled through a RC filter? If not, you will never get this code to work. And you probably meant to use the decimal mask 16 rather than hex 0x16. Don't use either, use a constant #define mask (1<<4) // pin 4. - Lundin
return 0; - Where do you return to?? There is no shell/etc. to return to. - too honest for this site

3 Answers

0
votes
  if((P1IN&0x16)!=0x16)  , 

when buttons arent pushed this statement is true. You should change it with equal sign.

Also im not sure about where 0x16 came from, i think you should also take a second look at it.

0
votes

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/

-1
votes

I am not authorized comment so I'm writing as an answer. If am not mistaken, you are trying to increment in the if, every time you pressed the button. That's why it should be like this.

if((P1IN&0x16) == 0x16)

But I would like to mention the following also:

if((P1IN&0x16) == 0x16)

by writing this statement you are expecting P1.1, P1.2 and P1.4 to be high.