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

📄 调试程序._c

📁 这段程序是步进电机的细分驱动程序
💻 _C
📖 第 1 页 / 共 4 页
字号:
			ep0_txdone();				//端点0IN中断
		}
		if(i_st & 0x04)
		{
			ep1_rxdone();				//端点1OUT中断
		}
		if(i_st & 0x08)
		{
			ep1_txdone();				//端点1IN中断
		}
		if(i_st & 0x10)
		{
			main_rxdone();				//端点2OUT中断
		}
		if(i_st & 0x20)
		{
			main_txdone();				//端点2IN中断 	
		}		
	}
	EIMSK=0x01;
}

//断开USB总线
void disconnect_USB(void)
{
	D12_SetMode(0x02, 0x40 | 0x03);
}

//连接USB总线
void connect_USB(void)
{
	
	D12_SetDMA(0x80 | 0x40|0x10);
	D12_SetMode(0x02|0x10, 0x40 | 0x03);
}

//重新连接到USB总线
void reconnect_USB(void)
{
	ulong clk_cnt;
	disconnect_USB();
	for(clk_cnt=0;clk_cnt<0x0800;clk_cnt++);
	connect_USB();
}

//恢复到未配置状态
void init_unconfig(void)
{
	D12_SetEndpointEnable(0);	
}

//设置配置状态
void init_config(void)
{
	D12_SetEndpointEnable(1);	
}

//从端点号1发送数据
void single_transmit(uchar * buf, uchar len)
{
	if( len <= 16) {
		D12_WriteEndpoint(1, len, buf);
	}
}

//发送端点号1建立代码
void code_transmit(uchar * pRomData, ushort len)
{
	ControlData.wCount = 0;
	if(ControlData.wLength > len)
		ControlData.wLength = len;
	ControlData.pData = pRomData;
	if( ControlData.wLength >= 16) 
	{
		D12_WriteEndpoint(1, 16, ControlData.pData);//发送16字节数据
		ControlData.wCount += 16;
		EIMSK=0x00;							   
		control_state = 1;
		EIMSK=0x01;
	}
	else 
	{
		D12_WriteEndpoint(1, ControlData.wLength, pRomData);//发送16字节内数据
		ControlData.wCount += ControlData.wLength;
		EIMSK=0x00;								 
		control_state = 0;
		EIMSK=0x01;
	}
}

//获取固件版本号
void get_firmware_version()
{
	unsigned char i;

	i = 0x30; // firmware version number
	single_transmit((unsigned char *)&i, 1);
}

//获取端点2缓存区大小
void get_buffer_size()
{
	unsigned char i[4];

	if(bNoRAM != 0) {
		i[0] = 64;
		i[1] = 0;
		i[2] = 0;
		i[3] = 0;
	} else {
		i[0] = 0;
		i[1] = 1;
		i[2] = 0;
		i[3] = 0;
	}
	single_transmit((unsigned char *)&i, 4);
}

//厂商请求处理
void read_write_register(void)
{
	unsigned char len,epstatus;

	if(ControlData.DeviceRequest.bmRequestType & 0x80) {

		if(ControlData.DeviceRequest.wIndex == 0x0472 &&
			ControlData.DeviceRequest.wValue == 0 &&
			ControlData.DeviceRequest.wLength == 1)
			get_firmware_version();//获取固件版本号
		else
		if(ControlData.DeviceRequest.wIndex == 0x0474 &&
			ControlData.DeviceRequest.wValue == 0 &&
			ControlData.DeviceRequest.wLength == 4)
			get_buffer_size();//获取缓冲区大小
		else{
			D12_SetEndpointStatus(0, 1);
			D12_SetEndpointStatus(1, 1);
		}

	}	// if read register
	else{

		if(ControlData.DeviceRequest.wIndex == 0x0471 &&
			ControlData.DeviceRequest.wValue == 0 &&
			ControlData.DeviceRequest.wLength == 6)
		   	{
				if(ControlData.dataBuffer[5]==0x81)
				{
					D12_WriteEndpoint(5, ControlData.dataBuffer[3], MainBuf);//发送数据
				}
				if(ControlData.dataBuffer[5]==0x80)
				{
					EIMSK=0x00;
					outportb(D12_COMMAND, 0x80 + 4);//读取端点状态
					epstatus = inportb(D12_DATA);
					EIMSK=0x01;

					epstatus &= 0x60;
					if (epstatus == 0x60)
						len = Isr_D12_ReadEndpoint(4, 64, MainBuf);//读取双缓冲区数据
 				}
				single_transmit(0, 0);
			}				
		else
			D12_SetEndpointStatus(0, 1);
			D12_SetEndpointStatus(1, 1);
	}	
}

//请求处理子程序
void control_handler()
{
	uchar type, req;
	type = ControlData.DeviceRequest.bmRequestType & (uchar)0x60;   //设备请求类型
	req = ControlData.DeviceRequest.bRequest & (uchar)0x0F;         //USB请求类型
    			          
	if (type == (uchar)0x00) 
	{
	 	switch(req) //调用标准请求
		{
			case 0x00:get_status();
					  break;
			case 0x01:clear_feature();
					  break;
			case 0x03:set_feature();
					  break;
			case 0x05:set_address();
					  break; 
			case 0x06:get_descriptor();
					  break;	  
			case 0x08:get_configuration();
					  break;
			case 0x09:set_configuration();
					  break; 
			case 0x0a:get_interface();
					  break; 
			case 0x0b:set_interface();
					  break; 
			case 0x04:
			case 0x02:
			case 0x07:
			case 0x0c:
			case 0x0d: 
			case 0x0e:
			case 0x0f:D12_SetEndpointStatus(0, 1);
					  D12_SetEndpointStatus(1, 1);
					  break; 
			default	 :break;					  
		}
	}
	else if (type == (uchar)0x40)
	{
		switch(req) //调用标准请求
		{
			case 0x0c:read_write_register();
						 break;
			case 0x00:
			case 0x01:
			case 0x02:
			case 0x03: 
			case 0x04:	  
			case 0x05:
			case 0x06: 
			case 0x07: 
			case 0x08: 
			case 0x09:
			case 0x0a:
			case 0x0b:
			case 0x0d: 
			case 0x0e:
			case 0x0f:
			case 0x10:D12_SetEndpointStatus(0, 1);
						 D12_SetEndpointStatus(1, 1);
						 break;
 
			default	 :break;					  
		}
	}
	else
	{
		D12_SetEndpointStatus(0, 1);
		D12_SetEndpointStatus(1, 1);
	}
}

//中断设置
void INT_Init(void)
{
   EIMSK = 0x00;
   MCUCR = 0X80;
   EICRA = 0x00;  ///
   EIMSK = 0x00;
   
   XMCRA = 0X00;
}

//***************************************************//
//延时函数
//***************************************************//
void DelayMs(uint TimeNum)       //delay Ms
 {
   uint i;
   for(;TimeNum>0;TimeNum--)
    {
       for(i=0;i<30000;i++);
    }
 }
//*****************************************************//
//清洗针头(时间)
//*****************************************************//
void   Pump_Delay(uint delayTime)  //Delay Ms     1//17Ms
  {                                             
     PORTF&=0XFC;
	 DelayMs(10);
     PORTF&=0XFE;
     DelayMs(delayTime);    
	 PORTF|=0X02;
     DelayMs(10);
	 PORTF|=0X01;
  }
//*****************************************************//
//  进样泵排气泡
//*****************************************************//
  void  push_air (void)
  {
     PORTF&=0XFC;
	 DelayMs(10);
     PORTF&=0XFE;
     mustermotor_contorl (17000,1);
	 mustermotor_contorl (17000,0);
	 DelayMs(30);
	 mustermotor_contorl (17000,1);
     mustermotor_contorl (17000,0);
	 DelayMs(30);
	 PORTF|=0X02;
     DelayMs(10);
	 PORTF|=0X01;
  }

//*****************************************************//
//   吸样针头保护
//*****************************************************//
 void  needle_egis (void)
  {
      PIND=PORTD;
	  g[5]=PIND&0x10;
	  while(g[5]==0)
	  {
	    PIND=PORTD;
	    g[5]=PIND&0x10;
	  }
  }

//*****************************************************//
//      进样器初始化
//*****************************************************//
void  muster_init (void)
 { 
	 arm_up ();
	 plate_oneround ();  
	 plate_init();
	 armround_init();
	 find_light();
	 DelayMs(15);
	
	 arm_down(200); 
	 OutputMotor(0x02,0x00,0x00);
	 needle_egis ();
	 arm_down(2900);
     mustermotor_init();
	 push_air ();
  }

//*******************************************************//
//连电脑主机的进样过程
//*******************************************************//
 void   one_course  (void)
    {   uint i;
		
		arm_up ();
		plate_round(Work.strat_number,Work.mission_number);
	    mustermotor_contorl (6500,1);
	    armround_control (1,Work.mission_number,1); 
		DelayMs(18);
		//arm_down(200);
		OutputMotor(0x02,0x00,0x00);
	        
	    needle_egis ();
	    arm_down(3300);
		mustermotor_contorl (volume,1);
        DelayMs(20);
		if(Work.clear_select==2)
		DelayMs(50);
	    arm_up (); 
		for(i=0;i<1500;i++)
	       { 
		      motor_right (2,1,1000);
		   }
		find_light();
	   // armround_control (0,Work.mission_number,1);
	    if(Work.clear_select==1)
		   {
		       armround_control (0,Work.mission_number,0);
		       DelayMs(20);
	           arm_down(200);
		       OutputMotor(0x02,0x00,0x00);
		       needle_egis ();
			   arm_down(6400+zx_value);  
          
		      mustermotor_contorl (volume+300,0);
		      DelayMs(10);
		      arm_up ();
		      over_flag=1; 
              //armround_control (Work.mission_number,0);
			  for(i=0;i<1500;i++)
	           { 
		      motor_left (2,1,1000);
		       }
			    find_light ();
		  } 
		
		      DelayMs(20);
		     // arm_down(200);
		      OutputMotor(0x02,0x00,0x00);
		      needle_egis ();
		      arm_down(3300);
          
		      mustermotor_init();
	          Pump_Delay(clear_S);
              DelayMs(220);
		     if(Work.clear_select==0)
		     over_flag=2; 
    }
//******************************************************//
//电机逆时针旋转
//******************************************************//
void   motor_right (uchar motor_number,uchar modulus ,uint delaytime)
   {   uint t;
       uchar  change;
	   OutputMotor(motor_number,H1DACData[step1[motor_number]],H2DACData[step1[motor_number]]);
	   step1[motor_number]+=modulus;
	   for(t=0;t<delaytime;t++);
	}
//******************************************************//
//电机顺时针旋转
//******************************************************//
void   motor_left (uchar motor_number,uchar modulus,uint delaytime) 
   {   uint t;
       uchar  change;
       OutputMotor(motor_number,H1DACData[step1[motor_number]],H2DACData[step1[motor_number]]);
	   step1[motor_number]-=modulus;
	   for(t=0;t<delaytime;t++);
	 }
//*******************************************************//
//旋转臂找光点
//*******************************************************//
/*void   find_light ()
 {
     uint  i;
     PIND=PORTD;
	 g[1]=PIND&0x08;
     if(g[1]==0)
	  {	
	    PIND=PORTD;
	    g[1]=PIND&0x08;
	    PIND=PORTD;
	    g[5]=PIND&0x10;
	    while(g[5]==0)
	      {
	       PIND=PORTD;
	       g[5]=PIND&0x10;
           motor_right (2,1,600);
	      }
		   for(i=0;i<100;i++)
	         { 
			   motor_right (2,1,600);
			 }
			  while(g[5]==0);
	   }	
	  else
	   {
	     PIND=PORTD;
	     g[1]=PIND&0x08;
	     PIND=PORTD;
	     g[5]=PIND&0x10;
	     while(g[5]==0)
	       {
	         PIND=PORTD;
	         g[5]=PIND&0x10;
             motor_left (2,1,600);
	       }
		   for(i=0;i<100;i++)
	         { 
			   motor_left (2,1,600);
			 }
			 while(g[5]==0);
	   }   
	}*/
 void   find_light (void)
  {
      uint  i;
      PIND=PORTD;
	  g[1]=PIND&0x08;
      if(g[1]==0)
	  {
	    while(g[1]==0)
	    {	
	      PIND=PORTD;
	      g[1]=PIND&0x08;
	      motor_right (2,1,1000);
        }
	  }
	 else
	 {
	      while(g[1])
	    {	
	      PIND=PORTD;
	      g[1]=PIND&0x08;
	      motor_left (2,1,1000);
        } 
	 }
  }
//******************************************************//
//上升臂上升
//******************************************************//
 void  arm_up (void)
   {  uint i;
      PIND=PORTD;
      g[0]=PIND&0x04;  
      while(g[0])          
       {
        PIND=PORTD;
        g[0]=PIND&0x04;
        motor_right (1,8,800);
	   }
	      for(i=0;i<10;i++)
	       { 
		    motor_left (1,8,800);
		   }
		  for(i=0;i<60;i++)
	       { 
		      motor_right (1,8,800);
		   }
	      while(g[0]);
		  OutputMotor(0x01,0x00,0x00);
    }
//***************************************************//
//旋转臂旋转初使化
//***************************************************//
 void   armround_init  (void)
    {  uint  i;
	 PIND=PORTD;
	 g[1]=PIND&0x08;
     if(g[1]==0)
	    { 
		  while(g[1])
		  {
	        for(i=0;i<1000;i++)
	       {
	         motor_right (2,1,1000);
	       }
		 }
		    if(g[1]==0)
		    find_light(); 
		    for(i=0;i<1000;i++)
	       {
	         motor_right (2,1,1000);
	       }
		    
		}
	  else 
	    {

⌨️ 快捷键说明

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