|
今天簡(jiǎn)單地介紹一下發(fā)光二極管,發(fā)光二極管看似簡(jiǎn)單,但是在市場(chǎng)中也有很多應(yīng)用。比如說(shuō),在大家經(jīng)常使用的充電器就有發(fā)光二極管的應(yīng)用,如下圖所示:

這就是簡(jiǎn)易充電器電路,其中LED也就是發(fā)光二極管既起到半波整流的作用又兼做指示燈。同樣的原理,發(fā)光二極管還可以給燈具開關(guān)做指示燈、給工業(yè)設(shè)備配電箱做指示燈以及用共陰極雙色LED對(duì)電源插座指示等等。
接下來(lái),簡(jiǎn)單地說(shuō)一下c語(yǔ)言程序。
大體而言,可以采用位操作與總線操作兩種方式寫程序,比如讓第一個(gè)發(fā)光二極管亮:
(1)、位操作 (2)總線操作
#include<at89x52.h> #include<at89x52.h>
sbit D1=P1^0; void main()
void main() {P1=0xfd;
{ D1=0; }
}
在此基礎(chǔ)上,添加一些循環(huán)、延時(shí)就可以讓其閃爍,如讓第一個(gè)二極管閃爍的三種方法:
法一:#include<at89x52.h>
sbit P1_1=P1^0;
void main()
{unsigned int i;
while(1)
{P1_1=0;
for(i=1;i<10000;i++);
P1_1=1;
for(i=1;i<10000;i++);
}}
法二:#include<at89x52.h>
sbit P1_1=P1^0;
void yanshi();
void main()
{while(1)
{P1_1=0;
yanshi();
P1_1=1;
yanshi();}}
void yanshi()
{unsigned int i;
for(i=0;i<10000;i++);}
法三:#include<at89x52.h>
void main()
{while(1)
{
int i;i=50000;
P1=0xfe;
while(i--);
P1=0xff;
i=50000;
while(i--);
}
}
當(dāng)然,還有更好玩的,比如非常好看的流水燈,其實(shí)原理跟上面一樣:
法一:直接法
#include<at89x52.h>
sbit P1_1=P1^0;
void yanshi();
void main()
{while(1)
{P1=0xfe;
yanshi();
P1=0xfd;
yanshi();
P1=0xfb;
yanshi();
P1=0xf7;
yanshi();
P1=0xef;
yanshi();
P1=0xdf;
yanshi();
P1=0xbf;
yanshi();
P1=0x7f;
yanshi();
}}
void yanshi()
{unsigned int i;
for(i=0;i<10000;i++);}
法二:使用數(shù)組
#include<at89x52.h>
unsigned char table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void yanshi();
void main()
{unsigned int i;
while(1)
{
for(i=0;i<8;i++)
{P1=table[ i]; (注意,在這里的大括號(hào)是一定要加的,否則你就out了)
yanshi();
}}}
void yanshi()
{unsigned int i;
for(i=0;i<10000;i++);}
法三:左移右移
#include<at89x52.h>
unsigned char table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned int i;
void yanshi();
void main()
{P1=0xfe;
while(1)
{P1=P1<<1;
P1=P1|0x01;(因?yàn)槭亲笠贫皇茄h(huán)左移,所以要末位置一,這樣的結(jié)果也會(huì)導(dǎo)致最后只挨個(gè)量一次)
yanshi();}
}
void yanshi()
{unsigned int i;
for(i=0;i<10000;i++);}
其實(shí)想想并不是很難,就是賦值控制亮滅,主要是記住延時(shí)那個(gè)函數(shù),最后調(diào)用就可以了,在這個(gè)基礎(chǔ)上再拓展一下,雙燈左移右移閃爍,也就是雙燈左移7次,右移7次,然后全閃7次,程序如下:
#include<at89x52.h>
void Delay(unsigned int i);
void main()
{unsigned char i;
unsigned char temp;
while(1)
{temp=0xfc;
P1=temp;
for(i=0;i<7;i++)
Delay(50000);
{temp<<=1;
temp=temp|0x01;
P1=temp;}之所以加入temp做中間變量,防止直接操作端口造成的短暫閃爍
Temp=0x3f;
P1=temp;
For(i=0;i<7;i++)
{Delay(50000);
Temp>>=1;
Temp|=0x80;
P1=temp;}
For(i=0;i<3;i++)
P1=0xff;
Delay(50000);
P1=0x00;
Delay(50000);}
} }
void Delay(unsigned int i)
{
while(--i);
}
這么一分析,發(fā)光二極管真的很簡(jiǎn)單,賦值端口,控制亮滅,延時(shí),調(diào)用函數(shù),基本上就可以了,最重要的是c語(yǔ)言簡(jiǎn)單知識(shí)的一些規(guī)范,這都是入門的,不難理解,細(xì)心一些就好了,個(gè)人覺得比較好的還是自己寫程序去調(diào)試,調(diào)試次數(shù)多了,自然而然就熟悉了,孰能生巧,對(duì)很多事情都是亙古不變的真理,學(xué)習(xí)更是如此。 |
|