MSP430流水灯Ⅱ
扫描二维码
随时随地手机看文章
//流水灯三种流动方式和四种流动速度
#include
typedef unsigned int uint;
uint i = 0,j = 0,dir = 0;
uint flag = 0,speed = 0;
void main( void )
{
P1DIR = 0XFF;P1OUT = 0XFF;
P2DIR = 0XFF;P2OUT = 0XFF;
P3DIR = 0XFF;P3OUT = 0XFF;
P4DIR = 0XFF;P4OUT = 0XFF;
P5DIR = 0XFF;P5OUT = 0XFF;
P6DIR = 0XFF;P6OUT = 0XFF;
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P6DIR |= BIT2;
P6OUT |= BIT2;
P5OUT &= ~BIT7;
CCTL0 = CCIE;
CCR0 = 50000;
TACTL = TASSEL_2 + ID_3 + MC_1;
P2DIR = 0XFF;
P2OUT = 0XFF;
_EINT();
LPM0;
}
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A(void)
{
if(flag == 0)
{
P2OUT =~(0x80>>(i++));
}
else if(flag == 1)
{
P2OUT =~(0x01<<(i++));
}
else
{
if(dir)
{
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;
case 1:
TACTL &= ~ (ID0 + ID1);
TACTL |= ID_2;
case 2:
TACTL &= ~ (ID0 + ID1);
TACTL |= ID_1;
case 3:
TACTL &= ~ (ID0 + ID1);
TACTL |= ID_0;
default:
break;
}
if(flag !=3) speed++;
if(speed == 4) speed = 0;
}
}
在中断中设置了,流水方向速度。通过设置TACTL寄存器的值设置流水速度