51單片機(jī)LED流星雨拖尾燈程序
控制全彩(三色)led也可以用同樣的概念編程。
用的是51的單片機(jī)pwm實(shí)現(xiàn)的拖尾效果
程序的解說(shuō)以及視頻演示請(qǐng)打開(kāi)http://www.51hei.com/bbs/dpj-22524-1.html里面有詳細(xì)的說(shuō)明.
程序源碼:
#include
#define uint unsigned int
#define uchar unsigned char
uchar abit=16; //流到哪位
uchar atimer0_n;
uchar code abit8[9]={1,2,4,8,16,32,64,128,0};
uchar nn;
char a,b,c,d;
main()
{TMOD = 0x11;
TL0 = 0x00;//2.5ms
TH0 = 0xF7;
TF0 = 0;
TR0 = 1;
EA=1;ET0=1;
while(1)
{
a=abit;
if(a>7||a<0)
a=8;
b=abit-1;
if(b>7||b<0)
b=8;
c=abit-2;
if(c>7||c<0)
c=8;
d=abit-3;
if(d>7||d<0)
d=8;
atimer0_n++;
if(atimer0_n==255)
atimer0_n=0;
if(atimer0_n<5)
P1=0xff-abit8[a];
if(atimer0_n<40)
P1=0xff-abit8[b];
if(atimer0_n<150)
P1=0xff-abit8[c];
if(atimer0_n<255)
P1=0xff-abit8[d];
}
}
atimer0 () interrupt 1
{
TL0 = 0x00;//50ms
TH0 = 0x4C;
nn++;
if(nn==2)
{
nn=0;abit--;
if(abit==0)
abit=16;
}
}
評(píng)論