基本信息
源码名称:步进电机调速 示例源码
源码大小:2.92KB
文件格式:.c
开发语言:C/C++
更新时间:2019-01-25
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


/*******************************************************************************
* 实 验 名		 : 步进电机实验
* 使用的IO	     : 电机用P1口,键盘使用P3.0、P3.1、P3.2、P3.3
* 实验效果       : 按下K1键,顺时针转,按下K2键,逆时针转,按下K3键,低速,
*按下K4键,高速。
* 注    意		 :由于P3.2口跟红外线共用,所以做按键实验时为了不让红外线影响实验
*效果,最好把红外线先取下来。
*******************************************************************************/ 
#include "reg52.h"

//电机IO
#define GPIO_MOTOR P1
//sbit F1 = P1^0;
//sbit F2 = P1^1;
//sbit F3 = P1^2;
//sbit F4 = P1^3;
//按键IO
sbit K1=P3^0;
sbit K2=P3^1;
sbit K3=P3^2;
sbit K4=P3^3;

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

unsigned char Direction,Speed;
void Delay(unsigned int t);
void  Motor(); 

/*******************************************************************************
* 函 数 名       : main
* 函数功能		 : 主函数
* 输    入       : 无
* 输    出       : 无
*******************************************************************************/

void main(void) 
{ 
	unsigned char i;
	Speed=30;
  while(1)
	{
		if(K1==0)		//检测按键K1是否按下
		{
			Delay(1);	//消除抖动
			if(K1==0)
			{
				Direction=1;
			}
			while((i<200)&&(K1==0))	 //检测按键是否松开
			{
				Delay(1);
				i  ;
			}
			i=0;
		}
		if(K2==0)		//检测按键K1是否按下
		{
			Delay(1);	//消除抖动
			if(K2==0)
			{
				Direction=2;
			}
			while((i<200)&&(K2==0))	 //检测按键是否松开
			{
				Delay(1);
				i  ;
			}
			i=0;
		}			
		if(K3==0)		//检测按键K1是否按下
		{
			Delay(1);	//消除抖动
			if(K3==0)
			{
				Speed=13;
			}
			while((i<200)&&(K3==0))	 //检测按键是否松开
			{
				Delay(1);
				i  ;
			}
			i=0;
		}
		if(K4==0)		//检测按键K1是否按下
		{
			Delay(1);	//消除抖动
			if(K4==0)
			{
				Speed=40;
			}
			while((i<200)&&(K4==0))	 //检测按键是否松开
			{
				Delay(1);
				i  ;
			}
			i=0;
		}		
		Motor();
	}
}
/*******************************************************************************
* 函 数 名         : Motor
* 函数功能		   : 电机旋转函数
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/

void  Motor()
{ 
	unsigned char i;
	for(i=0;i<8;i  )
	{
		if(Direction==1) 
			GPIO_MOTOR = FFW[i]&0x1f;  //取数据
		if(Direction==2) 
			GPIO_MOTOR = FFZ[i]&0x1f;
		Delay(Speed);	//调节转速	
	}	                  
}
/*******************************************************************************
* 函 数 名         : Delay
* 函数功能		   : 延时
* 输    入         : t
* 输    出         : 无
*******************************************************************************/
void Delay(unsigned int t)
{                           
	unsigned int k;
	while(t--)
	{
		for(k=0; k<80; k  )
		{ }
	}
}