51 MCU Example 4 - Using P3 port to realize 8-bit LED lighting and extinguishing in sequence

Use P3 port to realize 8-bit LED lighting and extinguishing in sequence

1. Design purpose

       Use P3 port to realize 8-bit LED lighting and extinguishing in sequence

2. Simulation circuit

 3. Programming (C language)

 

#include<reg51.h>     //包含单片机寄存器的头文件
#include<math.h>      //包含数学函数的头文件
/****************************************
函数功能:延时
*****************************************/
void delay(int time)          //无返回值,无参数
{
	unsigned int i;         //定义无符号整数,最大取值范围65535
	for(time;time>0;time--)
		for(i=0;i<20000;i++);  //做20000次空循环                  
}
/*******************************************************
函数功能:主函数
********************************************************/
void main()
{
	int patter;   //patter=0,依次点亮led;patter=1,依次熄灭led
	int n;
	while(1)       //程序持续运行
	{
		if(P3==0xff)  //判断是否全部熄灭
			patter=0;
		if(P3==0x00)  //判断是否全部点亮
			patter=1;
		if(patter==0)  //从上到下依次点亮
		{
			P3=P3<<1;    //改变P3口电平,依次点亮
			delay(5);   //调用延时函数
		}
		else          //从下到上依次熄灭
		{
			for(n=0;n<8;n++)
			{
				P3=P3+pow(2,7-n);  //改变P3口电平,依次熄灭
				delay(5);   //调用延时函数
			}
		}
	}
}

 

Guess you like

Origin blog.csdn.net/m0_63445811/article/details/125685092