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

📄 mainago.c

📁 用电键控制步进电机朝各个方向做曲线
💻 C
字号:
#include "reg52.h"
#include "intrins.h"
#include "math.h"
#include <absacc.h>
#include "VIIC_C51.h"
#include "ZLG7290.h"

sbit KEY_INT=P3^2;
sbit PA1=P2^0;
sbit PB1=P2^1;
sbit PC1=P2^2;
sbit PA2=P2^4;
sbit PB2=P2^5;
sbit PC2=P2^6;
  //端口定义

#define stepleng 1  //c=单步距离
#define a0 1160
#define b0 1492  //物体在原点的两悬线长度

bit dir;
char sta1,sta2;
//unsigned int linex,liney;
unsigned char key;
unsigned char send[8]={00,00,00,00,00,00,00,00};//定义发送缓冲区 0~3--y 4~6--x 7--sign
unsigned int x,y,a,b,za,zb;
int numa,numb;
unsigned char disp_buf[8]={0,0,0,0,0,0,0,0};//定义全局变量

unsigned char display(unsigned char  *sd)//显示程序
{ 
    disp_buf[0] = sd[0];			
    disp_buf[1] = sd[1];		
    disp_buf[2] = sd[2];   
    disp_buf[3] = sd[3];
    disp_buf[4] = sd[4];
    disp_buf[5] = sd[5];
    disp_buf[6] = sd[6];	
    disp_buf[7] = sd[7];

    ZLG7290_SendBuf(disp_buf,8);
    return 0;
}

	unsigned char delay(unsigned char  no)
{ 
	unsigned char  i,j;					//延时参数

  	for(; no>0; no--)
  	{ 
		for(i=0; i<10; i++)
     		for(j=0; j<100; j++);
  	}
  	return 0; 
}

void runner1(unsigned char num,bit dir)//输入转动的步数、方向,完成电机1转动(相对3相电机) 
{
	unsigned char i; 
	if(dir)
	{
		for(i=0;i<=num;i++)
		{
		 	sta1++;
		 	sta1=sta1%6;
			delay(2);
			if (sta1==0)PC1=1;
 			if (sta1==1)PB1=0;
			if (sta1==2)PA1=1;
			if (sta1==3)PC1=0;
			if (sta1==4)PB1=1;
			if (sta1==5)PA1=0;
		}
	}
	else
	  {
	   for(i=0;i<=num;i++)
		{
		sta1--;
		if (sta1<0)sta1=5;
		 delay(2);
			if (sta1==0)PB1=1;
 			if (sta1==1)PA1=0;
			if (sta1==2)PC1=1;
			if (sta1==3)PB1=0;
			if (sta1==4)PA1=1;
			if (sta1==5)PC1=0;
		}
	  }
}

void runner2(unsigned char num,bit dir)//输入转动的步数、方向,完成电机1转动(相对3相电机) 
{
	unsigned char i; 
	if(~dir)
	{
		for(i=0;i<=num;i++)
		{
		 	sta1++;
		 	sta1=sta1%6;
			delay(2);
			if (sta1==0)PC2=1;
 			if (sta1==1)PB2=0;
			if (sta1==2)PA2=1;
			if (sta1==3)PC2=0;
			if (sta1==4)PB2=1;
			if (sta1==5)PA2=0;
		}
	}
	else
	  {
	   for(i=0;i<=num;i++)
		{
		sta1--;
		if (sta1<0)sta1=5;
		 delay(2);
			if (sta1==0)PB2=1;
 			if (sta1==1)PA2=0;
			if (sta1==2)PC2=1;
			if (sta1==3)PB2=0;
			if (sta1==4)PA2=1;
			if (sta1==5)PC2=0;
		}
	  }
}


unsigned int point_A (unsigned int x,unsigned int y)//变量转换xy-a
{
	unsigned int lineA;
	unsigned int tmp;
	tmp=(1150-y)*(1150-y)+(x+150)*(x+150);
	tmp=sqrt(tmp);
	lineA=tmp;
	return lineA;
}

unsigned int point_B (unsigned int x,unsigned int y) //变量转换xy-b
{
	unsigned int lineB;
	unsigned int tmp;
	tmp=(1150-y)*(1150-y)+(950-x)*(950-x);
	tmp=sqrt(tmp);
	lineB=tmp;
	return lineB;
}

unsigned int loc_X (int lineA,int lineB)//变量转换ab-x
{
	unsigned int loc;
	loc = (lineA*lineA-lineB*lineB)/220+40;
	return loc;
}


void circle (int x,int y)//画圆程序  
{
	unsigned char i;
	int x1,y1;
	unsigned int tmp1,tmp2;
	
	x1=x;
	y1=y;

	for (i=0;i<360;i+=5)
		{
    	//取绝对坐标
    	tmp1=sin(3.1415926*(1-i/180));
		tmp2=cos(3.1415926*(1-i/180));

		x1=x1+(x1+25)*tmp2;
		y1=y1+25*tmp1;
		linex=point_A(x1,y1);
		liney=point_B(x1,y1);
		

//		electr(linex,liney);
		}
}

void mypath (void)//将物体直线运动 5mm*200
{unsigned int x=0,y=0,i,ta,tb;//ta,tb a,b前一个状态长
 ta=a0;tb=b0;
 for(i=0;i<=200;i++)
 {a=point_A(x,y);
  b=point_B(x,y);
  za=ta-a;//za,zb a,b的增量
  zb=tb-b;
  numa=za/stepleng;
  numb=zb/stepleng;
  runner1(numa,1);//  1/0表示挂线伸缩的方向
  runner2(numb,1);
  x=x+5;
  y=y+5;
  ta=a;
  tb=b;
 }
 
}



void main()
{
	
PA1=0; 
PB1=1;
PC1=1;
sta1=0;//电机1初始化
PA2=0; 
PB2=1;
PC2=1;
sta2=0;//电机2初始化



display(send);

 while (1)
 {
 
 key=ZLG7290_GetKey();

			  	switch(key)
		      {
					case 1 :	send[0]++;	/*ymm位*/	
					if(send[0]==10)
					{
		 			
						send[0]=00;
					}	break; 

					case 2 :	send[1]++;/*ycm位*/ 
						if(send[1]==10)
					{
					
						send[1]=00;
					}	break;

					case 3 :	send[2]++;/*ydm位*/
					if(send[2]==10)
					{
					
						send[2]=00;
					}break;

					case 4 :	send[3]++;/*ym位*/
					if(send[3]==2)
					{
					
						send[3]=00;
					}	break; 

					case 5 :	send[4]++;/*xmm位*/
					if(send[4]==10)
					{
					
						send[4]=00;
					}	break;
	
					case 6 :	send[5]++;/*xcm位*/	
					if(send[5]==10)
					{
						send[5]=00;
					}	break;

					case 7 :	send[6]++;/*xdm位*/	
					if(send[6]==8)
					{
						send[6]=00;
					}	break;

				
				}
				delay(100);
				display(send);
				if (key!=9) continue;//等待按键,输入坐标
				else break;
 }
x=send[6]*100+send[5]*10+send[4];
y=send[3]*1000+send[2]*100+send[1]*10+send[0];//xy坐标,以mm计
a=point_A(x,y);
b=point_B(x,y);
za=a0-a;
zb=b0-b;
numa=za/stepleng;
numb=zb/stepleng;
runner1(numa,1);//  1/0表示挂线伸缩的方向
runner2(numb,1); //完成从原点到预定点,准备开始画曲线;

while (1)
 {
 
 key=ZLG7290_GetKey();

			  	switch(key)
		      		{
					case 16 : send[7]=1;circle (x,y);break;

					case 15 : send[7]=2;mypath ();break;	
					
				}
				delay(100);
				display(send);
				if (key!=9) continue;//等待按键,输入坐标
				else break;
 }


}


⌨️ 快捷键说明

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