⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 runled.c

📁 这是51的C程序
💻 C
字号:
//************************************************************/
//************************************************************/
//			action:	流水灯(模拟汽车防盗器防盗过程)
//			MCU:	AT89S51
//			OSC:	6MHZ
//			TIME:	2008,4,9
//************************************************************/
//************************************************************/
//	   preprocess command, compiling the  *.inc file
//************************************************************/
#include  	<C:\Keil\C51\INC\Atmel\REGX51.H>
#include	<stdio.h>
#include	<intrins.h>
typedef		unsigned int	UNINT;
typedef		unsigned char	UNCHAR;
typedef		bit				BOOL;  	

/********* constant	define ************************************/
void	Main_init(void);					//initialize all the set and flag and status
void 	into_runled(void);
void 	out_runled(void);
void 	INDLms(UNINT count);

/********** variable define ***********************************/
UNINT 		a,b,c,d;
UNINT		a_temp,b_temp,c_temp,d_temp;
BOOL		flag;							//flag=0, is show no signal and end to run led.

/********** I/O define ***********************************/
sbit	SING_IN		= P2^7;
sbit	MOTO_OUT	= P2^6;

/*********** function define***********************************/
void INDLms(UNINT count)
{
	UNINT k_temp,k,j,i;
 	for(k=0;k<count;k++)
	{
	  	if(SING_IN==0)					//SING_IN=0, out runled and end to run.
		{
			for(i=0;i<500;i++);
			if(SING_IN==0)
			{
			flag=0;						//flag=0, is show no signal and end to run led.
			goto  out_INDLms;
			}
			else
			{	k=k_temp;	}
		}
	  	else
		{	for(j=0;j<120;j++);
			k_temp=k;
		}
	 }
	out_INDLms:
		_nop_();
}

//==============================================================
void norDLms(UNINT count)
{
	UNINT k_temp,k,j,i;
	for(k=0;k<count;k++)
	{
	  	if(SING_IN==1)					//SING_IN=1, have signal and continue to run.
		{
  			for(i=0;i<500;i++);
			if(SING_IN==1)
			{
			flag=1;						//flag=0, is show no signal and end to run led.
			goto  out_norDLms;
			}
			else
			{	k=k_temp;	}
		}
	  	else
			for(j=0;j<120;j++);
	 }
	out_norDLms:
		_nop_();
}

//==============================================================
/*********** function define***********************************/
UNCHAR    designP[18]={0x00,0x00,0x01,0x02,0x04,0x09,0x12,0x24,0x49,0x92,0x24,0x49,0x92,0x24,0x49,0x92,0x24,0x49};
UNCHAR  keeploopP[18]={0x49,0x92,0x24,0x49,0x92,0x24,0x49,0x92,0x24,0x49,0x92,0x24,0x49,0x92,0x24,0x49,0x92,0x24};

//==============================================================
//				 Main loop
//==============================================================
void main(void)
{
	Main_init();
	while(1)
	{
		MOTO_OUT=0;
		_nop_();
		if(SING_IN==0)
		{	flag = 0;	}
		else
		{	into_runled();
			out_runled();	
		}
	}
}//main

//==============================================================
//				 Main initialize  Subpro
//==============================================================
void 	Main_init(void)
{
//	AUXR = 0x08;		//复位引脚仅为输入
	EA	=	0;			//关闭所有中断
	P0 = 0x00;
	P1 = 0x00;
	P2 = 0x00;
	P3 = 0x00;
	a_temp=b_temp=c_temp=d_temp=0;
}

//=============================================================//
//			 			Main Program
//				P1\P0\P3\P2(P2.0~P2.5) is LED output port
//=============================================================//
void into_runled(void)
{
	flag = 1;
	if(P0 = 0x00)
	{
		for(a=a_temp;a<10;a++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				a_temp=a;
			}
		}
		for(a=7,b=2;b<10;a++,b++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				a_temp=a;
				b_temp=b;
			}
		}
		for(a=9,b=7,c=2;c<10;a++,b++,c++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				P3=designP[c];
				a_temp=a;
				b_temp=b;
				c_temp=c;
			}
		}
		for(a=8,b=9,c=7,d=2;d<8;a++,b++,c++,d++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				P3=designP[c];
				P2=designP[d];
				a_temp=a;
				b_temp=b;
				c_temp=c;
				d_temp=d;
			}
		}
	}

	else if(P3 = 0x00)
	{
		for(a=a_temp,b=b_temp;b<10;a++,b++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				a_temp=a;
				b_temp=b;
			}
		}
		for(a=9,b=7,c=2;c<10;a++,b++,c++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				P3=designP[c];
				a_temp=a;
				b_temp=b;
				c_temp=c;
			}
		}
		for(a=8,b=9,c=7,d=2;d<8;a++,b++,c++,d++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				P3=designP[c];
				P2=designP[d];
				a_temp=a;
				b_temp=b;
				c_temp=c;
				d_temp=d;
			}
		}
	}

//	else if(P2^5==0 || P2^4==0 || P2^3==0 || P2^2==0 || P2^1==0 || P2^0==0)
	else if(P2=0x80)
	{
		for(a=a_temp,b=b_temp,c=c_temp;c<10;a++,b++,c++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				P3=designP[c];
				a_temp=a;
				b_temp=b;
				c_temp=c;
			}
		}
		for(a=8,b=9,c=7,d=2;d<8;a++,b++,c++,d++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				P3=designP[c];
				P2=designP[d];
				a_temp=a;
				b_temp=b;
				c_temp=c;
				d_temp=d;
			}
		}	
	}

	else
	{
		for(a=a_temp,b=b_temp,c=c_temp,d=d_temp;d<8;a++,b++,c++,d++)
		{	
			INDLms(10);
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				P3=designP[c];
				P2=designP[d];
				a_temp=a;
				b_temp=b;
				c_temp=c;
				d_temp=d;
			}
		}	
	}

	while(1)
	{	
		for(a=8,b=9,c=7,d=5;d<8;a++,b++,c++,d++)	
		{	
			MOTO_OUT=1;
			INDLms(180);	
			if(flag==0)
			{	goto will_out_runled;}
			else
			{
				P1=designP[a];
				P0=designP[b];
				P3=designP[c];
				P2=designP[d];
				a_temp=a;
				b_temp=b;
				c_temp=c;
				d_temp=d;
			}
		}
	}
	will_out_runled:
	_nop_();
}

//==============================================================
void out_runled(void)
{	
	flag=0;
	if(b==0)
	{
		for(a=a_temp-1;a>0;a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=designP[a];
		  		MOTO_OUT=!MOTO_OUT;
			}
		}
		goto end_runled;
	}
	
	else if(c==0)
	{
		for(b=b_temp-1,a=a_temp;b>0;b--,a--)
		{
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=keeploopP[a];
				P0=designP[b];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		for(a=8;a>0;a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=designP[a];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		goto end_runled;
	}

	else if(d==0)
	{
		for(c=c_temp-1,b=b_temp,a=a_temp;c>0;c--,b--,a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=keeploopP[a];
				P0=keeploopP[b];
				P3=designP[c];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		for(b=8,a=8;b>0;b--,a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=keeploopP[a];
				P0=designP[b];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		for(a=8;a>0;a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=designP[a];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		goto end_runled;
	}

	else
	{
		for(d=d_temp-1,c=c_temp,b=b_temp,a=a_temp;d>0;d--,c--,b--,a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=keeploopP[a];
				P0=keeploopP[b];
				P3=keeploopP[c];
				P2=designP[d];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		for(c=8,b=8,a=7;c>0;c--,b--,a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=keeploopP[a];
				P0=keeploopP[b];
				P3=designP[c];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		for(b=8,a=8;b>0;b--,a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=keeploopP[a];
				P0=designP[b];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		for(a=8;a>0;a--)
		{	
			norDLms(120);
			if(flag==1)
			{	goto end_runled;}
			else
			{
				P1=designP[a];
				MOTO_OUT=!MOTO_OUT;
			}
		}
		goto end_runled;
	}
	end_runled:
	_nop_();
}
//=============================================================//
//			 			Main END
//				P1\P0\P3\P2(P2.0~P2.5) is LED output port
//=============================================================//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -