2 流水灯轮流闪烁
程序设计:
#include
do(n--); while(n!=0); }
void main()
{WDTCTL=WDTPW+WDTHOLD; P2DIR=0xff; P2OUT=0xfe; while(1)
{ delay(30000); P2OUT<<=1;
P2OUT|=0x01; if(P2OUT==0X7f) { delay(30000); P2OUT=0xfe; } } }
实验结果:
3 数码管0~7显示
程序设计:
#include
#define duan_l P6OUT &= ~BIT6 #define duan_h P6OUT |= BIT6 //数码管7位段码:0--f
uchar scandata[8] = {0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07, 0x7f}; uchar cnt = 0; 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 = WDT_ADLY_1_9; // 设置内部看门狗工作在定时器模式,1.9ms中断一次 IE1 |= WDTIE; // 使能看门狗中断 P6DIR |= BIT2;P6OUT |= BIT2; //关闭电平转换
_BIS_SR(LPM3_bits + GIE); //CPU进入LPM3低功耗模式,同时打开全局中断 }
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
{ P4OUT = scandata[cnt]; //输出段选信号 duan_h; duan_l;
P4OUT = ~(1 << cnt); //输出位选信号 wei_h;
wei_l;
cnt++; //位计数变量在0~5之间循环 if(cnt == 8) cnt = 0; }
实验结果: