MSP430 ...
/***************************************************
程式功能:控制8個LED閃爍,控制燈的流向以及流速
---------------------------------------------------
***************************************************/
#include<msp430x14x.h>
/*下麵關閉所有的IO口*/
#define close_IO P1DIR = 0XFF;P1OUT = 0XFF; P2DIR = 0XFF;P2OUT = 0XFF;P3DIR = 0XFF;P3OUT = 0XFF;P4DIR = 0XFF;P4OUT = 0XFF; P5DIR = 0XFF;P5OUT = 0XFF; P6DIR = 0XFF;P6OUT = 0XFF;
typedef unsigned int uint;
uint i = 0,j = 0,dir = 0;
uint flag = 0,speed = 0; //flag--燈光流動方式,speed--燈光流動速度
/****************主函數****************/
void main(void)
{
void P_setting();
close_IO;
P_setting();
_EINT(); //使能全局中斷
LPM0; //CPU進入LPM0模式
}
/*將設置進行封裝*/
void P_setting(void){
WDTCTL = WDTPW + WDTHOLD; //關閉看門狗
P6DIR |= BIT2;P6OUT |= BIT2; //關閉電平轉換
P5OUT &= ~BIT7;
CCTL0 = CCIE; //使能CCR0中斷
CCR0 = 50000;
TACTL = TASSEL_2 + ID_3 + MC_1; //定時器A的時鐘源選擇SMCLK,增計數模式
P2DIR = 0xff; //設置P2口方向為輸出
P2OUT = 0xff;
}
/*******************************************
函數名稱:Timer_A
功 能:定時器A的中斷服務函數,在這裡通過標誌
控制流水燈的流動方向和流動速度
********************************************/
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
if(flag == 0)
{
P2OUT = ~(0x80>>(i++)); //燈的點亮順序D8 -> D1
}
else if(flag == 1)
{
P2OUT = ~(0x01<<(i++)); //燈的點亮順序D1 -> D8
}
else
{
if(dir) //燈的點亮順序 D8 -> D1,D1 -> D8,迴圈繞圈
{
P2OUT = ~(0x80>>(i++));
}
else
{
P2OUT = ~(0x01<<(i++));
}
}
if(i == 8)
{
i = 0;
dir = ~dir;
}
j++;
if(j == 40)
{
i = 0;
j = 0;
flag++;
if(flag == 4) flag = 0;
switch(speed)
{
case 0:
TACTL &=~ (ID0 + ID1); //增計數模式
TACTL |= ID_3; //8分頻
break;
case 1:
TACTL &=~ (ID0 + ID1);
TACTL |= ID_2; //4分頻
break;
case 2:
TACTL &=~ (ID0 + ID1);
TACTL |= ID_1; //2分頻
break;
case 3:
TACTL &=~ (ID0 + ID1);
TACTL |= ID_0; //1分頻
break;
default:
break;
}
if(flag != 3) speed++;
if(speed == 4) speed = 0;
}
}
《經過測試適用於f149系列》