2.2LED闪烁
2.2.1LED闪烁的程序
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@11.0592MHz
{
unsigned char data i, j, k;
_nop_();
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
while(1)
{
P2=0xFE;//LED亮一次
Delay500ms();
P2=0xFF;//LED灭一次
Delay500ms();
}
}
单片机的运行速度很快 ,所以需要引入延迟函数,使得单片机的亮灭之间存在间隔,以达到亮灭的效果
延迟函数需在STC-ISP中软件演示计算器生成,选择11.0592MHZ频率,定时长度500ms,指令集STC-Y1。(头文件:#include <INTRINS.H>)
2.3LED流水灯
2.3.1LED闪烁的程序(基础版)
#include <REGX52.H>
#include <INTRINS.H>
void Delay500ms() //@11.0592MHz
{
unsigned char data 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();
}
}
2.3.2LED闪烁的程序(进阶版)
#include <REGX52.H>
#include <INTRINS.H>
void Delay1ms(unsigned int xms) //@11.0592MHz
{
unsigned char data i, j;
while(xms)
{
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
xms--;
}
}
void main()
{
while(1)
{
P2=0xFE;//1111 1110
Delay1ms(500);
P2=0xFD;//1111 1101
Delay1ms(500);
P2=0xFB;//1111 1011
Delay1ms(500);
P2=0xF7;//1111 0111
Delay1ms(500);
P2=0xEF;//1110 1111
Delay1ms(500);
P2=0xDF;//1101 1111
Delay1ms(500);
P2=0xBF;//1011 1111
Delay1ms(500);
P2=0x7F;//0111 1111
Delay1ms(500);
}
}
相比于第一个程序,第二个程序可以随时改变流水的延时函数的频率,无需再次生成新频率的延时函数,更具有通用性。
补充知识点1:C51数据类型
学习总结:因为这一个星期要复习计算机二级就暂时放下单片机的学习了,下周要写各种作业可能也学不了多少,先更着吧。几天不学习,都不会建工程了。加粗样式