LED模块电路图
VCC为正电压直接连接着LED灯,而LED灯的负极连接着引脚,则控制LED灯是否亮的问题可转换为是否给LED灯负极端低电频的问题。单片机中低电平为0V相当于负极,高电平为5V相当于正极(TTL)。
1、点亮一个LED灯
#include<REGX52.H>
void main()
{
P2=0xFE;
while(1)
{
}
}
2、LED闪烁
用串口调试器的软件延时计算器编写一个延时500毫秒的函数,并调用函数使得“亮”与“不亮”间隔500毫秒进行变化。
系统频率应为12.000Hz,8051指令集:STC-Y1.
#include<REGX52.H>
#include<INTRINS.H>
void Delay500ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();//使得时间精确到毫秒级别
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main(){
while(1){
P2=0xFE;
Delay500ms();
P2=0xFF;
Delay500ms();
}
}
3、流水灯
#include <REGX52.H>
#include <INTRINS.H>
void Delay200ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
i = 2;
j = 134;
k = 20;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main(){
while(1){
P2=0xFE;
Delay200ms();
P2=0xFD;
Delay200ms();
P2=0xFB;
Delay200ms();
P2=0xF7;
Delay200ms();
P2=0xEF;
Delay200ms();
P2=0xDF;
Delay200ms();
P2=0xBF;
Delay200ms();
P2=0x7F;
Delay200ms();
}
}