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

📄 laserscanning.c

📁 89S51单片机通过PDIUSBD12的USB接口芯片实现与上位机通讯的固件源码
💻 C
字号:
#include <REGX51.H>
#include "LCD.h"
#include "D12_USB.h"

sbit En573 = P3^4;
extern EVENT_FLAGS EventFlags;	//事件信号
extern unsigned char idata EndPoint1Buffer[4];
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char IntToStrTemp[10];
void IntToStr(unsigned int t, unsigned char *str, unsigned char n) 
{
	unsigned char a[5]; char i, j;                                 	
	a[0]=(t/10000)%10;         //取得整数值到数组         	
	a[1]=(t/1000)%10;                                     	
	a[2]=(t/100)%10;                                      	
	a[3]=(t/10)%10;                                       	
	a[4]=(t/1)%10;                                        	
                                                      
	for(i=0; i<5; i++)         //转成ASCII码              	
		a[i]=a[i]+'0';                                    	
	for(i=0; a[i]=='0' && i<=3; i++);                     	
	for(j=5-n; j<i; j++)       //填充空格                 	
		{ *str=' ';  str++; }                             	
	for(; i<5; i++)                                       	
		{ *str=a[i]; str++; }  //加入有效的数字           	
	*str='\0'; 
} 
//******************************************定时器相关***********************************************************************
unsigned int  Timer0Count,MotorPlusDelay;

void SetSpeed(unsigned int Speed)
{
	MotorPlusDelay = Speed;
}

void InitialTimer0(void)
{
	EventFlags.Bits.Timer = 0;
	SetSpeed(10);		//定时事件10ms
	TMOD = 0x01;
	TH0  = 0xFC;
	TL0  = 0x18;
	TR0=1;
	ET0=1;
	EA=1;
}

void Timer0(void) interrupt 1 using 3
{
	TH0=0xFC;
	TL0=0x18;
	if(++Timer0Count>MotorPlusDelay)
	{
		Timer0Count = 0;
		EventFlags.Bits.Timer = 1;	//1ms定时溢出事件信号
   	}
}

//******************************************电机相关*******************************************************************
unsigned char MotorXIndex,     MotorYIndex;						//相位标志
unsigned char MotorXStatus,    MotorYStatus;					//端口状态
int           MotorXPosion,    MotorYPosion;		            //坐标
unsigned char RunMode = 0x00;									//电机系统运行模式
bit			  Running = 0;										//是否运行标志
unsigned char ScanRange = 0;									//扫描范围
//扫描标志
bit MotorXArrival = 0,MotorYArrival = 0,RunDirection = 0;
unsigned char RectCodeIndex = 0,RoundCodeIndex = 0;RingIndex = 0;

unsigned char code MotorCodeX[8] = {0xFC,0xFD,0xF9,0xFB,0xF3,0xF7,0xF6,0xFE};
unsigned char code MotorCodeY[8] = {0xCF,0xDF,0x9F,0xBF,0x3F,0x7F,0x6F,0xEF};

void StopMotor(void)	//释放端口
{
	Running = 0;
	P1 = 0xFF;
	MotorXArrival  = 0;
	MotorYArrival  = 0;
	RunDirection   = 0;
	RectCodeIndex  = 0;
	RoundCodeIndex = 0;
	RingIndex      = 0;
}

void InitialMotor(void)
{
	MotorXIndex  = MotorYIndex = 0;
	MotorXStatus = MotorCodeX[MotorXIndex];
	MotorYStatus = MotorCodeY[MotorYIndex];
	MotorXPosion = MotorYPosion = 0;
	P1 = MotorXStatus&MotorYStatus;
	Delay1ms(2);
	StopMotor();
}
//Motor X
void MotorXOneStep(bit Direction)
{
	if(Direction)
	{
		MotorXIndex = (MotorXIndex+7)%8;
		MotorXPosion++;
	}
	else
	{	
		MotorXIndex = (MotorXIndex+1)%8;
		MotorXPosion--;
   	}
  	MotorXStatus = MotorCodeX[MotorXIndex];
	P1 = MotorXStatus&MotorYStatus;
}
//Motor Y
void MotorYOneStep(bit Direction)
{
	if(Direction)
	{
		MotorYIndex = (MotorYIndex+7)%8;
		MotorYPosion++;
	}
	else
	{
		MotorYIndex = (MotorYIndex+1)%8;
		MotorYPosion--;
   	}
   	MotorYStatus = MotorCodeY[MotorYIndex];
	P1 = MotorXStatus&MotorYStatus;
}

void BackHome(void)//归位
{
	if(MotorXPosion >= 0)
	{
		while(MotorXPosion !=0)
		{
			MotorXOneStep(0);
			Delay1ms(2);
		}
	}
	else
	{
		while(MotorXPosion !=0)
		{
			MotorXOneStep(1);
			Delay1ms(2);
		}		
	}
	
	if(MotorYPosion >= 0)
	{
		while(MotorYPosion !=0)
		{
			MotorYOneStep(0);
			Delay1ms(2);
		}
	}
	else
	{
		while(MotorYPosion !=0)
		{
			MotorYOneStep(1);
			Delay1ms(2);
		}		
	}
}
//******************************************键盘检测*******************************************************************
unsigned char code KeyCode[] = {126,125,123,119,111,95,63};//0~6
unsigned char KeyStatus(void)		//按键状态
{
	unsigned char i,KeyValue;
	En573 = 0;
	KeyValue = P2&0x7F;
	En573 = 1;
	if(KeyValue == 0x7F)
		return 0xFF;
	for(i = 0; i<7; i++)
	{
		if(KeyValue == KeyCode[i])
			return i;
	}
	return 0xFF;
}

unsigned char GetKey(void)		//Press Key,(KeyDown,KeyUp)
{
	unsigned char KeyOld,KeyNew;
	KeyOld = KeyStatus();
	if(KeyOld == 0xFF)
		return 0xFF;
   	Delay1ms(3);
	KeyNew = KeyStatus();
	if(KeyNew == 0xFF)
		return KeyOld;
	else
		return 0xFF;
}
//*****************************************CPU相关********************************************************************
void InitialCPU(void)
{
	En573 = 1;
	P1 = 0xFF;
}
//*************************************************************************************************************
void UpdataScreen(unsigned char Mode)
{
	switch(Mode)
	{
	   	case 0x00:
			GotoXY(0,0);
			Print("[Square]   R=");
			if(!Running)
			{
				GotoXY(0,1);
				Print(" OFF       S=");
			}
			else
			{
				GotoXY(0,1);
				Print("  ON       S=");
			}
			IntToStr(ScanRange,&IntToStrTemp[0],3);
			GotoXY(13,0);
			Print(&IntToStrTemp[0]);
			IntToStr(MotorPlusDelay,&IntToStrTemp[0],3);
			GotoXY(13,1);
			Print(&IntToStrTemp[0]);
			break;
		case 0x01:
			GotoXY(0,0);
			Print("[Round]    R=");
			if(!Running)
			{
				GotoXY(0,1);
				Print(" OFF       S=");
			}
			else
			{
				GotoXY(0,1);
				Print("  ON       S=");
			}
			IntToStr(ScanRange,&IntToStrTemp[0],3);
			GotoXY(13,0);
			Print(&IntToStrTemp[0]);
			IntToStr(MotorPlusDelay,&IntToStrTemp[0],3);
			GotoXY(13,1);
			Print(&IntToStrTemp[0]);
			break;
		case 0x02:
			GotoXY(0,0);
			Print("[Emendation]    ");
			GotoXY(0,1);
			Print("    Press Key...");
			break;
		default: break;
	}
}

//Rect 
char code RectX[] = { 0, 0, 1, 1, 1, 0,-1,-1,-1};
char code RectY[] = { 0, 1, 1, 0,-1,-1,-1, 0, 1};
//Round
char code CodeXSin[128] = {0x00,0x06,0x0C,0x13,0x19,0x1F,0x25,0x2B,0x31,0x37,
					  	   0x3C,0x42,0x47,0x4C,0x51,0x56,0x5A,0x5E,0x63,0x66,
					  	   0x6A,0x6D,0x70,0x73,0x76,0x78,0x7A,0x7C,0x7D,0x7E,
					  	   0x7F,0x7F,0x7F,0x7F,0x7F,0x7E,0x7D,0x7C,0x7A,0x78,
					  	   0x76,0x73,0x70,0x6D,0x6A,0x66,0x63,0x5E,0x5A,0x56,
					  	   0x51,0x4C,0x47,0x42,0x3C,0x37,0x31,0x2B,0x25,0x1F,
					  	   0x19,0x13,0x0C,0x06,0x00,0xFB,0xF5,0xEE,0xE8,0xE2,
					  	   0xDC,0xD6,0xD0,0xCA,0xC5,0xBF,0xBA,0xB5,0xB0,0xAB,
					  	   0xA7,0xA3,0x9E,0x9B,0x97,0x94,0x91,0x8E,0x8B,0x89,
					  	   0x87,0x85,0x84,0x83,0x82,0x82,0x81,0x82,0x82,0x83,
					  	   0x84,0x85,0x87,0x89,0x8B,0x8E,0x91,0x94,0x97,0x9B,
					  	   0x9E,0xA3,0xA7,0xAB,0xB0,0xB5,0xBA,0xBF,0xC5,0xCA,
					  	   0xD0,0xD6,0xDC,0xE2,0xE8,0xEE,0xF5,0xFB };
							
char code CodeYCos[128] = {0x7F,0x7F,0x7F,0x7E,0x7D,0x7C,0x7A,0x78,0x76,0x73,
					  	   0x70,0x6D,0x6A,0x66,0x63,0x5E,0x5A,0x56,0x51,0x4C,
					  	   0x47,0x42,0x3C,0x37,0x31,0x2B,0x25,0x1F,0x19,0x13,
					  	   0x0C,0x06,0x00,0xFB,0xF5,0xEE,0xE8,0xE2,0xDC,0xD6,
					  	   0xD0,0xCA,0xC5,0xBF,0xBA,0xB5,0xB0,0xAB,0xA7,0xA3,
					  	   0x9E,0x9B,0x97,0x94,0x91,0x8E,0x8B,0x89,0x87,0x85,
					  	   0x84,0x83,0x82,0x82,0x81,0x82,0x82,0x83,0x84,0x85,
					  	   0x87,0x89,0x8B,0x8E,0x91,0x94,0x97,0x9B,0x9E,0xA3,
					  	   0xA7,0xAB,0xB0,0xB5,0xBA,0xBF,0xC5,0xCA,0xD0,0xD6,
					  	   0xDC,0xE2,0xE8,0xEE,0xF5,0xFB,0x00,0x06,0x0C,0x13,
					  	   0x19,0x1F,0x25,0x2B,0x31,0x37,0x3C,0x42,0x47,0x4C,
					  	   0x51,0x56,0x5A,0x5E,0x63,0x66,0x6A,0x6D,0x70,0x73,
					  	   0x76,0x78,0x7A,0x7C,0x7D,0x7E,0x7F,0x7F };	


void SetScanRange(unsigned char Range)
{
	ScanRange = Range;
}

void TimerEvent(void)
{
	int Value;
	int RoundValueX,RoundValueY,RoundTemp;
 	
	if(RunMode == 0x00 && Running)	//矩形
	{
		if(RectCodeIndex==0)
		{
			if(ScanRange!=0)
				Value = MotorXPosion;		
		   	else
			{
				MotorXOneStep(1);
				Value = MotorXPosion;
			}
	   	}
		else
			Value = RectX[RectCodeIndex]*(RingIndex+1);		

		if(Value == MotorXPosion)
		{
			MotorXArrival = 1;
		}
		else if(Value > MotorXPosion)
		{
			MotorXOneStep(1);	
		}
		else if(Value < MotorXPosion)
		{
			MotorXOneStep(0);	
		}
			
		if(RectCodeIndex==0)
		{
			if(ScanRange!=0)
			{
				MotorYOneStep(1);
				Value = MotorYPosion;
			}
			else
				Value = MotorYPosion;	
		}
		else
		{
			Value = RectY[RectCodeIndex]*(RingIndex+1);
		}

		if(Value == MotorYPosion)
		{
			MotorYArrival = 1;
		}
		else if(Value > MotorYPosion)
		{
			MotorYOneStep(1);	
		}
		else if(Value < MotorYPosion)
		{
			MotorYOneStep(0);	
		}
		
		if(MotorXArrival&MotorYArrival)
		{
			MotorXArrival = 0;
			MotorYArrival = 0;
			RectCodeIndex = (RectCodeIndex+1)%9;
			if(RectCodeIndex==0)
				RingIndex = (RingIndex+1)%(ScanRange+1);	
		}
	}
	else if(RunMode == 0x01 && Running)	//圆形
	{
		RoundTemp = CodeXSin[RoundCodeIndex]*(RingIndex+1);
		RoundValueX = RoundTemp/128;
		if(RoundTemp%128 > 63)	RoundValueX++;

		RoundTemp = CodeYCos[RoundCodeIndex]*(RingIndex+1);
		RoundValueY = RoundTemp/128;
		if(RoundTemp%128 < -64) RoundValueY--;

		if(RoundValueX == MotorXPosion)
		{
			MotorXArrival = 1;
		}
		else if(RoundValueX > MotorXPosion)
		{
			MotorXOneStep(1);	
		}
		else if(RoundValueX < MotorXPosion)
		{
			MotorXOneStep(0);	
		}

		if(RoundValueY == MotorYPosion)
		{
			MotorYArrival = 1;
		}
		else if(RoundValueY > MotorYPosion)
		{
			MotorYOneStep(1);	
		}
		else if(RoundValueY < MotorYPosion)
		{
			MotorYOneStep(0);	
		}

		if(MotorXArrival&MotorYArrival)
		{
			MotorXArrival = 0;
			MotorYArrival = 0;
			RoundCodeIndex = (RoundCodeIndex+1)%128;
			if(RoundCodeIndex==0)
			{
				if(RunDirection)
				{
					RingIndex = (RingIndex+ScanRange)%(ScanRange+1);		
					if(RingIndex==0) 
					{
						RunDirection = 0;						
						RingIndex = 1;
				  	}
			   	}
				else
				{
					RingIndex = (RingIndex+1)%(ScanRange+1);	
					if(RingIndex==ScanRange)	RunDirection = 1;
				}
			}
		}
	}
}
//*************************************************************************************************************
main()
{
	unsigned char Key = 0xFF;
	InitialCPU();
	InitialMotor();
	LCD_Initial();
	InitialTimer0();

	SetSpeed(10);
	SetScanRange(10);
	UpdataScreen(RunMode);
	ReconnectUSB();

	while(1)
	{
		Key = GetKey();
		if(EventFlags.Bits.SetupPacket)
		{
			EA = 0;
			EventFlags.Bits.SetupPacket = 0;
			ControlHandler();
			EA = 1;
		}
		if(EventFlags.Bits.Timer)
		{
			TimerEvent();
			EventFlags.Bits.Timer = 0;
	   	}
		if(EventFlags.Bits.Port1RxDone)
		{
			switch(EndPoint1Buffer[0])
			{
				case 0x00:	//Mode
					if(EndPoint1Buffer[1]>=0 && EndPoint1Buffer[1]<=2)
					{
						BackHome();
						Delay1ms(10);
						StopMotor();
						RunMode = EndPoint1Buffer[1];
						UpdataScreen(RunMode);
					}
					break;
				case 0x01:	//Range
					if(EndPoint1Buffer[1]>=0 && EndPoint1Buffer[1]<=20)
					{
						ScanRange = EndPoint1Buffer[1];
						UpdataScreen(RunMode);
					}
					break;
				case 0x02:	//Speed
					if(EndPoint1Buffer[1]>=0 && EndPoint1Buffer[1]<=200)
					{
						MotorPlusDelay = EndPoint1Buffer[1];
						UpdataScreen(RunMode);
					}
					break;
				case 0x03:	//校正
					if(EndPoint1Buffer[1]==0x01)
						MotorYOneStep(0);
					if(EndPoint1Buffer[1]==0x02)
						MotorXOneStep(0);
					if(EndPoint1Buffer[1]==0x03)
						MotorYOneStep(1);
					if(EndPoint1Buffer[1]==0x04)
						MotorXOneStep(1);
					MotorXPosion = MotorYPosion = 0;
					break;
			   	case 0x04: //Run
					if(EndPoint1Buffer[1]==0x01)
					{
						Running = 0;
						BackHome();
						Delay1ms(10);
						StopMotor();
					}
					if(EndPoint1Buffer[1]==0x02)
						Running = 1;
					UpdataScreen(RunMode);
					break;
			}
			EndPoint1Buffer[0]=EndPoint1Buffer[1]=EndPoint1Buffer[2]=EndPoint1Buffer[3]=0;
			EventFlags.Bits.Port1RxDone = 0;
		}
		//Key Scan
		if(Key!=0xFF)
		{
			if(RunMode==0x02)
			{
				if(Key==0x00)
					MotorYOneStep(0);	
				if(Key==0x01)
					MotorXOneStep(0);
				if(Key==0x02)
					MotorYOneStep(1);
				if(Key==0x03)
					MotorXOneStep(1);
				MotorXPosion = MotorYPosion = 0;
			}

			if(Key == 4)
			{
				Running = 0;
				BackHome();
				Delay1ms(10);
				StopMotor();
				RunMode = (RunMode+1)%3;
				UpdataScreen(RunMode);
			}
			if(Key == 5)
			{
				if(RunMode==0x00 || RunMode==0x01)
				{
					if(RunMode==0x00)
						RingIndex = 0;
					else if(RunMode==0x01)
						RingIndex = 1;
					Running = 1;
					UpdataScreen(RunMode);
				}
			}
			if(Key == 6)
			{
				if(RunMode==0x00 || RunMode==0x01)
				{
					Running = 0;
					BackHome();
					Delay1ms(10);
					StopMotor();
					UpdataScreen(RunMode);
				}
			}
			if(Key == 0x00)
			{
				if(RunMode==0x00 || RunMode==0x01)
				{
					if(ScanRange<20)
					{
						ScanRange++;
						UpdataScreen(RunMode);
				   	}
				}				
			}
			if(Key == 0x02)
			{
				if(RunMode==0x00 || RunMode==0x01)
				{
					if(ScanRange>0)
					{
						ScanRange--;
						UpdataScreen(RunMode);
				   	}
				}
			}
			if(Key == 0x01)
			{
				if(RunMode==0x00 || RunMode==0x01)
				{
					if(MotorPlusDelay < 200)
					{
						MotorPlusDelay++;
						UpdataScreen(RunMode);
				   	}
				}			
			}
			if(Key == 0x03)
			{
				if(RunMode==0x00 || RunMode==0x01)
				{
					if(MotorPlusDelay > 0)
					{
						MotorPlusDelay--;
						UpdataScreen(RunMode);
				   	}
				}			
			}
		}
	}
}

⌨️ 快捷键说明

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