基于51单片机的两相步进电机控制系统proteus仿真

硬件设计

链接:https://pan.baidu.com/s/19qBroJVEvd2DRPwfSVv80Q
提取码:kxoc
在这里插入图片描述

代码设计

#include "MAIN.H"
#include "LCD1602.h"
#include "PCF8591.h"


unsigned char code FFW_X[8]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //X反转顺序

unsigned char code FFZ_X[8]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //X正转顺序

unsigned char code FFW_Y[8]={0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90}; //Y反转顺序

unsigned char code FFZ_Y[8]={0x90,0x80,0xc0,0x40,0x60,0x20,0x30,0x10}; //Y正转顺序

unsigned char Speed = 30,Direction = 1,X_Y = 0,stop = 1,mode = 0;	 
unsigned int k, temp;
int Angle = 0;


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

* 函 数 名 : main

* 函数功能 : 主函数

* 输    入 : 无

* 输    出 : 无

*******************************************************************************/
void main(void)
{	
	System_Init();
	while(1)
	{
		temp = Read_PCF();		// 读取AD转换后的数字量
		PCF_Output(temp);		// DA转换,模拟量输出
		//将数字量转换为十进制,*100 + 0.5将小数转换为整数,便于显示和存储
		k = (float)temp * 5 / 256 * 100 + 0.5;	// 将得到的结果存入数组

		Key_Scan();
		if(mode == 0)Motor();
		if(mode == 1)
		{
			Angle = k/61*45;
			if(Angle >= 0)
			{
					LCD1602_Location(2,7);
					LCD1602_Write_Num(Angle);	
					if(X_Y==0)
						GPIO_MOTOR = FFW_Y[k/62];	
					else
						GPIO_MOTOR = FFW_X[k/62];
			}
			Delay(20);		
		}

	}
}

void System_Init(void)
{
	Init_PCF();
	Init_LCD1602();
	LCD1602_Location(1,1);
	LCD1602_write_word("Welcome To Use!");
	Delay(1000);
	LCD1602_Clear();	
	LCD1602_Location(1,1);
	LCD1602_write_word("Speed:");	
	LCD1602_Write_Num(Speed);
	LCD1602_Location(2,1);
	LCD1602_write_word("Angle:");	
	LCD1602_write_word("Null");	
	LCD1602_Location(1,11);
	LCD1602_write_word("Stop");	
	LCD1602_Location(2,12);
	LCD1602_write_word("X+");
	LCD1602_Location(2,15);
	LCD1602_write_word("O");		
}
/*******************************************************************************
* 函 数 名: Motor
* 函数功能: 电机旋转函数
* 输    入: 无
* 输    出: 无
*******************************************************************************/
void Motor()
{
	unsigned char i;
	if(Speed>0)
	for(i=0;i<8;i++)
	{
		if(Direction==1&&X_Y==0&&stop==0)		 //正转&Y电机&电机没按下停止
			GPIO_MOTOR = FFW_Y[i];
		if(Direction==2&&X_Y==0&&stop==0)		 //反转&Y电机&电机没按下停止
			GPIO_MOTOR = FFZ_Y[i];
		if(Direction==1&&X_Y!=0&&stop==0)		 //正转&X电机&电机没按下停止
			GPIO_MOTOR = FFW_X[i];
		if(Direction==2&&X_Y!=0&&stop==0)		 //反转&X电机&电机没按下停止
			GPIO_MOTOR = FFZ_X[i];
		Delay(1000/Speed); //调节转速
	}
}
/*******************************************************************************
* 函 数 名  : Delay
* 函数功能  : 延时
* 输    入  : t
* 输    出  : 无
*******************************************************************************/
void Delay(unsigned int t)
{
 unsigned int k;
 while(t--)
 {
	for(k=0; k<80; k++);
 }
}
/*******************************************************************************
* 函 数 名  : Key_Scan
* 函数功能  : 按键
* 输    入  : 无
* 输    出  : 无
*******************************************************************************/
void Key_Scan(void)
{
	unsigned int i,j; 
	if(K1==0) //检测按键K1是否按下
	{
		Delay(1); //消除抖动
		if(K1==0)
		{
			if(stop == 1)
			{
				stop=0;		  
				LCD1602_Location(1,11);
				LCD1602_write_word("Move");		
				LCD1602_Location(2,7);
				LCD1602_write_word("Null");					
			}

			else
			{
				stop=1;				   //stop为控制电机停止的标志位,但它为1时,电机停止
				LCD1602_Location(1,11);
				LCD1602_write_word("Stop");	
			}
		}
		while((i<300)&&(K1==0))//检测按键是否松开
		{
			Delay(1);
			i++;
		}
		i=0;
	}
	
 if(K2==0) //检测按键K1是否按下
 {
		Delay(1); //消除抖动
		if(K2==0)
		{
			Direction=1;	 	//Direction 转向标志位,为2时为正转
			LCD1602_Location(2,13);
			LCD1602_write_word("+");		
		}
		while((i<200)&&(K2==0))//检测按键是否松开
		{
			Delay(1);
			i++;
		}
		i=0;
 }
/********************检测按键K2是否按下***********************/
 if(K3==0) //检测按键K2是否按下
 {
		Delay(1); //消除抖动
		if(K3==0)
		{
			Direction=2;		  //Direction 转向标志位,为2时为反转
			LCD1602_Location(2,13);
			LCD1602_write_word("-");		
		}
		while((i<200)&&(K3==0))//检测按键是否松开
		{
			Delay(1);
			i++;
		}
		i=0;
 }
/********************检测按键K3是否按下***********************/
		if(K4==0)//检测按键K3是否按下
		{
			Delay(1);//消除抖动
			if(K4==0)
			{
				if(Speed < 100)
				Speed += 5;
				LCD1602_Location(1,7);
				LCD1602_Write_Num(Speed);
			}
		while((i<200)&&(K4==0))//检测按键是否松开
		{
		Delay(1);
		i++;
		}
		i=0;
 }
 /********************检测按键K3是否按下***********************/
 if(K5==0) //检测按键K4是否按下
 {
		Delay(1); //消除抖动
		if(K5==0)
		{
			if(Speed > 0)
			Speed -= 5;
			LCD1602_Location(1,7);
			LCD1602_Write_Num(Speed);	
		}
		while((i<200)&&(K5==0))//检测按键是否松开
		{
			Delay(1);
			i++;
		}
		i=0;
 }
 if(K6==0)//检测按键K1是否按下
 {
		Delay(1);//消除抖动
		if(K6==0)
		{ 
//   	Direction=0;				   //X_Y 电机的参数  X_Y=0为Y电机 X_Y=1为X电机
			if(X_Y==0)				   //当切换电机时,对Direction=0;对标志位清零,防止上个电机的转向影响本次所控电机
			{
				X_Y=1;	
				LCD1602_Location(2,12);
				LCD1602_write_word("Y");
			}
			else
			{
				X_Y=0;	
				LCD1602_Location(2,12);
				LCD1602_write_word("X");
			}
		}
		while((i<400)&&(K6==0))//检测按键是否松开
		{
			Delay(1);
			i++;
		}
		i=0;
		}
	if(K7==0)//检测按键K1是否按下
	{
		Delay(1);//消除抖动
		if(K7==0)
		{ 
			if(Angle >= 0)
			{
				if(Direction==1&&stop==1)		 //正转&Y电机&电机没按下停止
				{
					if(j >= 8)j -= 8;
					Angle += 45;
					if(Angle >= 360)Angle -= 360;
					j++;
					LCD1602_Location(2,7);
					LCD1602_Write_Num(Angle);	
					
					if(X_Y==0)
						GPIO_MOTOR = FFW_Y[j];	
					else
						GPIO_MOTOR = FFW_X[j];
				}
					
				if(Direction==2&&stop==1)		 //反转&Y电机&电机没按下停止
				{
					if(j > 0)
					{
						Angle -= 45;	
						if(Angle <= 0)Angle = 0;
						j--;
						LCD1602_Location(2,7);
						LCD1602_Write_Num(Angle);	
						if(X_Y==0)
							GPIO_MOTOR = FFW_Y[j];	
						else
							GPIO_MOTOR = FFW_X[j];					
					}
					else
						j = 0;
				}			
			}
			Delay(20);
		}
		while((i<2000)&&(K7==0))//检测按键是否松开
		{
			Delay(1);
			i++;
		}
		i=0;
	}
	if(K8==0)//检测按键K1是否按下
	{
		Delay(1);//消除抖动
		if(K8==0)
		{ 
			mode = 1;
			LCD1602_Location(2,15);
			LCD1602_write_word("C");
		}
		while((i<500)&&(K8==0))//检测按键是否松开
		{
			Delay(1);
			i++;
		}
		i=0;
	}	
	if(K9==0)//检测按键K1是否按下
	{
		Delay(1);//消除抖动
		if(K9==0)
		{ 
			mode = 0;
			LCD1602_Location(2,15);
			LCD1602_write_word("O");
		}
		while((i<500)&&(K9==0))//检测按键是否松开
		{
			Delay(1);
			i++;
		}
		i=0;
	}		
}
  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值