51单片机入门教程——LED灯
Posted MilkShake0303
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了51单片机入门教程——LED灯相关的知识,希望对你有一定的参考价值。
CPU通过配置寄存器来控制硬件电路实现功能
实例1 点亮一个LED
#include <REGX52.H>
void main()
{
P2 = 0xFE; //1111 1110
while(1)
{
}
}
实例2 LED闪烁
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2 = 0xFE;
Delay500ms();
P2 = 0xFF;
Delay500ms();
}
}
实例3 LED流水灯
//延时函数通过STC-ISP生成
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2 = 0xFE; //1111 1110
Delay500ms();
P2 = 0xFD; //1111 1101
Delay500ms();
P2 = 0xFB; //1111 1011
Delay500ms();
P2 = 0xF7; //1111 0111
Delay500ms();
P2 = 0xEF; //1110 1111
Delay500ms();
P2 = 0xDF; //1101 1111
Delay500ms();
P2 = 0xBF; //1011 1111
Delay500ms();
P2 = 0x7F; //0111 1111
Delay500ms();
}
}
实例4 LED流水灯Plus
//给定参数调节延时
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@11.0592MHz
{
unsigned char i, j;
while(xms)
{
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
xms --;
}
}
void main()
{
while(1)
{
P2 = 0xFE; //1111 1110
Delay1ms(100);
P2 = 0xFD; //1111 1101
Delay1ms(100);
P2 = 0xFB; //1111 1011
Delay1ms(100);
P2 = 0xF7; //1111 0111
Delay1ms(100);
P2 = 0xEF; //1110 1111
Delay1ms(100);
P2 = 0xDF; //1101 1111
Delay1ms(100);
P2 = 0xBF; //1011 1111
Delay1ms(100);
P2 = 0x7F; //0111 1111
Delay1ms(100);
}
}
以上是关于51单片机入门教程——LED灯的主要内容,如果未能解决你的问题,请参考以下文章