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

📄 motor.c

📁 可用于控制步进电机正反转的51单片机汇编程序
💻 C
字号:

/*****************************************************************/ 
/*                                                                                                                               
/*              热释红外探测人体,USB摄像头进行观察                                                                                                                                                                         
/*                                                                                   
/*                                                                                                                      
/*               
/*                                                                                                                                                                                                                                                    
/******************************************************************/
    
#include <reg52.h>       
#include <string.h>    

#define uchar unsigned char
#define uint  unsigned int



uchar code FFW1[4]={0x03,0x09,0x0c,0x06};//正转数组
uchar code REV1[4]={0x06,0x0c,0x09,0x03};//反转数组
uchar code FFW2[4]={0x30,0x90,0xc0,0x60};//正转数组
uchar code REV2[4]={0x60,0xc0,0x90,0x30};//反转数组




bit    StartFlag1=0; 
bit    StartFlag2=0;
bit    SingalFlag=0;
sbit   sensor_signal=P3^3;	
sbit  photoelectric_switch=P3^2;
uchar rate=0xff;
uchar motorstep1=0;
uchar motorstep2=0;
uchar con_step;
uchar count1;
uchar count2;
uchar j=0;
bit con_direction;
uchar m=0,n=0;
   
/********************************************************/
/*                                                  
/* 延时	1ms
/* 11.0592MHz时钟,                                    
/*                                                      
/********************************************************/
void delay()
 {                           
   uchar k;
   uint s;
   k = rate;
   do
    {
       for(s = 0 ; s <100 ; s++) ;  
    }while(--k);
 }

 

/********************************************************/
/*
/*步进电机正转
/*
/********************************************************
void  motor2_ffw()
{ 
   uchar i;
 
    for (i=0; i<4; i++)      //一个周期转30度
    {  motorstep2++;
	  	 if(motorstep2==1)EX1=1;
		 
       P1 = FFW2[i];//取数据
       delay();                 //调节转速
    }
}
*******************************************************/
/*
/*步进电机反转
/*
/**********************************************
void  motor2_rev()
{ 
   uchar i;
 
    for (i=0; i<4; i++)      //一个周期转30度
    {  motorstep2++;
	   if(motorstep2==1)EX1=1;
       P1 = REV2[i]; //取数据
       delay();                 //调节转速
    }
}

**********/
/********************************************************
*                                                       
*  主程序                                               
*                                                      
*********************************************************/
   void main(void)
{   
	 uint a,b,c;
   
   
  
   /*******************定时器0初始化************************/
    count1=0;
  	TMOD=0x61;
 	TH0=-50000/256;
    TL0=-50000%256;							 
	PT0=0;
	ET0=1;
	TR0=1;
    EA=1;

	/*****************定时器1初始化**************************/
	TL1=0xff;
	TH1=0xff;
	 ET1=1;
	 TR1=1;				 
	 PT1=1;
  	/******************外部中断0初始化——光电对管1**********************/
   	    EX0=1;				
  	    PX0=0;
        IT0=0;				
		EA=1;
	/******************外部中断1初始化——光电对管2********************/
	   
		EX1=1;
	    PX1=0;
        IT1=0;				
	   	EA=1;			
  	   
	/******************************************************/





   while(1)



  {		
  
  		 
  												
   	 	if(SingalFlag==1)
	   {           
				   
				  EA=0; 	
				   con_direction=StartFlag1;
				   con_step=motorstep1;
					
					//MOTOR2转至信号输入方向				 
		           	 if(con_direction==1) { 
					                      	while(motorstep2<=con_step)				  
											{ P1= REV2[m];
											  motorstep2++;
											  if(motorstep2==1)EX1=1;
											 m++;
											 if(m==4)m=0;
											 delay();
		              						}
							        	
										
										} 
	      			  
		  
		                  else          {
						  
						                           while(motorstep2<=con_step)				  
											{ P1= FFW2[m];
											 motorstep2++;
											  if(motorstep2==1)EX1=1;
											m++;

											 if(m==4)m=0;
											 delay();
		              						}
						
		  		  						    
	 								 }

			    	
				
				  EA=1;

			  //延时1000*10ms=10s
			  for(a=0;a<=1000;a++)
			      for(b=0;b<=10;b++)
				      for(c=0;c<=120;c++);
	              

		    for(a=0;a<=20;a++)	{ET0=0;	ET1=0;}
			
				//返回致零角度
		         if(con_direction==1) {  
						                    while(motorstep2!=0)				  
											{ P1= FFW2[m];

											   motorstep2++;
											  if(motorstep2==1)EX1=1;
											 m++;
											 if(m==4)m=0;
											 delay();
		              						}
				                      }


					  else   if(con_direction==0)          {    while(motorstep2!=0)				  
											                          { P1= REV2[m];
																		  motorstep2++;
											                              if(motorstep2==1)EX1=1;
											                              m++;
											                                 if(m==4)m=0;
											                                     delay();
		              						               }           }

									  
			for(a=0;a<=20;a++){ET0=1;ET1=1;}
			
				
				SingalFlag=0;		           
	  
	  
	   }			 
														
  } 


}





/********************************************************外部0中断服务——MOTOR1零角度中断*****************/
int0_srv() interrupt 0 using 0			//电机起始定位ISP
 { 
    	
	        EX0=0;							 //关中断0
	     
		    StartFlag1^=1;
		    motorstep1=0;			
		                       //开中断
		

	  	   								  
   }





 /**********************************************************外部中断1服务—— **********************************/  
 
 int1_srv() interrupt 2		using  2
 { 
    
	     
	   
		 EX1=0; 
		StartFlag2^=1;
	   motorstep2=0;		
		                   
	 
	  								
   }
 



/********************************************定时器0中断服务——MOTOR1步进中断***************************************************/
void timer0(void) interrupt 1	using 1
{

    TH0=-50000/256;
	TL0=-50000%256;
	
	
	 if( StartFlag1==1)P1 = REV1[j]; 
			     else  P1 = FFW1[j];			              
			
	 count1++;
	 if(count1==10)
	       {count1=0; 
		     motorstep1++;
			 if(motorstep1==12)EX0=1;
			 j++;
			 if(j==4){j=0;}
			 }				
					   
}
																			

  /********************************定时器3——热释红外输入服务**************************************/
   void timer1(void) interrupt 3  using  3

 {	
 
 	  							
	      SingalFlag=1;
		  con_step=motorstep1;	
    
	
 
 }										

⌨️ 快捷键说明

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