通过P2口实现8个LED发光二极管从左到右、再从右到左循环点亮,即第一个灯点亮-熄灭,第二个灯点亮-熄灭……第八个灯点亮-熄灭,第七个灯点亮-熄灭……第一个灯点亮-熄灭……一直循环运行。(C语言及编程)
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char TAB[]= {0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff}; // 改变这里 LED 各种花样
uchar t,i;
void main()
{
TMOD=0x10;
TH1=(65535-50000)/256; // 50ms一次
TL1=(65535-50000)%256;
EA=1;
ET1=1;//开定时器1中断
TR1=1;//启动定时器1
t=0;
i=0;
while(1)
{
}
}
void timer1() interrupt 3
{
TH1=(65535-50000)/256; // 50ms一次
TL1=(65535-50000)%256;
t++;
if(t==20)
{
t=0;
P2=TAB[i];
i++;
if(i==18) { i=0;}
}
}