简体   繁体   English

MSP430问题,按钮未关闭

[英]MSP430 issue with button not switching off

I'm making a program for the msp430. 我正在为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;  
}
  if((P1IN&0x16)!=0x16)  , 

when buttons arent pushed this statement is true. 当按钮arent按下时,此语句为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. 我也不确定0x16的来源,我想您也应该再看一遍。

At first write your button pins by mask like this #define MASK PIN1 | PIN2 首先,像这样#define MASK PIN1 | PIN2通过面罩写下您的按钮图钉#define MASK PIN1 | PIN2 #define MASK PIN1 | PIN2 (1 and 2 change to your pins) it's better for visual error control. #define MASK PIN1 | PIN2 (将1和2更改为您的引脚),最好进行视觉错误控制。 At second statement for check all pressed buttons if ((P1IN&MASK)==MASK) . 在第二条语句中检查所有按下的按钮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 现在您的语句if((P1IN&0x16)!=0x16)检查3个引脚(PIN1,PIN2,PIN4)是否处于“高”状态,以及假密码是否为假

{
         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) 如果要在按下一个或两个按钮时增加if((P1IN&MASK)!=0)声明必须类似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) . 所有这些对于按下时按下(HI状态)的按钮都是正确的,对于下拉(LOW状态)的if((P1IN&MASK)!=MASK)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/ 如果您的按钮是通过PIN接地的,则必须为此引脚启用上拉/

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. 如果没记错的话,您每次按下按钮时都会尝试增加if。 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. 通过编写此语句,您期望P1.1,P1.2和P1.4很高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM