//晶振11.0592
//燈光在單片機控制之下完成由亮到暗的逐漸變化,感覺像是在呼吸
//本例在51hei-5型開發(fā)板上實現(xiàn)了一個數碼管和一個LED燈一起實現(xiàn)呼吸效果
//文件下載:http://www.51hei.com/f/fxd.rar
#include
#define uint unsigned int
#define uchar unsigned char
sbit D1=P0^7;
uchar sr;
uchar jf;
uchar code table[]={
0,1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,
19,20,21,22,23,24,25,26,27,
28,29,30,31,32,33,34,35,36,
37,38,39,40,41,42,43,44,45,
46,47,48,49};
void light(uchar num);
void delay(uint z);
void main()
{
//設置計數器
//選取計數方式1
TMOD = 0x11;
//給計數器寫初值
TH0 = 0;
TL0 = 0;
////////////////////
//操作單片機
//開啟CPU中斷
EA = 1;
///////////////////
//開啟定時器T0位中斷
ET0 = 1;
//開啟計數器
TR0 = 1;
//操作二極管
P1=0;
while(1)
{
if(sr<50)
light(sr);
else sr=0;
}
}
//子函數
void light(uchar num)
{
uchar tme;
D1 = 0;
tme = table[num];
delay(tme);
D1 = 1;
delay(49-tme);
}
//中斷函數
void time () interrupt 1
{
//自變量自加
if (jf<2)
{
jf++;
TH0 =0;
TL0 = 0;
}
if (jf==2)
{
//寫初值
jf=0;
TH0 = 254;
TL0 = 254;
sr++;
}
}
void delay(uint z)
{
uint x,y;
for(x=10;x>0;x--)
for(y=z;y>0;y--);
}