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

📄 调试程序1.i

📁 这段程序是步进电机的细分驱动程序
💻 I
📖 第 1 页 / 共 4 页
字号:
	j = inportb(0);
 	if(j > len)
		j = len;
 	for(i=0; i<j; i++)
		*(buf+i) = inportb(0);
	outportb(1, 0xF2);
 	return j;
}

 unsigned char Isr_D12_ReadEndpoint(unsigned char endp,unsigned char len,unsigned char * buf)
{
	unsigned char i, j;

(*(volatile unsigned char *)0x59)=0x00;
	outportb(1, endp);
	if((inportb(0) & 0x01) == 0)
	{
(*(volatile unsigned char *)0x59)=0x01;
		return 0;
	}
 	outportb(1, 0xF0);
	j = inportb(0);
	j = inportb(0);
 	if(j > len)
		j = len;
 	for(i=0; i<j; i++)
		*(buf+i) = inportb(0);
	outportb(1, 0xF2);
(*(volatile unsigned char *)0x59)=0x01;
 	return j;
}


 unsigned char D12_WriteEndpoint(unsigned char endp,unsigned char len,unsigned char * buf)
{
	unsigned char i;

(*(volatile unsigned char *)0x59)=0x00;
 	outportb(1, endp);
	i = inportb(0);
 	outportb(1, 0xF0);
	outportb(0, 0);
	outportb(0, len);
	for(i=0; i<len; i++)
		outportb(0, *(buf+i));
 	outportb(1, 0xFA);
(*(volatile unsigned char *)0x59)=0x01;
	return len;
}


void D12_AcknowledgeEndpoint(unsigned char endp)
{
	outportb(1, endp);
	outportb(1, 0xF1);
	if(endp == 0)
	outportb(1, 0xF2);
}


void get_status(void)
{
 unsigned char endp, txdat[2];
 unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & (unsigned char)0x1F;
 unsigned char c;

 	if (bRecipient == (unsigned char)0x00)
	{
		if(remote_wakeup == 1)
			txdat[0] = 3;
		else
		  txdat[0] = 1;
		  txdat[1]=0;
		  single_transmit(txdat, 2);
	}
	else if (bRecipient == (unsigned char)0x01)
	{
		txdat[0]=0;
		txdat[1]=0;
		single_transmit(txdat, 2);
	}
	else if (bRecipient == (unsigned char)0x02)
	{
		endp = (unsigned char)(ControlData.DeviceRequest.wIndex & (unsigned char)0x3);
		if (ControlData.DeviceRequest.wIndex & 0x80)
			c = D12_SelectEndpoint(endp*2 + 1);
		else
			c = D12_SelectEndpoint(endp*2);
		if(c & 0x02)
			txdat[0] = 1;
		else
			txdat[0] = 0;
		txdat[1] = 0;
		single_transmit(txdat, 2);
	}
	else
	{
		D12_SetEndpointStatus(0, 1);
		D12_SetEndpointStatus(1, 1);
	}
}


void clear_feature(void)
{
 unsigned char endp;
 unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & (unsigned char)0x1F;

	if (bRecipient == (unsigned char)0x00
		&& ControlData.DeviceRequest.wValue == 0x0001)
	{
(*(volatile unsigned char *)0x59)=0x00;
		remote_wakeup = 0;
(*(volatile unsigned char *)0x59)=0x01;
		single_transmit(0, 0);
	}
	else if (bRecipient == (unsigned char)0x02
		&& ControlData.DeviceRequest.wValue == 0x0000)
	{
		endp = (unsigned char)(ControlData.DeviceRequest.wIndex & (unsigned char)0x3);
		if (ControlData.DeviceRequest.wIndex & 0x80)
			D12_SetEndpointStatus(endp*2 + 1, 0);
		else
			D12_SetEndpointStatus(endp*2, 0);
		single_transmit(0, 0);
	}
	else
	{
		D12_SetEndpointStatus(0, 1);
		D12_SetEndpointStatus(1, 1);
	}
}


void set_feature(void)
{
 unsigned char endp;
 unsigned char bRecipient = ControlData.DeviceRequest.bmRequestType & (unsigned char)0x1F;

	if (bRecipient == (unsigned char)0x00
		&& ControlData.DeviceRequest.wValue == 0x0001)
	{
(*(volatile unsigned char *)0x59)=0x00;
		remote_wakeup = 1;
(*(volatile unsigned char *)0x59)=0x01;
		single_transmit(0, 0);
	}
	else if (bRecipient == (unsigned char)0x02
		&& ControlData.DeviceRequest.wValue == 0x0000)
	{
		endp = (unsigned char)(ControlData.DeviceRequest.wIndex & (unsigned char)0x3);
		if (ControlData.DeviceRequest.wIndex & 0x80)
			D12_SetEndpointStatus(endp*2 + 1, 1);
		else
			D12_SetEndpointStatus(endp*2, 1);
		single_transmit(0, 0);
	}
	else
	{
		D12_SetEndpointStatus(0, 1);
		D12_SetEndpointStatus(1, 1);
	}
}


void set_address(void)
{
	D12_SetAddressEnable((unsigned char)(ControlData.DeviceRequest.wValue & 0x7f), 1);
	single_transmit(0, 0);
}


void get_descriptor(void)
{
 unsigned char bDescriptor =(((ControlData.DeviceRequest.wValue) >> 8) & 0xFF);

	if (bDescriptor == 0x01)
	{

 		code_transmit((unsigned char *)&DeviceDescr, 0x12);
	}
	else if (bDescriptor == 0x02)
	{
		code_transmit((unsigned char *)&ConfigDescr, 0x2e);
	}
	else
	{
		D12_SetEndpointStatus(0, 1);
		D12_SetEndpointStatus(1, 1);
	}
}


void get_configuration(void)
{
 unsigned char c = configuration;

	single_transmit(&c, 1);
}


void set_configuration(void)
{
	if (ControlData.DeviceRequest.wValue == 0)
	{

		single_transmit(0, 0);
(*(volatile unsigned char *)0x59)=0x00;
		configuration = 0;
(*(volatile unsigned char *)0x59)=0x01;
		init_unconfig();
	}
	else if (ControlData.DeviceRequest.wValue == 1)
	{

		single_transmit(0, 0);
		init_unconfig();
		init_config();
(*(volatile unsigned char *)0x59)=0x00;
		configuration = 1;
(*(volatile unsigned char *)0x59)=0x01;
	}
	else
	{
		D12_SetEndpointStatus(0, 1);
		D12_SetEndpointStatus(1, 1);
	}
}


void get_interface(void)
{
 unsigned char txdat = 0;

	single_transmit(&txdat, 1);
}


void set_interface(void)
{
	if (ControlData.DeviceRequest.wValue == 0 && ControlData.DeviceRequest.wIndex == 0)
		single_transmit(0, 0);
	else
	{
		D12_SetEndpointStatus(0, 1);
		D12_SetEndpointStatus(1, 1);
	}
}


void ep0_rxdone(void)
{
	unsigned char ep_last, i;

	ep_last = D12_ReadLastTransactionStatus(0);
	if (ep_last & 0x20)
	{
		ControlData.wLength = 0;
		ControlData.wCount = 0;
		if( D12_ReadEndpoint(0, sizeof(ControlData.DeviceRequest),
			(unsigned char *)(&(ControlData.DeviceRequest))) != 0x08 )
		{
			D12_SetEndpointStatus(0, 1);
			D12_SetEndpointStatus(1, 1);
			control_state = 0;
			return;
		}
		D12_AcknowledgeEndpoint(0);
		D12_AcknowledgeEndpoint(1);
		ControlData.wLength = ControlData.DeviceRequest.wLength;
		ControlData.wCount = 0;

		if (ControlData.DeviceRequest.bmRequestType & 0x80)
		{
			setup_packet = 1;
			control_state = 1;
		}
		else
		{
			if (ControlData.DeviceRequest.wLength == 0)
			{
				setup_packet = 1;
			    control_state = 0;
			}
			else
			{
				if(ControlData.DeviceRequest.wLength > 8)
				{

					control_state = 0;
					D12_SetEndpointStatus(0, 1);
					D12_SetEndpointStatus(1, 1);
				}
				else
				{
					control_state = 2;
				}
			}
		}
	}
	else if (control_state == 2)
	{

		i =	D12_ReadEndpoint(0, 16,
			ControlData.dataBuffer + ControlData.wCount);
		ControlData.wCount += i;
		if( i != 16 || ControlData.wCount >= ControlData.wLength)
		{

			setup_packet = 1;
			control_state = 0;
		}
	}
	else
	{
		control_state = 0;
	}
}


void ep0_txdone(void)
{
	short i = ControlData.wLength - ControlData.wCount;

	D12_ReadLastTransactionStatus(1);

	if (control_state != 1)
		return;
	if( i >= 16)
	{

(*(volatile unsigned char *)0x59)=0x00;
 		outportb(1, 1);
		num = inportb(0);
 		outportb(1, 0xF0);
		outportb(0, 0);
		outportb(0, 16);
		for(num=0; num<16; num++)
			outportb(0, *(ControlData.pData + ControlData.wCount+num));
 		outportb(1, 0xFA);
(*(volatile unsigned char *)0x59)=0x01;
		ControlData.wCount += 16;
		control_state = 1;
	}
	else if( i != 0)
	{

(*(volatile unsigned char *)0x59)=0x00;
 		outportb(1, 1);
		num = inportb(0);
 		outportb(1, 0xF0);
		outportb(0, 0);
		outportb(0, i);
		for(num=0; num<i; num++)
			outportb(0, *(ControlData.pData + ControlData.wCount+num));
 		outportb(1, 0xFA);
(*(volatile unsigned char *)0x59)=0x01;
		ControlData.wCount += i;
		control_state = 0;
	}
	else if (i == 0)
	{
(*(volatile unsigned char *)0x59)=0x00;
 		outportb(1, 1);
		num = inportb(0);
 		outportb(1, 0xF0);
		outportb(0, 0);
		outportb(0, 0);
 		outportb(1, 0xFA);
(*(volatile unsigned char *)0x59)=0x01;
 		control_state = 0;
	}
}



void ep1_txdone(void)
 {
	 D12_ReadLastTransactionStatus(3);
	 D12_WriteEndpoint(3,2,TxdBuf);
 }


void ep1_rxdone(void)
{
 unsigned char Len,i;
 unsigned char StrLen;

	D12_ReadLastTransactionStatus(2);
	Len = D12_ReadEndpoint(2, 16, RxdBuf);

 if(RxdBuf[0]==0xff)
	{
	 Work.mission_number = RxdBuf[1];
	 Work.strat_number=RxdBuf[2];
	 Work.muster_volume=RxdBuf[3];
	 Work.clear_time=RxdBuf[4];
	 volume=Work.muster_volume*560;
	 clear_S=Work.clear_time*59;
	 Work.clear_select=RxdBuf[5];

(*(volatile unsigned char *)0x4E) =0x05;
(*(volatile unsigned char *)0x57)=0x04;
	     Work_flag=1;
	}

 else if(RxdBuf[0]==0XEE)
    {
        Debugger.place_number = RxdBuf[1];



	    Debugger.recoup_value=RxdBuf[2];
	    Debugger.water_pump=RxdBuf[3];
	    Debugger.air_pump=RxdBuf[4];
	    Debugger.init_place=RxdBuf[5];
	    Debugger.armup_down=RxdBuf[6];
        Debugger.setp_num=RxdBuf[7]*256+RxdBuf[8];
	    Debugger.add_sub=RxdBuf[9];
		debug_flag=1;
	}

 else if (RxdBuf[0]==0xDD)

		reset_flag=1;

 }

void main_txdone(void)
{
	D12_ReadLastTransactionStatus(5);
}


void main_rxdone(void)
{
 unsigned char Len;
 unsigned char StrLen;

	D12_ReadLastTransactionStatus(4);
	Len = D12_ReadEndpoint(4, 64, RxdBuf);

}


void MainSend(void)
{
 unsigned char StrLen;
(*(volatile unsigned char *)0x59)=0x00;
	outportb(1, 3);
	StrLen = inportb(0);
	outportb(1, 0xF0);
	outportb(0, 0);
	outportb(0, 16);
	for(StrLen=0; StrLen<16; StrLen++)
		outportb(0, *(RxdBuf+StrLen));
	outportb(1, 0xFA);
(*(volatile unsigned char *)0x59)=0x01;
}


#pragma interrupt_handler INTHandler:2
void INTHandler(void)
{
 unsigned int i_st;
(*(volatile unsigned char *)0x59)=0x00;
	i_st = D12_ReadInterruptRegister();
	if(i_st != 0)
	{
		if(i_st & 0x01)
		{
			ep0_rxdone();
		}
		if(i_st & 0x02)
		{
			ep0_txdone();
		}
		if(i_st & 0x04)
		{
			ep1_rxdone();
		}
		if(i_st & 0x08)
		{
			ep1_txdone();
		}
		if(i_st & 0x10)
		{
			main_rxdone();
		}
		if(i_st & 0x20)
		{
			main_txdone();
		}
	}
(*(volatile unsigned char *)0x59)=0x01;
}


void disconnect_USB(void)
{
	D12_SetMode(0x02, 0x40 | 0x03);
}


void connect_USB(void)
{

	D12_SetDMA(0x80 | 0x40|0X10);
	D12_SetMode(0x02|0x10, 0x40 | 0x03);
}


void reconnect_USB(void)
{
 unsigned long clk_cnt;
	disconnect_USB();
	for(clk_cnt=0;clk_cnt<0x800;clk_cnt++);
	connect_USB();
}


void init_unconfig(void)
{
	D12_SetEndpointEnable(0);
}


void init_config(void)
{
	D12_SetEndpointEnable(1);
}


void single_transmit(unsigned char * buf,unsigned char len)
{
	if( len <= 16) {
		D12_WriteEndpoint(1, len, buf);
	}
}


void code_transmit(unsigned char * pRomData,unsigned short len)
{
	ControlData.wCount = 0;
	if(ControlData.wLength > len)
		ControlData.wLength = len;
	ControlData.pData = pRomData;
	if( ControlData.wLength >= 16)
	{
		D12_WriteEndpoint(1, 16, ControlData.pData);
		ControlData.wCount += 16;
(*(volatile unsigned char *)0x59)=0x00;
		control_state = 1;
(*(volatile unsigned char *)0x59)=0x01;
	}
	else
	{
		D12_WriteEndpoint(1, ControlData.wLength, pRomData);
		ControlData.wCount += ControlData.wLength;
(*(volatile unsigned char *)0x59)=0x00;
		control_state = 0;
(*(volatile unsigned char *)0x59)=0x01;
	}
}


void get_firmware_version()
{
	unsigned char i;

	i = 0x30;
	single_transmit((unsigned char *)&i, 1);
}


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);
		}

	}
	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)
				{
(*(volatile unsigned char *)0x59)=0x00;
					outportb(1, 0x80 + 4);
					epstatus = inportb(0);
(*(volatile unsigned char *)0x59)=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()
{
 unsigned char type, req;
	type = ControlData.DeviceRequest.bmRequestType & (unsigned char)0x60;
	req = ControlData.DeviceRequest.bRequest & (unsigned char)0x0F;

	if (type == (unsigned char)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 == (unsigned char)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)
{
(*(volatile unsigned char *)0x59) = 0x00;
(*(volatile unsigned char *)0x55) = 0X80;
(*(volatile unsigned char *)0x6A) = 0x00;
(*(volatile unsigned char *)0x59) = 0x00;

(*(volatile unsigned char *)0x6D) = 0X00;
}




void DelayMs(unsigned int TimeNum)
 {
 unsigned int i;
   for(;TimeNum>0;TimeNum--)
    {
       for(i=0;i<30000;i++);
    }
 }



void   Pump_Delay(unsigned int delayTime)
  {
(*(volatile unsigned char *)0x62)&=0XFC;
	 DelayMs(10);
(*(volatile unsigned char *)0x62)&=0XFE;
     DelayMs(delayTime);
(*(volatile unsigned char *)0x62)|=0X02;
     DelayMs(10);
(*(volatile unsigned char *)0x62)|=0X01;
  }



  void  push_air (void)
  {
(*(volatile unsigned char *)0x62)&=0XFC;
	 DelayMs(10);
(*(volatile unsigned char *)0x62)&=0XFE;
     mustermotor_contorl (17000,1);
	 mustermotor_contorl (17000,0);
	 DelayMs(30);
	 mustermotor_contorl (17000,1);
     mustermotor_contorl (17000,0);
	 DelayMs(30);
(*(volatile unsigned char *)0x62)|=0X02;
     DelayMs(10);
(*(volatile unsigned char *)0x62)|=0X01;
  }




 void  needle_egis (void)
  {
(*(volatile unsigned char *)0x30)=(*(volatile unsigned char *)0x32);
	  g[5]=(*(volatile unsigned char *)0x30)&0x10;
	  while(g[5]==0)
	  {
(*(volatile unsigned char *)0x30)=(*(volatile unsigned char *)0x32);
	    g[5]=(*(volatile unsigned char *)0x30)&0x10;
	  }
  }



void  armup_init  (void)
 {
(*(volatile unsigned char *)0x30)=(*(volatile unsigned char *)0x32);
       g[0]=(*(volatile unsigned char *)0x30)&0x04;
       while(g[0])
       {
(*(volatile unsigned char *)0x30)=(*(volatile unsigned char *)0x32);
        g[0]=(*(volatile unsigned char *)0x30)&0x04;
        motor_right (1,1,50);
	   }
	   OutputMotor(0x01,0x00,0x00);
 }



void  muster_init (void)
 {

	 armup_init ();

	 plate_oneround ();
	 plate_init();
	 OutputMotor(0x03,0x00,0x00);
	 armround_init();

	 DelayMs(15);

	 needle_egis ();
	 arm_down(3150*8);
	 OutputMotor(0x02,0x00,0x00);
     mustermotor_init();
	 Pump_Delay(130);


  }




 void   one_course  (void)
    {
 unsigned int i;

	   	ArmState=0;

		arm_up ();
	   mustermotor_contorl (7500,1);
	   plate_round(Work.strat_number,Work.mission_number);
		if(Work.mission_number)
		    armround_control1(0,1,3703);
		else
		    armround_control1(0,2,6385);

		DelayMs(20);

		arm_down(3220*8);

		ArmState=2;
	    OutputMotor(0x03,0x00,0x00);

		mustermotor_contorl (volume,1);
        DelayMs(20);
		if(Work.clear_select==0)
		DelayMs(50);
	    arm_up ();

⌨️ 快捷键说明

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