LED模块电路示意图
可以看出LED右端接的是高电平VCC,则左端只要接低电平即可点亮LED
1、点亮一个LED
#include <REGX52.H>
void main()
{
while(1)
{
P2 = 0xFE; // 1111 1110
}
}
2、LED闪烁
#include <REGX52.H>
void Delay(unsigned int xms) //@12.000MHz 延迟函数
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
// 实现LED按照1000ms的规律进行闪烁
void main()
{
while(1)
{
P2 = 0xFE;
Delay(1000);
P2 = 0xFF;
Delay(1000);
}
}
3、LED流水灯
#include <REGX52.H>
void Delay(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void method(unsigned int xms)
{
P2 = 0xFE;
Delay(xms);
P2 = 0xFD;
Delay(xms);
P2 = 0xFB;
Delay(xms);
P2 = 0xF7;
Delay(xms);
P2 = 0xEF;
Delay(xms);
P2 = 0xDF;
Delay(xms);
P2 = 0xBF;
Delay(xms);
P2 = 0x7F;
Delay(xms);
}
void main()
{
while(1)
{
method(200);
}
}