MSP430单片机流水灯程序

转载自http://hi.baidu.com/qiankun_room/blog/item/b0e925c24c79f75db219a856.html
/***************************************************
程序功能:控制8个LED闪烁,
测试说明:观察LED闪烁
***************************************************/
#include <msp430x14x.h>
unsigned int i = 0;
unsigned int flag = 0 ,speed = 0;
/****************主函数****************/
void main(void)
{ 
    WDTCTL = WDTPW + WDTHOLD;       //关闭看门狗
    
    CCTL0 = CCIE;                   //使能CCR0中断
    CCR0 = 2047;                    //设定周期0.5S                
    TACTL = TASSEL_1 + ID_3 + MC_1; //定时器A的时钟源选择ACLK,增计数模式
    P2DIR = 0xff;                   //设置P2口方向为输出
    P2OUT = 0xff;

    _EINT();                        //使能全局中断
    LPM3;                           //CPU进入LPM3模式
}
/*******************************************
函数名称:Timer_A 
功    能:定时器A的中断服务函数
参    数:无
返回值 :无
********************************************/
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void)
{ 
   if(flag==0)
   {
     P2OUT = ~((0X80)>>(i++));
   }
   
   else
   {
     P2OUT = ~((0X01)<<(i++));
   }
   
   if(i==8)
   {
     i=0;
     flag = ~flag;
     speed++;
   }
   switch(speed)
   {
     case 0:TACTL &=~ (ID0 + ID1);TACTL |= ID_3;break;
     case 1:TACTL &=~ (ID0 + ID1);TACTL |= ID_3;break;
     case 2:TACTL &=~ (ID0 + ID1);TACTL |= ID_2;break;
     case 3:TACTL &=~ (ID0 + ID1);TACTL |= ID_2;break;
     case 4:TACTL &=~ (ID0 + ID1);TACTL |= ID_1;break;
     case 5:TACTL &=~ (ID0 + ID1);TACTL |= ID_1;break;
     case 6:TACTL &=~ (ID0 + ID1);TACTL |= ID_0;break;
     case 7:TACTL &=~ (ID0 + ID1);TACTL |= ID_0;break;
     default: break;
    }
   if(speed==8) speed=0;
   
}

你可能感兴趣的:(单片机)