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

📄 main1.txt

📁 用电键控制步进电机朝各个方向做曲线
💻 TXT
字号:
#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 c  //c=单步距离

bit PA,PB,PC,PD;
char sta1,sta2;
float linex,liney;
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;
		}
	  }
	}


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

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

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


void circle (int x,int y)//画圆程序
{
	unsigned char i;
	int x1,y1;
	float 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 main()
{
PA1=0; 
PB1=1;
PC1=1;
sta1=0;//电机1初始化
PA2=0; 
PB2=1;
PC2=1;
sta2=0;//电机2初始化

float x,y;
unsigned char key;
unsigned char send[8]={00,00,00,00,00,00,00,00};//定义发送缓冲区 0~3--y 4~6--x 7--sign
display(send);

/*while(1)
 {runner1(200,0);
runner1(200,0);
runner1(200,0);
 runner1(200,1);
runner1(200,1);
runner1(200,1);
 }*/
 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]==10)
					{
						send[6]=00;
					}	break;

					case 8 :	send[7]++;/*标志位*/	
					if(send[7]==4)
					{
						send[7]=00;
					}	break;
				}
				delay(100);
				display(send);
				if (key!=9) continue;//等待按键,输入坐标
				else break;
 }
x=



 
}


⌨️ 快捷键说明

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