無法正確控制led燈!

139 views
Skip to first unread message

Dim Grove

unread,
Jan 4, 2013, 9:35:23 AM1/4/13
to msp430lau...@googlegroups.com
這個程式是我參閱貴網站寫的程式,目的是藉由按P1.3按鈕,使P1.0的LED燈亮,但一直存在
BUG,有請版上先進幫我DEBUG,感謝!

再請問為何P1IN 跟 BIT3 要一起作AND判斷呢?

#include <msp430g2553.h>
void main(void)
{
  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
  P1OUT=BIT3;
  P1DIR=BIT0; // set P1.0 to red led
  P1REN = BIT3;
  while(1)
 {
 if((P1IN & BIT3) ==0)
  
    P1OUT = 0x0001; //SET P1.0 RED LED TURN ON
 else
P1OUT = 0x0000;
 }
}

Anderson

unread,
Jan 6, 2013, 5:12:23 PM1/6/13
to msp430lau...@googlegroups.com
你的寫法只是讓它亮而已,只進行一個步驟。
因為我手邊G2553拿去做研究焊接了,剩下G2452幫你測試!

請試試...(記得將include改為你的晶片型號):
------------------------------------------------------------
#include <msp430g2452.h>

#define RED    BIT0
#define Button BIT3

void main(void) {
    WDTCTL = WDTPW + WDTHOLD;

    P1REN = P1OUT;
    P1DIR = RED;
    P1OUT = Button;

    unsigned int i;
    unsigned int L;
    for (;;) {
        for (i = 0; i < 1; i++) {
            P1OUT ^= RED;
            for (L = 0; L < 45000; L++);
        }
        while ((P1IN & Button) == Button);
    }
}
--------------------------------------------------------------
//*
Dim Grove於 2013年1月4日星期五UTC+8下午10時35分23秒寫道:

Grove

unread,
Jan 7, 2013, 8:46:10 AM1/7/13
to msp430lau...@googlegroups.com
 測試後也不全然正確,因當初我的原始構想只是當按著P1.3時,紅色的LED(P1.0)就要亮起,
手放開後,燈就熄掉。
感謝AndersonChun 幫我解惑,也請先進們再幫我DEBUG,謝謝!

Grove

unread,
Jan 7, 2013, 9:46:39 AM1/7/13
to msp430lau...@googlegroups.com
這是我重新寫的程式,但不全然正確,請版上先進能幫我除錯!

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog
  P1OUT=BIT3; // set P1.3 button
  P1DIR=BIT0; // set P1.0 to red led
  P1REN = BIT3;
  while(1)
 {
int i;
    while((P1IN & BIT3) ==0)
      {
    P1OUT ^= 0x0001;
    for(i=50000;i--;i<0);
      }
 }
}


 
  

AndersonChun

unread,
Jan 10, 2013, 8:40:42 AM1/10/13
to msp430lau...@googlegroups.com
嗯!! 你沒有說清楚,原來是要<按亮放滅>,
我增加了一點變化,一樣按紐按下紅燈亮(黃燈熄)、放開按鈕紅燈錫(黃燈亮),
交替變化 ..另外只需要刪掉一行,就可以黃燈不動作,維持你要的紅燈亮滅。
---------------------------------------------------------
#include "msp430g2452.h"

#define Button BIT3
#define Red_LED BIT0
#define Yellow_LED BIT6

void main(void)
{
WDTCTL = WDTPW + WDTHOLD;

P1DIR = Yellow_LED;
P1DIR |= Red_LED;

while (1)
{

if (P1IN &= Button){
unsigned int i;
unsigned int count;
P1OUT ^= Yellow_LED;
        for (i=0; i<1; i++){
            P1OUT &= Yellow_LED;
            __delay_cycles(5000);
            for (count=0; count<100; count++);
        }
}
else{
P1OUT = Red_LED;
}
}
}
---------------------------------------------------------

Grove於 2013年1月7日星期一UTC+8下午10時46分39秒寫道:

Grove

unread,
Jan 11, 2013, 10:38:20 AM1/11/13
to msp430lau...@googlegroups.com
感謝AndersonChun 費心指導!

 
 
  

Allen Chen

unread,
Feb 18, 2015, 10:21:36 AM2/18/15
to msp430lau...@googlegroups.com
您好,小弟剛接觸MSP430G2,板子是我從網路上買來的,
在run這個程式的時候發現當我按P1.3按鈕時都沒有相對的反應,
請問這是不是表示按鈕壞了?
有沒有什麼辦法可以知道按鈕有沒有壞?感謝

Mr.Kaito於 2013年1月10日星期四 UTC+8下午9時40分42秒寫道:
Reply all
Reply to author
Forward
0 new messages