点灯
进制转换
// An highlighted block
#include <REGX52.H>//头文件
void main()
{
P2=0xFE;//(1111 1110)点亮第一个灯
}
闪烁
// An highlighted block
include <REG52.H>//头文件
#include<INTRINS.H>//因为nop所以加了个头文件INTRINS
void Delay500ms() //@12.000MHz延时函数
{
unsigned char i, j, k;
_nop_();//因为这里有个未定义的nop所以上面又加了个头文件
i = 4;
j = 205;
k = 187;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
P2=0xFE;//(1111 1110)第一个灯亮
Delay500ms;//应用延时函数
P2=0xFF;//(1111 1111)第一个灯灭
Delay500ms;
}
延时函数从软件STC-ISP中得来,选中“软件延时计算器”,系统频率选择12MHz,定时长度选择500ms。
流水灯
数据类型
定时长度选择1ms
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
// An highlighted block
#include <REGX52.H>
void Delay1ms(unsigned int xms) //@12.000MHz
{
unsigned char i, j;
while(xms)//只要while()里的变量非0就会执行,xms是变量
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
xms--;//每执行一遍xms减1,所以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);
}
}
流水灯,要改变延时可改变Delayms()函数内数字。
如果需要前面两个延时长,后面快,可改变前两个延时函数参数为1000,后为100。