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

📄 sys.c

📁 PWM输出控制三路步进电机
💻 C
📖 第 1 页 / 共 2 页
字号:
	else
	{
		asm("nop");		
		if(Puls_count1 > Cofficient)
    	{
			asm("nop");		
			if(M_c_position[1])
			{
				asm("nop");		
				M_c_position[1] --;
			}
			asm("nop");		
			Puls_count1 -= Cofficient;
		}
	}
	asm("nop");		
	if(M_c_direct[2])//channel 3
	{
		asm("nop");		
		if(Puls_count2 > Cofficient)
    	{
			asm("nop");		
			M_c_position[2] ++;
			Puls_count2 -= Cofficient;
			if(M_c_position[2] > 0xf7) 
			{
				asm("nop");		
			    M_c_position[2]=0xf8;
				Start_data[2]=0;
				if(End_data[2] >100)
				{
					asm("nop");		
				  	End_data[2]=100;
				}
			}								
		}
	}
	else
	{
		asm("nop");		
		if(Puls_count2 > Cofficient)
    	{
			asm("nop");		
			if(M_c_position[2])
			{
				asm("nop");		
				M_c_position[2] --;
			}
			asm("nop");		
			Puls_count2 -= Cofficient;
		}
	}
}
//IN: M_0_position/M_0_speed. OUT:Start_data/Const_T/End_data
void Data_Process(void)	//controller module
{
	uchar i,u;
	uint j,p,h;
	asm("nop");
	for(i=0; i<3; i++)
	{
		asm("nop");		
		if(M_state[i])//running
		{	
			asm("nop");		
			if(M_0_position[i] && M_0_speed[i])//2nd data no use
			{
				asm("nop");		
				if(M_c_direct[i])//up
				{				
					asm("nop");		    
					u =	M_c_position[i] + (uchar)((Start_data[i] +End_data[i])/Cofficient);	
				    if(u < M_0_position[i])
					{ //add 2nd data, same direct, running on constant speed
						asm("nop");		
						if(Start_data[i]>10)
						{
							asm("nop");		
					    	Start_data[i] += (uint)(M_0_position[i] - u)*Cofficient; 					    
						}
					  // speed will be change!!
					}
					else
					{
						asm("nop");		
						p = (uint)(u - M_0_position[i])*Cofficient;
						if(Start_data[i] >= p)
						{
							asm("nop");		
					    	Start_data[i] -= p; 					    
						}
						else
						{
							asm("nop");		
						 	Start_data[i]=2;
						}
   				  	  //Start_data[i] %= Cofficient; //cancel current action
 					}
				}
				else   //down
				{
					asm("nop");		
					u =	M_c_position[i] - (uchar)((Start_data[i] +End_data[i])/Cofficient);	
	    			if(u > M_0_position[i])
					{ //add 2nd data, same direct, running on constant speed
						asm("nop");		
						if(Start_data[i]>10)
						{
							asm("nop");		
					    	Start_data[i] += (uint)(u - M_0_position[i])*Speed_Coff; 					   
						}
					  // speed will be change!!
					}
					else
					{
						asm("nop");		
						p = (uint)(M_0_position[i] -u)*Cofficient; 
						if(Start_data[i]> p)
						{
							asm("nop");		
							Start_data[i] -= p; 
						}
						else
						{
							asm("nop");		
						 	Start_data[i]=2;
						}					    
   				  	  		//Start_data[i] %= Cofficient; //cancel current action
					}
				}
				asm("nop");		
				M_0_position[i]=0;
				M_0_speed[i]=0;  //input data has already used! 
			}		
		}
		else //stoped
		{
			asm("nop");		
		    if(M_0_position[i] || M_0_speed[i])//2nd data no use
			{
				asm("nop");		
				if(M_c_position[i] > M_0_position[i])
				{	//down 
					asm("nop");		
					M_c_direct[i]=0;
					j= (uint)(M_c_position[i] - M_0_position[i]) * Cofficient;					
					asm("nop");		
					if(j < 300)
					{
						asm("nop");		
						Const_T[i] =  34-2*M_0_speed[i];//highest freq.
						Start_data[i] =  j/2;//30 +
						End_data[i] = Start_data[i]; 								
						Raise_data[i] = 0;//End_data[i]+20-M_0_speed[i]*2;
					}
					else
					{
						asm("nop");		
						End_data[i] = (uint) Speed_Coff *  M_0_speed[i]+20;
						Const_T[i] =  34-2*M_0_speed[i];//highest freq.
						Start_data[i] = j - End_data[i];// + j/32;					
						Raise_data[i] = 0;//End_data[i]+ 20-M_0_speed[i]*2;
					}				
				}
				else if(M_c_position[i] < M_0_position[i])
				{
					//up 
					asm("nop");		
					M_c_direct[i]=1;
					if(M_0_position[i] >0xF8)//250 limit
					{
						asm("nop");		
					   M_0_position[i] =0xF8;			 	
					}
					asm("nop");		
					j= (uint)(M_0_position[i] - M_c_position[i]) * Cofficient;
					if(j <200)
					{
						asm("nop");		
						Const_T[i] =  34-2*M_0_speed[i];//highest freq.
						Start_data[i] = j/2;
						End_data[i] = Start_data[i];
						Raise_data[i] = 0;//End_data[i]+20-M_0_speed[i]*2; 					
									
					}
					else
					{
						asm("nop");		
						End_data[i] = (uint) Speed_Coff *  M_0_speed[i]+20;
						Start_data[i] = j - End_data[i];
						Const_T[i] =  34-2*M_0_speed[i];					
						Raise_data[i] = 0;//End_data[i]+20-M_0_speed[i]*2;
					}
				}
				else
				{
					asm("nop");
				}	
			}
			if(!i)
			{
				asm("nop");		
				Puls_count0=20;
			}
            else if(i==1)
			{
				asm("nop");		
				Puls_count1=20;
			}
			else
			{
				asm("nop");		
			    Puls_count2=20;
			}
			if(!M_c_direct[0])
			{
				asm("nop");		
				PORTE |= 0x10;
			}
			else
			{
				asm("nop");		
			    PORTE &= 0xEF;
			}
			if(!M_c_direct[1])
			{
				asm("nop");		
				PORTB |= 0x40;
			}
			else
			{
				asm("nop");		
			    PORTB &= 0xB0;
			}
			if(!M_c_direct[2])
			{
				asm("nop");		
				PORTE |= 0x20;
			}
			else
			{
				asm("nop");		
			    PORTE &= 0xD0;  
			}
			asm("nop");		
			M_0_position[i]=0;
			M_0_speed[i]=0;  //input data has already used! 
		}
	}
}

//check and start going 
void Check_Process(void)
{
	uchar i,k;
	uint j;
	asm("nop");		
	for(i=0; i<3; i++)
	{	//running or stop check
		asm("nop");		
	   j= Start_data[i] +End_data[i];
	   if(j)
	   {	   	
	   		 asm("nop");		
		     M_state[i]=1;
			 if(!i)//channel 1
			 {		 
			 	asm("nop");		
				k =TCCR3A;			
				if(k != 0x40)
				{
					asm("nop");		
					TCCR3A  = 0x40;
					TCCR3B	 =0x0B;
					OCR3A = 2*Low_freq; //first step, 500hZ,
					Start_flag[0]=1;
				}
			 }		 	
			 if(i==1)//channel 2
			 {		 
			 	asm("nop");		
				k =TCCR2;			
				if(k != 0x1B)
				{
					asm("nop");		
					TCCR2  = 0x1B;
					OCR2 = 2*Low_freq; //first step, 500hZ, Start_data[i];
					Start_flag[1]=1;						
				}
			 }		 
			 if(i==2)//channel
			 {		 
			 	asm("nop");		
				k =TCCR1A;			
				if(k != 0x40)
				{
					asm("nop");		
					TCCR1A  = 0x40;
					TCCR1B	 =0x0B;
					OCR1A = 2*Low_freq; //first step, 500hZ, Start_data[i];
					Start_flag[2]=1;
				}
			 }		 
		}
       else
	   {
	   		 asm("nop");		
		     M_state[i]=0;
		     if(i==0) //Timer0
			 {
			 		asm("nop");		
			    	TCCR3A=0; TCNT3=0;TCCR3B=0;
			  }			 
		      else if(i==1) //timer2
			  {
			  		asm("nop");		
				    TCCR2=0; TCNT2=0;
		       }
		       else
			   {
			   		asm("nop");		
					TCCR1A=0; TCNT1=0; TCCR1B=0;
			   }
		}
	}
}


void Power_ON(void)
{
uchar i;
	asm("nop");		
	if(!power0)
	{
		asm("nop");		
		return;
	}
	if(power_count < 10)
	{
		asm("nop");		
		return;
	}
    if(power1)
	{
		asm("nop");				
		if(power1 == 1)
		{
			asm("nop");	
			if(!(Reset_data &0x01))//off zero
			{
				asm("nop");	
				TCCR3A=0; TCCR3B=0;
				M_c_direct[0]=0;
				Start_data[0] =0;  
				if(power_count1 >20)
				{	
					asm("nop");	
					power1=2; 
				}
			}
			else
			{
				asm("nop");	
				if(power_on_flag10 !=1)
				{
					asm("nop");	
					Start_data[0] =3000; 
				}
				power_on_flag10=1;
				power_count1=0;
				M_c_direct[0]=1;										 
				TCCR3A  = 0x40;
				TCCR3B	 =0x0B;
				OCR3A = 60; //first step, 500hZ,
				//Start_flag[0]=1;
			}
		}
		if(power1 == 2)
		{
			asm("nop");	
			if(Reset_data &0x01)//zero
			{
				asm("nop");	
				TCCR3A=0; TCCR3B=0;
				Start_data[0] =0;
				power1=0;
			}
			else
			{
				asm("nop");	
				M_c_direct[0]=0;
				Start_data[0] =63000;  
				TCCR3A  = 0x40;
				TCCR3B	 =0x0B;
				OCR3A = 60; 
			}
		}
	}
    if(power2)
	{
		asm("nop");	
		if(power2 == 1)
		{
			asm("nop");	
			if(!(Reset_data &0x02))//off zero
			{
				asm("nop");	
				TCCR2=0; 
				M_c_direct[1]=0;
				Start_data[1] =0; 
				if(power_count2 >20)
				{	
					asm("nop");	
					power2=2; 
				}
			}
			else
			{
				asm("nop");	
				power_count2=0;
				M_c_direct[1]=1;
				if(power_on_flag20 !=1)
				{
					asm("nop");	
					Start_data[1] =3000; 
				}
				power_on_flag20=1;				

				TCCR2  = 0x1B;
				OCR2 = 60; //first step, 500hZ,
				//Start_flag[0]=1;
			}
		}
		if(power2 == 2)
		{
			asm("nop");	
			if(Reset_data &0x02)//zero
			{
				TCCR2=0; 
				Start_data[1] =0;
				power2=0;
			}
			else
			{
				M_c_direct[1]=0;			
				Start_data[1] =63000;  
				TCCR2  = 0x1B;
				OCR2 = 60;
			}
		}
	}
    if(power3)
	{
		asm("nop");	
		if(power3 == 1)
		{
			asm("nop");	
			if(!(Reset_data &0x04))//off zero
			{
				TCCR1A=0; TCCR1B=0;
				M_c_direct[2]=0;
				Start_data[2] =0;  
				if(power_count3 >20)
				{	
					asm("nop");	power3=2; 
				}
			}
			else
			{
				asm("nop");	
				power_count3=0;
				M_c_direct[2]=1;
				if(power_on_flag30 !=1)
				{
					asm("nop");	
					Start_data[2] =3000; 
				}
				power_on_flag30=1;
				TCCR1A  = 0x40;
				TCCR1B	 =0x0B;
				OCR1A = 60; //first step, 500hZ,
				//Start_flag[0]=1;
			}
		}
		if(power3 == 2)
		{
			asm("nop");	
			if(Reset_data &0x04)//zero
			{
				asm("nop");	
				TCCR1A=0; TCCR1B=0;
				Start_data[2] =0;
				power3=0;
			}
			else
			{
				asm("nop");	
				M_c_direct[2]=0;
				Start_data[2] =63000;  
				TCCR1A  = 0x40;
				TCCR1B	 =0x0B;
				OCR1A = 60; 
			}
		}
	}
	if(!M_c_direct[0])
	{
		asm("nop");	
		PORTE |= 0x10;
	}
	else
	{
		asm("nop");	
	    PORTE &= 0xEF;
	}
	if(!M_c_direct[1])
	{
		asm("nop");						
		PORTB |= 0x40;
	}
	else
	{
		asm("nop");	
	    PORTB &= 0xB0;
	}
	if(!M_c_direct[2])
	{
		asm("nop");	
		PORTE |= 0x20;
	}
	else
	{
		asm("nop");	
	    PORTE &= 0xD0;  
	}
	i = power1+power2+power3;
	if(!i)
	{
		asm("nop");	
		power0=0;
	}
}

⌨️ 快捷键说明

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