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

📄 led.c

📁 用于控制LED广告灯箱的源代码
💻 C
字号:
#include <reg51.h>
#include <intrins.h>
#include "EEprom.h"
sbit SET = P1^0;
sbit ADD = P1^1;
sbit DEC = P1^2;

//sbit UpEnable = P0^0;
//sbit UpBrake = P0^1;
//sbit DownEnable = P0^4;
//sbit DownBrake = P0^5;

sbit text = P3^4;

sbit UpS = P1^6;
sbit DownS = P1^7;

sbit chooseHigh = P3^7;
sbit chooseLow = P3^6;

sbit ControlSendReceive = P3^5; //主从控制位
/*
#define CHOOSE P3		//数码管位控制寄存器
#define chooseLeft 0x00  //左边数码管选择位数据
#define chooseMiddle 0x40  //中间数码管选择位数据
#define chooseRight 0x80   //右边数码管选择位数据
*/

#define DATA P2				//数码管数据位

#define MOTOR P0			//无刷直流电机控制寄存器

#define UP 0x01				//上电机驱动数据
#define DOWN 0x10			//下电机驱动数据
#define STOP 0x22			//停止驱动数据

#define MoveUPdata 0x55
#define StopData 0x0f
#define MoveDOWNdata 0xf0
#define Receive() {while (RI == 0); RI = 0;}
#define Send() {while (TI == 0); TI = 0;}
	
unsigned char SendData = 0x0f;
unsigned char ReceiveData;

unsigned int photoLong = 0;	//计算整个画面距离变量
unsigned char left;		//左边数码管数据寄存器
unsigned char middle;	//中间数码管数据寄存器
unsigned char right;		//右面数码管数据寄存器

unsigned char sendflag; //发送次数标志

unsigned char setSec;
unsigned char setPage;

unsigned char code ledCode[19] = 
{
	0x3f, 0x06, 0x5B, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 
	0x77, 0x7c, 0x58, 0x5e, 0x79, 0x71, 0x40, 0x73, 0x76
};//0,1,2,3,4,5,6,7,8,9 共阴数码管

unsigned char code moveCode[4] = 
{
	0x00, 0x08, 0x40, 0x01 
};

unsigned char code EEprom[4] =
{
	0x02, 0x04
};

//定义
unsigned int flag = 0;
unsigned int flag1 = 0;  //定义增量
unsigned char temp = 0;
unsigned int count1;
unsigned char control = 1;

unsigned char page = 1;


void DelayMs(unsigned int count1)
{
	while (count1--)
	{
		unsigned char count2 = 0;
		for (; count2<62; count2++);
	}
}

void DisplaySegment(unsigned char temp)
{
	DATA = moveCode[temp&0x03];

	chooseHigh = 0;
	chooseLow = 0;
	DelayMs(1);

	chooseHigh = 0;
	chooseLow = 1;
	DelayMs(1);

	chooseHigh = 1;
	chooseLow = 0;
	DelayMs(1);
}


void DisplayStop(unsigned char one, unsigned char two, unsigned char three)
{
	unsigned char temp;
	chooseHigh = 0;
	chooseLow = 0;
	DATA = ledCode[one];
	for(temp=0; temp<200; temp++);

	chooseHigh = 0;
	chooseLow = 1;
	DATA = ledCode[two];
	for(temp=0; temp<200; temp++);

	chooseHigh = 1;
	chooseLow = 0;
	DATA = ledCode[three];
	for(temp=0; temp<200; temp++);
}

//功能:从机时间设定
//函数:SetReceiveSecondUP()
//调用变量:unsigned char page	 
//返回值:无
//修改日期:2007-1-14
//作者:狼魅
void SetReceiveSecondUP(unsigned char page)
{
	unsigned char count1;
	unsigned char second = 0;
	while (1)
	{
		for (count1=0; count1<255; count1++)
		{
			DisplayStop(page, second/10, second%10);
			if (ReceiveData == MoveUPdata)
			{
				break;
			}
		}
		if (ReceiveData == MoveUPdata)
		{
			break;
		}
		second++;
   	}
}

//功能:从机时间设定
//函数:SetReceiveSecondDOWN()
//调用变量:unsigned char page
//返回值:无
//修改日期:2007-1-14
//作者:狼魅
void SetReceiveSecondDOWN(unsigned char page)
{
	unsigned char count1;
	unsigned char second = 0;
	while (1)
	{
		for (count1=0; count1<255; count1++)
		{
			DisplayStop(page, second/10, second%10);
			if (ReceiveData == MoveDOWNdata)
			{
				break;
			}
		}
		if (ReceiveData == MoveDOWNdata)
		{
			break;
		}
		second++;
   	}
}

//功能:主机时间设定
//函数:SetSendSecond()
//调用变量:unsigned char page
//返回值:无
//修改日期:2007-1-14
//作者:狼魅
void SetSendSecond(unsigned char page)
{	
	unsigned char count1;
	unsigned char second = 0;
	
	while (second <= setSec)
	{ 	
 		for (count1=0; count1<255; count1++)
		{
			DisplayStop(page, second/10, second%10);	
	   	}
		second++;  	
	}
}

/*****************************

设置手动模式

*****************************/
void ManPower(void)
{
	unsigned int count1 = 0;
	unsigned char temp1 = 0;
	unsigned char flag1 = 0;
	while (1)
	{
		if ((ADD==0) || (DEC==0))
		{
			if ((ADD==1) && (DEC==0))
			{
				MOTOR = UP;
			
				DisplaySegment(temp1);
					
				if (flag1 == 60)
				{
					temp1++;
					flag1 = 0;
				}
			} 
	
			if ((ADD==0) && (DEC==1))
			{
				MOTOR = DOWN;
			
				DisplaySegment(temp1);
					
				if (flag1 == 60)
				{
					temp1--;
					flag1 = 0;
				}	
			}
	   	}
		else
		{
			DisplaySegment(2);
			MOTOR = STOP;
		}
	 	
		if (SET == 0)
		{
			break;
		}
		
		flag1++;
	}
	while (!SET)
	{
		DisplaySegment(2);
	}
}


/*****************************

设置自动模式的页数和时间

*****************************/
void Automatism(void)
{
	unsigned char controlPage;
//	unsigned char controlSecHigh, controlSecLow;
	unsigned char controlSec;
	unsigned char count1;

	
//	controlSecHigh = ReadByte(0x2001);
//	controlSecLow = ReadByte(0x2002);
/*	controlPage = 5;
	controlSecHigh = 0x00;
	controlSecLow = 0x08;
//	controlSec = ((unsigned int)controlSecHigh << 8) | controlSecLow;
*/   
	controlPage = ReadByte(0x2000);
	controlSec = ReadByte(0x2001);

	while (1)
	{
		if ((ADD==0) && (DEC==1))
		{
			for (count1=0; count1<50; count1++)
			{
				DisplayStop(17, controlPage/10, controlPage%10);
			}
			if ((ADD==0) && (DEC==1))
			{
				if (controlPage != 2)
				{
					controlPage--;
				}		
			}
		}
		if ((ADD==1) && (DEC==0))
		{
			for (count1=0; count1<50; count1++)
			{
				DisplayStop(17, controlPage/10, controlPage%10);
			}
			if ((ADD==1) && (DEC==0))
			{
				if (controlPage != 25)
				{
					controlPage++;
				}					
			}
		}
		if (SET == 0)
		{
			break;
		}
		
		DisplayStop(17, controlPage/10, controlPage%10);
	}
	//停顿显示 当按键松开时执行下步
	while (!SET)
	{
		DisplayStop(17, controlPage/10, controlPage%10);	
	};
	while (1)
	{
		if ((ADD==0) && (DEC==1))
		{
			for (count1=0; count1<50; count1++)
			{
				DisplayStop(controlSec/100%10, controlSec/10%10, controlSec%10);
			}
			if ((ADD==0) && (DEC==1))
			{
				if (controlSec != 0)
				{
					controlSec--;
				}					
			}
		}
		if ((ADD==1) && (DEC==0))
		{
			for (count1=0; count1<50; count1++)
			{
				DisplayStop(controlSec/100%10, controlSec/10%10, controlSec%10);
			}
			if ((ADD==1) && (DEC==0))
			{
				if (controlSec != 99)
				{
					controlSec++;
				}				
			}
		}
		if (SET == 0)
		{	
			break;
		}

		DisplayStop(controlSec/100%10, controlSec/10%10, controlSec%10);
  	}

	while (!SET)
	{
		DisplayStop(controlSec/100%10, controlSec/10%10, controlSec%10);	
	};
	
	DeleteSector(0x2000);
	WriteByte(0x2000, controlPage);
//			WriteByte(0x2001, (unsigned char)(controlSec >> 8));
//			WriteByte(0x2002, (unsigned char) controlSec);
	WriteByte(0x2001, controlSec);
	for (count1=0; count1<250; count1++)
	{
		DisplayStop(18, 18, 18); 
	}
}



//中断函数
/*
void TIME0display(void) interrupt 1 using 2
{

}
*/


//功能:串口中断发送接受服务子程序
//函数:SendReceiveInt()
//调用变量:无	 
//返回值:无
//修改日期:2007-1-14
//作者:狼魅
void SendReceiveInt() interrupt 4 using 1
{
	if (ControlSendReceive == 0)
	{
		if (RI != 0)
		{
			RI = 0;
			ReceiveData = SBUF;
			if (TI != 0)
			{
				TI = 0;
				SBUF = SendData;
			}
		}
   	}
	else
	{
		if (TI != 0)
		{
			TI = 0;
			SBUF = SendData;
		}
   	}
}


/*******************************************
*	     主函数
*       广告灯箱
*狼魅  	
*开始时间:2006-11-27
*修改时间:2006-12-4
/******************************************/

void main(void)
{
/**********************
串口中断模式设置	
波特率:4800
**********************/
	TMOD = 0x20;
	TH1 = 0xfa;
	TL1 = 0xfa;
	SCON = 0xd8;
	PCON = 0x00;
	IE = 0x90;
	TR1 = 1;

/******************************
功能:设置手动,自动设置。
日期:2006-12-4
作者:狼魅
*******************************/
	
	if (!(ADD | DEC))
	{
		while (1)
		{
			while (1)
			{			  	
				if ((ADD==0) && (DEC==1))
				{
					control = 1;
				}
	
				if ((ADD==1) && (DEC==0))
				{
					control = 2;
				}

				if (SET == 0)
				{
					break;
				}
				DisplayStop(15, 16, control);		
			}				 
			//停顿显示 当按键松开时执行下步
			while (!SET)
			{
				DisplayStop(15, 16, control);
			};
				 
			if (control == 1)
			{
				Automatism();			//设置自动模式
			}
			else
			{
				ManPower();				//设置手动模式
			}

			DisplayStop(15, 16, control);
	   	}
	}



	setPage = ReadByte(0x2000)-1;
	setSec = ReadByte(0x2001);

/******************************
功能:计算扫描图纸的总长度
日期:2006-12-1
作者:狼魅
*******************************/
	while (1)
	{
		if (DownS == 0)
		{   
		 	MOTOR = UP;

			DisplaySegment(temp);
		
			if (flag == 80)
			{
				temp++;
				flag = 0;
			}	
		}
		else
		{
		 	MOTOR = STOP;

			for (; flag1<200; flag1++)
			{
				DisplaySegment(temp);
			}
			flag = 0;
			break;
		}
		flag++;
 	}

	while (1)
	{		
		if (UpS == 0)
		{
			MOTOR = DOWN;

			DisplaySegment(temp);

			if (flag == 80)
			{
				temp--;
				flag = 0;
			}   

			photoLong ++;

			flag++;
		}
		else
		{
			MOTOR = STOP;
			break;
		}
		for (count1=0; count1<40; count1++);  //测试整幅图画偏差
 	}
	

	MOTOR = STOP;

/********************************
间隔时间的设定
********************************/	
	if (ControlSendReceive == 0)
	{
		RI = 1;
		ES = 1;
		SetReceiveSecondUP(page);
	}
	else
	{
		TI = 1;
		SetSendSecond(page);
	}	

	while (1)
	{	
	/*向上步进*/
	 	flag1 = 0;
		
	 	while (1)
		{	   
			if (DownS == 0)
			{  
				MOTOR = UP; 
				ES = 1;
				if (sendflag != 100)
				{	
					SendData = MoveUPdata;	    //制动数据
					sendflag++;
				}
				else
				{
					ES = 1;
					SendData = StopData;
				}

				DisplaySegment(temp);
				
				if (flag == 80)
				{
					temp++;
					flag = 0;
				}
				
				for (count1=0; count1<45; count1++);  //测试向上图画偏差
		
				if (page != setPage)
				{
					if (flag1 == (photoLong)/setPage)
					{
						page++;
						sendflag = 0;
						MOTOR = STOP;

						if (ControlSendReceive == 0)
						{
							RI = 1;
							ES = 1;
							SetReceiveSecondUP(page);
						}
						else
						{
							SetSendSecond(page);
					   	}
	
						flag1 = 0;
					}
				}

				flag++;
				flag1++;
		  	}
			else
			{			
				flag1 = 0;
				break;
			}
		}		
		MOTOR = STOP; 
		page++;	
	    
		
		if (ControlSendReceive == 0)
		{
			RI = 1;
			ES = 1;
			SetReceiveSecondDOWN(page);
		}
		else
		{	  
			TI = 1;
			SetSendSecond(page);
		}

		
		page = setPage;
		sendflag = 0;

	/*向下步进*/
		while (1)
		{	
			
			if (UpS == 0)
			{  
				MOTOR = DOWN;				
				ES = 1;
				if (sendflag != 100)
				{
					SendData = MoveDOWNdata;	    //制动数据
					sendflag++;
				}
				else
				{   
					ES = 1;
					SendData = StopData;	
				}
				
				DisplaySegment(temp);
				
				if (flag == 80)
				{
					temp--;
					flag = 0;
				}
				
				for (count1=0; count1<38; count1++); //测试向下图画偏差
				
				if (page != 1)
				{
					if (flag1 == (photoLong)/setPage)
					{   
						sendflag = 0;
						MOTOR = STOP;
						if (ControlSendReceive == 0)
						{
							RI = 1;
							ES = 1;
							SetReceiveSecondDOWN(page);
						}
						else
						{
							SetSendSecond(page);
						}
						
						page--;
		
						flag1 = 0;
							
					}
			   	}

				flag++;
				flag1++;
		  	}
			else
			{
				break;
			}
		}
	
		MOTOR = STOP;
		page = 1;

		if (ControlSendReceive == 0)
		{
			RI = 1;
			ES = 1;
			SetReceiveSecondUP(page);
		}
		else
		{
			SetSendSecond(page);
		}

		sendflag = 0;
	}
}

⌨️ 快捷键说明

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