Proteus教程:步进电机

描述

视频教程:https://www.bilibili.com/video/BV1fF411K79x

 步进电机步进电机步进电机步进电机步进电机步进电机
#include< reg51.h >
#include < math.h >
#define uchar unsigned char
#define uint unsigned int
#define LCD1602_DATAPINS P0
sbit LCD1602_E=P2^2;
sbit LCD1602_RW=P2^1;
sbit LCD1602_RS=P2^0;

sbit MA_1 = P1^0;
sbit MA_2 = P1^1;
sbit MA_3 = P1^2;
sbit MA_4 = P1^3;

sbit MB_1 = P1^4;
sbit MB_2 = P1^5;
sbit MB_3 = P1^6;
sbit MB_4 = P1^7;

uchar flag;
unsigned char code M_A[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //X反转顺序
unsigned char code M_B[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正转顺序


/*******************************************************************************
* 函 数 名  : Delay
* 函数功能  : 延时
*******************************************************************************/

void DDelay(unsigned int t)
{
 unsigned int k;
 while(t--)
 {
		for(k=0; k< 80; k++);
 }
}


/*******************************************************************************
* 函 数 名         : Lcd1602_Delay1ms
* 函数功能		   : 延时函数,延时1ms
* 输    入         : c
* 输    出         : 无
* 说    名         : 该函数是在12MHZ晶振下,12分频单片机的延时。
*******************************************************************************/

void Lcd1602_Delay1ms(uint c)   //误差 0us
{
    uchar a,b;
	for (; c >0; c--)
	{
		 for (b=199;b >0;b--)
		 {
		  	for(a=1;a >0;a--);
		 }      
	}
    	
}

/*******************************************************************************
* 函 数 名         : LcdWriteCom
* 函数功能		   : 向LCD写入一个字节的命令
* 输    入         : com
* 输    出         : 无
*******************************************************************************/
void LcdWriteCom(uchar com)	  //写入命令
{
	LCD1602_E = 0;     //使能
	LCD1602_RS = 0;	   //选择发送命令
	LCD1602_RW = 0;	   //选择写入
	
	LCD1602_DATAPINS = com;     //放入命令
	Lcd1602_Delay1ms(1);		//等待数据稳定

	LCD1602_E = 1;	          //写入时序
	Lcd1602_Delay1ms(5);	  //保持时间
	LCD1602_E = 0;
}
/*******************************************************************************
* 函 数 名         : LcdWriteData
* 函数功能		   : 向LCD写入一个字节的数据
* 输    入         : dat
* 输    出         : 无
*******************************************************************************/		   	   
void LcdWriteData(uchar dat)			//写入数据
{
	LCD1602_E = 0;	//使能清零
	LCD1602_RS = 1;	//选择输入数据
	LCD1602_RW = 0;	//选择写入

	LCD1602_DATAPINS = dat; //写入数据
	Lcd1602_Delay1ms(1);

	LCD1602_E = 1;   //写入时序
	Lcd1602_Delay1ms(5);   //保持时间
	LCD1602_E = 0;
}


/*******************************************************************************
* 函 数 名       : LcdInit()
* 函数功能		 : 初始化LCD屏
* 输    入       : 无
* 输    出       : 无
*******************************************************************************/		   
void LcdInit()						  //LCD初始化子程序
{
 	LcdWriteCom(0x38);  //开显示
	LcdWriteCom(0x0c);  //开显示不显示光标
	LcdWriteCom(0x06);  //写一个指针加1
	LcdWriteCom(0x01);  //清屏
	LcdWriteCom(0x80);  //设置数据指针起点
}





//按指定位置显示一个字符
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
 Y &= 0x1;
 X &= 0xF; //限制X不能大于15,Y不能大于1
 if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
 X |= 0x80; // 算出指令码
 LcdWriteCom(X); //这里不检测忙信号,发送地址码
 LcdWriteData(DData);
}
//按指定位置显示一串字符
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
 unsigned char ListLength;

  ListLength = 0;
 Y &= 0x1;
 X &= 0xF; //限制X不能大于15,Y不能大于1
 while (DData[ListLength] >=0x20) //若到达字串尾则退出
  {
   if (X <= 0xF) //X坐标应小于0xF
    {
     DisplayOneChar(X, Y, DData[ListLength]); //显示单个字符
     ListLength++;
     X++;
    }
  }
}

//unsigned char code M_A[8]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9}; //X反转顺序
//unsigned char code M_B[8]={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1}; //X正转顺序

void Motor_A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B_4 = 1;	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 0;
	MB_3 = 1;
	MB_4 = 1;	
	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 0;
	MB_3 = 1;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 1;
	MB_3 = 1;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 1;
	MB_3 = 0;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 1;
	MB_2 = 1;
	MB_3 = 0;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 1;
	MB_2 = 0;
	MB_3 = 0;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 1;
	MB_2 = 0;
	MB_3 = 0;
	MB_4 = 1;
}

void Motor_B2()
{
	DDelay(3);
	MB_1 = 1;
	MB_2 = 0;
	MB_3 = 0;
	MB_4 = 1;	
	DDelay(3);
	MB_1 = 1;
	MB_2 = 0;
	MB_3 = 0;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 1;
	MB_2 = 1;
	MB_3 = 0;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 1;
	MB_3 = 0;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 1;
	MB_3 = 1;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 0;
	MB_3 = 1;
	MB_4 = 0;	
	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 0;
	MB_3 = 1;
	MB_4 = 1;	
	
	DDelay(3);
	MB_1 = 0;
	MB_2 = 0;
	MB_3 = 0;
	MB_4 = 1;
}



void M_Con(uchar id,uchar dir,uint step)
{
	uchar i;
	if(id == 0)
	{
		if(dir == 0)
		{
			for(i=0;i< step;i++)
			{
				Motor_A1();
			}
		}else if(dir == 1)
		{
			for(i=0;i< step;i++)
			{
				Motor_A2();
			}
		}
	}
	
	
		if(id == 1)
	{
		if(dir == 0)
		{
			for(i=0;i< step;i++)
			{
				Motor_B1();
			}
		}else if(dir == 1)
		{
			for(i=0;i< step;i++)
			{
				Motor_B2();
			}
		}
	}
	
	
	
}




/*******************************************************
函数名称:void MM_CON(uchar dir1,uchar dir2,uint step1,uint step2)
参数说明:dir1 dir2(步进电机1/2的方向 ) step1/step2(步进电机脉冲数)
********************************************************/
void MM_CON(uchar dir1,uchar dir2,uint step1,uint step2)
{
	uchar i;
	uint num;
	if(step1 >=step2)
	{
		num = step1 - step2;
		for(i=0;i< step2;i++)
			{
				if(dir1 == 0){Motor_A1();}else{Motor_A2();}
				if(dir2 == 0){Motor_B1();}else{Motor_B2();}
			}
			for(i=0;i< num;i++)
			{
				if(dir2 == 0){Motor_B1();}else{Motor_B2();}
			}
	}
	
	if(step1< step2)
	{
		num = step2 - step1;
		
		for(i=0;i< step1;i++)
			{
				if(dir1 == 0){Motor_A1();}else{Motor_A2();}
				if(dir2 == 0){Motor_B1();}else{Motor_B2();}
			}
			for(i=0;i< num;i++)
			{
				if(dir1 == 0){Motor_A1();}else{Motor_A2();}
			}		
		
	}
	
}







double sin_ans;
double arc_sin;
uint get_H;
uint get_L;
uchar show;
void main()
{
	LcdInit();
	DisplayListChar(2,0,"Step Motor");
	
	MM_CON(1,0,(uint)(15/2*5),(uint)(18/2*5));
	while(1)
	{

	}
	
}


 

  审核编辑:汤梓红
 
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分