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

📄 zoubeifuwei.c

📁 这个事自己做的第一个单片机程序
💻 C
📖 第 1 页 / 共 3 页
字号:
        PORTB &=~0x02;
        delay1(rate);
		PORTB |=0x02;
        delay1(rate);
        if(rate>=high)
           rate-=5;
        step++;
		if(step>i)
		break;
	   }
    while(!END_GO)              //快要到位置的减速阶段
	  {
	    PORTB &=~0x02;
        delay1(rate);
		PORTB |=0x02;
        delay1(rate);
           rate+=2;
		   
        step++;	
        if(step>MAXstep)
         {
          M_LOWMOVE;
          M_UNENABLE;
		  ClrCurrent();
          Warn(2);                         //报警程序
          return 0;
         } 
      }       
    delay(100,1000);

    if(FX)
        M_CLOCKWISE;
    else M_UNCLOCKWISE;
    rate=180;
    step=0;                         //复位
    while(!START_GO)
      {
        PORTB &=~0x02;
        delay1(rate);
		PORTB |=0x02;
        delay1(rate);
        if(rate>=high)
           rate-=5;
        step++;
        if(step>MAXstep)
         {
          M_LOWMOVE;
          M_UNENABLE;
		  ClrCurrent();
          Warn(1);                         //报警程序
          return 0;
         } 
       }        
    delay(100,100);
	ClrCurrent();
    M_LOWMOVE;
   //Eflag=0;
    return 1;
  }
  
//预温电机动作1,需要跨区走的程序,新的
uchar YW1move(uint mstep)
{
   uint i;
   uchar FX;
   uint rate;
   uint high=100;
   uchar j;
   uint step=0;
   rate=180;
   if(START_GO)        //当前在通光区,要去挡光
   {
        M_CLOCKWISE;
        FX=0x01;
        if(mstep>MAXstepDG)
        {
            mstep=MAXstepDG;
            Warn(0x11);
        }
   }
   else                //当前在挡光区,要去通光
   {
        M_UNCLOCKWISE;
        FX=0;
        if(mstep>MAXstepTG)
        {
            mstep=MAXstepTG;
            Warn(0x11);
        }
   }
   
   SetCurrent(10);
   M_ENABLE;
   M_HIGHMOVE;

   if(START_GO)         //在通光区,先向零点走
   {
        while(START_GO)
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(rate>=high)
                rate-=5;
            step++;
            if(FX)
    			Ystep--;
            else
    			Ystep++;
            if(Ystep<1970)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(2);                         //报警程序
                return 0;
            } 
            if(step>MAXstepTG)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(3);                         //报警程序
                return 0;
            } 
        }
		Ystep=2000;
    } 
    else               //在挡光区,先向零点走
    {
        while(!START_GO)
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(rate>=high)
                rate-=5;
            step++;
            if(FX)
    			Ystep--;
            else
    			Ystep++;
            if(Ystep>2030)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(2);                         //报警程序
                return 0;
            } 
            if(step>MAXstepDG)
            {
                M_LOWMOVE;
                M_UNENABLE;
				ClrCurrent();
                Warn(3);                         //报警程序
                return 0;
            } 
        }
		Ystep=2000;
    }  

    if(mstep<=30)                          //跨区后需要走的步数<=30
    {
        for(i=0;i<mstep;i++)
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(FX)
    			Ystep--;
            else
    			Ystep++;
            if(rate>=100)
                rate-=5;
        }
    }       
    else                                  //跨区后需要走的步数>30
    {
        for(i=0;i<=mstep-30;i++)
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(FX)
    			Ystep--;
            else
    			Ystep++;
            if(rate>=100)
                rate-=5;
        }
        
        for(i=0;i<=30;i++)
        {
            PORTB &= ~0x02;
            delay1(rate);
            PORTB |= 0x02;
            delay1(rate);
            if(FX)
    			Ystep--;
            else
    			Ystep++;
            if(rate<=180)
                rate+=5;
        }
    }
	delay(100,100);
	ClrCurrent();
	M_LOWMOVE;
	//Eflag=0;
	return 1;
}
  
//预温电机动作1,BZ为1则到零点后继续走,即为需要跨区走的程序,旧的
  uchar YW1moveold(uchar BZ,uint mstep)
  {
   uint i;
   uchar FX;
   uint rate;
   uint high=100;
   uchar j;
   uint step=0;
   rate=180;
   if(!START_GO) 	  		  //当前挡光
   {  
	 if(BZ && (mstep>MAXstepTG))
	 {
	  	mstep=MAXstepTG;
	 	Warn(0x11);
	 }
	 if(!BZ && (mstep>MAXstepDG))
	 {
	    mstep=MAXstepDG;
	 	Warn(0x11);
	 }
   }
   if(START_GO)	   			//当前通光
   {  
	 if(BZ && (mstep>MAXstepDG))
	 {
	  	mstep=MAXstepDG;
	 	Warn(0x11);
	 }
	 if(!BZ && (mstep>MAXstepTG))
	 {
	    mstep=MAXstepTG;
	 	Warn(0x11);
	 }
   }
   
   SetCurrent(10);
   M_ENABLE;
   M_HIGHMOVE;
   
   if(START_GO)        //在通光区 
   {
	  M_CLOCKWISE;
	  FX=0x01;
	  /*if(mstep>MAXstepTG)
      {
        mstep=MAXstepTG;
	    Warn(0x11);
      }*/
   }
   else                //在挡光区
   {
	  M_UNCLOCKWISE;
	  FX=0;
	  /*if(mstep>MAXstepDG)
      {
         mstep=MAXstepDG;
	     Warn(0x11);
      }*/
   }
   
   if(BZ)                 //如果需要跨区
   {
	 if(START_GO)         //在通光区,先向挡光区走
	 {
	     while(START_GO)
         {
           PORTB &=~0x02;
           delay1(rate);
		   PORTB |=0x02;
           delay1(rate);
           if(rate>=high)
               rate-=5;
           step++;
		   if(FX) Ystep--;
		   else   Ystep++;
           if(Ystep<1975)
           {
              M_LOWMOVE;
              M_UNENABLE;
			  ClrCurrent();
              Warn(2);                         //报警程序
              return 0;
           } 
		   if(step>MAXstepTG)
           {
              M_LOWMOVE;
              M_UNENABLE;
			  ClrCurrent();
              Warn(3);                         //报警程序
              return 0;
           } 
		   Ystep=2000;
        }
	  } 
	  else                            //在挡光区,需要先向通光区走
	  {
	     while(!START_GO)
         {
            PORTB &=~0x02;
            delay1(rate);
		    PORTB |=0x02;
            delay1(rate);
            if(rate>=high)
                   rate-=5;
            step++;
		    if(FX) Ystep--;
		    else   Ystep++;
            if(Ystep>2025)
            {
               M_LOWMOVE;
               M_UNENABLE;
			   ClrCurrent();
               Warn(2);                         //报警程序
               return 0;
            } 
			if(step>MAXstepDG)
           {
              M_LOWMOVE;
              M_UNENABLE;
			  ClrCurrent();
              Warn(3);                         //报警程序
              return 0;
           }
		    Ystep=2000;
         }
	   }  
	  
       if(mstep<=30)                          //跨区后需要走的步数(小于30)
       {
         for(i=0;i<mstep;i++)
         {
    	   PORTB &=~0x02;
    	   delay1(rate);
    	   PORTB |=0x02;
    	   delay1(rate);
    	   if(FX) Ystep--;
    	   else   Ystep++;
    	   if(rate>=100)
    	   rate-=5;
    	 }
       }       
       else                                  //跨区后需要走的步数
       {
    	 for(i=0;i<=mstep-30;i++)
    	 {
    	    PORTB &=~0x02;
    	    delay1(rate);
    	    PORTB |=0x02;
    	    delay1(rate);
    	    if(FX) Ystep--;
    	    else   Ystep++;
    	    if(rate>=100)
    	      rate-=5;
    	 }
    	 
    	 for(i=0;i<=30;i++)
    	 {
    	    PORTB &= ~0x02;
    	    delay1(rate);
    	    PORTB |= 0x02;
    	    delay1(rate);
    	    if(FX) Ystep--;
    	    else   Ystep++;
    	    if(rate<=180)
    	      rate+=5;
    	 }	 
       }
  }
  else                    //不需要跨区
  {
	  while(!START_GO)
      {
        PORTB &=~0x02;
        delay1(rate);
		PORTB |=0x02;
        delay1(rate);
        if(rate>=high)
           rate-=5;
        step++;
        if(step>MAXstepDG)
         {
            M_LOWMOVE;
            M_UNENABLE;
			ClrCurrent();
            Warn(3);                         //报警程序
            return 0;;
         } 
		 Ystep=2000;
      }       
	}
	delay(100,100);
	ClrCurrent();
	M_LOWMOVE;
	//Eflag=0;
	return 1;
  }
  
//预温电机动作2,即向指定方向走指定步数,肯定不跨区
uchar YW2move(uchar FX,uint mstep)         
{
    uint i;
    uint rate;
    rate=180;
	if(START_GO && mstep>MAXstepTG)
	{
	 	mstep = MAXstepTG;
		Warn(0x11);
	}
	if(!START_GO && mstep>MAXstepDG)
	{
	 	mstep = MAXstepDG;
		Warn(0x11);
	}
    if(FX)
	    M_CLOCKWISE; 	  //向下,即向前
	else 
	    M_UNCLOCKWISE;	  //向上,即向后
	
	SetCurrent(10);
    M_ENABLE;
    M_HIGHMOVE;
    
    if(mstep<=30)                           //走指定步数,需要分档
    {
        for(i=0;i<mstep;i++)
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(FX)
        		Ystep--;
            else
        		Ystep++;
            if(rate>=100)
                rate-=5;
        }
    }
    else 
    {
        for(i=0;i<mstep-30;i++)                //加速阶段
        {
            PORTB &=~0x02;
            delay1(rate);
            PORTB |=0x02;
            delay1(rate);
            if(FX)
        		Ystep--;
            else
        		Ystep++;
            if(rate>=100)
                rate-=5;

⌨️ 快捷键说明

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