😂一个打过一年半的oier,写过一年多的Java,现在致力于学习iot应用的普通本科生 🙏作者水平有限,如发现错误,还请私信或者评论区留言!
使用示例单片机:
stc89c52rc编译软件:keil 烧录软件:stc-isp
原理图如下:


8个LED灯按照由上而下,由下而上的顺序循环点亮,间隔约50ms,无限循环
分析:
延时为50ms的实现:打开stc-isp寻求对应芯片,来实现软件延时

LED如何闪烁?
通过上面的LED原理图不难发现,当P2^0为0时,LED1亮,对应的十六进制为0xFE(1111 1110);
编码实现:
//流水灯实现
#include <REGX52.H>
#include <INTRINS.H>
//使用延时来模拟流水灯
void Delay50ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 3;
j = 26;
k = 223;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2=0xFE;
Delay50ms();
P2=0xFD;
Delay50ms();
P2=0xFB;
Delay50ms();
P2=0xF7;
Delay50ms();
P2=0xEF;
Delay50ms();
P2=0xDF;
Delay50ms();
P2=0xBF;
Delay50ms();
P2=0x7F;
Delay50ms();
}
}P2过于繁琐,可以用一个数组来存储,正逆循环操作来进行循环实现:
#include <REGX52.H>
#include <INTRINS.H>
char a[] = {0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};
//使用延时来模拟流水灯
void Delay50ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 3;
j = 26;
k = 223;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
char i;
while(1)
{
for(i=0;i<8;i++)
{
P2 = a[i];
Delay50ms();
}
for(i=7;i>=0;i--)
{
P2 = a[i];
Delay50ms();
}
}
}8只发光二极管先整体闪烁3次
分析:
编码实现
#include <REGX52.H>
#include <INTRINS.H>
void Delay50ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 3;
j = 26;
k = 223;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
int w;
while(1)
{
for(w = 0; w<3; w++)
{
//P2 全为0,亮
P2 = 0x00;
Delay50ms();
//P2 全为1,灭
P2 = 0xFF;
Delay50ms();
}
break;
}
}之后再把,流水灯的代码加到for循环后面就可以实现了,hh