4.流水灯电路:P2端口输出低电平时二极管发光。P2DIR=0XF8,对应表示P27-P23均为输出,若这时P2OUT=0XFF,则L1-L5都会亮。
5.流水灯程序:实现流水灯以三种流动方式和四种流动速度的不同组合而进行点亮"流动" #include <msp430x14x.h>
typedef unsigned int uint;
uint i = 0,j = 0,dir = 0;
uint flag = 0,speed = 0; //flag--灯光流动方式,speed--灯光流动速度
/****************主函数****************/
void main(void)
{
/*给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;
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;
_EINT(); //使能全局中断
LPM0; //CPU进入LPM0模式
}
/*******************************************
函数名称: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) //循环5次就改变flag改变流动方向
{
i = 0;
j = 0;
flag++;
if(flag == 4) flag = 0;
switch(speed)
{
case 0:
TACTL &=~ (ID0 + ID1);
TACTL |= ID_3;
break;
case 1:
TACTL &=~ (ID0 + ID1);
TACTL |= ID_2;
break;
case 2:
TACTL &=~ (ID0 + ID1);
TACTL |= ID_1;
break;
case 3:
TACTL &=~ (ID0 + ID1);
TACTL |= ID_0;
break;
default:
break;
}
if(flag != 3) speed++;
if(speed == 4) speed = 0;
}
}
|