LED
led:发光二极管,Light Emitting Diode,可用万用表测量正负极
二极管电路图
直插式led,长的一端是正极,短的一端是负极
贴片式led,三角形的尖指向的是负极,有颜色的是负极,底边是正极
开发板的led模块是共阳极的,因此只需要单片机对应I/O口输出低电平就可以点亮
流水灯逐个点亮并且循环
#include <reg52.h>
#include <intrins.h>
void Delayms(int ms) //1毫秒
{
unsigned char i, j;
while(ms)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
ms--;
}
}
void main()
{
int i = 0;
P2=0xfe;
while(1)
{
for(i=0;i<8;i++)
{
P2=0xfe<<i;//1111 1110
Delayms(1000);//1秒
}
P2=0xff;
Delayms(1000);
P2=0xfe;
}
}
单个点亮并且循环,注意_crol_ 函数的使用
#include <reg52.h>
#include <intrins.h>
void Delayms(int ms) //1毫秒
{
unsigned char i, j;
while(ms)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
ms--;
}
}
void main()
{
int i = 0;
P2=0xfe;
Delayms(1000);//1秒
while(1)
{
for(i=0;i<8;i++)
{
P2=_crol_(P2,1);//1111 1110
Delayms(1000);//1秒
}
}
}
呼吸灯的亮度并不是通过电压控制的,而是控制单位时间内亮灯时间占的比例,所以当占比逐渐增大再逐渐减少,就会有一种亮度的视觉差。
用延时控制的呼吸灯
#include <reg52.h>
#include <intrins.h>
sbit LED0=P2^0;
}
void Delayus(int us) //10us
{
unsigned char i;
while(us)
{
i = 2;
while (--i);
us--;
}
}
void main()
{
int i = 0;
while(1)
{
for(i=1;i<300;i++)//由暗变亮
{
LED0=0;
Delayus(i);
LED0=1;
Delayus(300-i);
}
for(i=1;i<300;i++)//由亮变暗
{
LED0=1;
Delayus(i);
LED0=0;
Delayus(300-i);
}
}