用51单片机制作呼吸灯
扫描二维码
随时随地手机看文章
//晶振11.0592
//灯光在单片机控制之下完成由亮到暗的逐渐变化,感觉像是在呼吸
//本例在51hei-5型开发板上实现了一个数码管和一个led灯一起实现呼吸效果
//文件下载:http://www.51hei.com/f/fxd.rar
#include<reg52.h>
#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--);
}