51单片机利用定时器不使用中断,进行8个灯循环持续一秒

#include<reg51.h>
void delay(int n)
{
	int i,j;

	for(i = 0; i < n; i++)
	{
		for(j = 0; j < 20; j++)		//循环20次,即为1s
		{
			while(TF0 == 0);
			TF0 = 0;
			TH0 = 0x3c;
			TL0 = 0xb0;
		}
	}
}
 
int a[8] = {1, 2, 4, 8, 16, 32, 64, 128};
 
void main() 
{
	int i = 0;
	TMOD = 0x01;	//使用模式1
	TH0 = 0x3c;		//50ms
	TL0 = 0xb0;
	TR0 = 1;
	P2 = 1;
	while(1)		//使P2八个引脚循环出现高电平
	{
		P2 = a[i++];
		delay(1);
		if(i == 8)
		{
			i = 0;
		}
	}
}

欢迎大家批评指正!

猜你喜欢

转载自blog.csdn.net/qq_43710889/article/details/106202440