本帖最后由 gaoyang9992006 于 2012-6-12 18:00 编辑
//默认一起闪烁,按下按键后交替闪烁,再按下又一起,……
#include <msp430g2452.h>
#define BUTTON BIT3
#define LED_RED BIT0
#define LED_GREEN BIT6
#define LED_DIR P1DIR
#define BUTTON_DIR P1DIR
#define BUTTON_OUT P1OUT
#define LED_OUT P1OUT
#define BUTTON_REN P1REN
#define BUTTON_ON (P1IN&BIT3)
#define BUTTON_OFF !(P1IN&BIT3)
#define LED_RED_ON() P1OUT|=BIT0
#define LED_RED_OFF() P1OUT&=~BIT0
#define LED_GREEN_ON() P1OUT|=BIT6
#define LED_GREEN_OFF() P1OUT&=~BIT6
volatile unsigned char i=0,flag=0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
BUTTON_DIR &= ~BUTTON;
LED_DIR |= LED_RED+LED_GREEN;
BUTTON_REN |= BUTTON;
BUTTON_OUT = BUTTON;
while(1)
{
/*通过按键改变选择标志位*/
if(BUTTON_OFF)
{
flag=!flag;
while(BUTTON_OFF);
}
if(flag)
{
LED_RED_ON();
i=10;
while(i--)
_delay_cycles(50000);
LED_RED_OFF();
LED_GREEN_ON();
i=10;
while(i--)
_delay_cycles(50000);
LED_GREEN_OFF();
}
else
{
LED_OUT |= LED_RED+LED_GREEN;
i=10;
while(i--)
_delay_cycles(50000);
LED_OUT ^= (LED_RED+LED_GREEN);
i=10;
while(i--)
_delay_cycles(50000);
}
}
} |