【重拾51单片机(1)】点灯,流水灯,延时

【重拾51单片机Day1】点灯,流水灯,延时

点灯

进制转换
在这里插入图片描述

// 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。
在这里插入图片描述

流水灯

数据类型C51数据类型
定时长度选择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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值